简单指针初始化 [英] simple Pointer initialization

查看:165
本文介绍了简单指针初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我使用指针,我只想快速检查如何初始化整数指针。

  a)int * tmpPtr = 0; 

b)int * tmpPtr = null;

c)int a = 0;
int * tmpPtr =& a;

EDIT



感谢您迄今为止的所有答案。有趣的是,如果我初始化
指针如下,那么mem :: copy操作正常。

  int tmp = 0; 
int * tmpPtr =& tmp;
Mem :: Copy((void *)tmpPtr,basepointer(),sizeof(int));

但是,如果我这样做:

  int * tmpPtr = 0; 
Mem :: Copy((void *)tmpPtr,basepointer(),sizeof(int));

那么我在mem :: copy ...时遇到崩溃



很奇怪!

解决方案

简单的问题很好,我认为SO的意思是



在C中没有 null (除非你自己定义) 。可以通过以下两种方式之一来初始化为空指针:

  int * p = 0; 
int * p = NULL;

如果您之后取消引用 p (我相信它是根据标准的未定义的行为,所以真的,任何事情可能发生,直到和包括,宇宙的完全湮灭 - 它甚至可以继续运行良好,但我不会依靠它)。



要获得指向真实整数的指针,只需使用:

  int a = 7; 
int * p =& a;

使用运算符的地址。



重新编辑,这根本不奇怪,你只需要可视化它。让我们假设所有变量都是从内存位置100开始创建的,整数和指针的长度都是4字节。将两种情况分解为最简单的形式:

  int x = 2; 
int * px = 0; int * px =& x;

+ ----- + + ----- +
px(100):| 0 | x(100)| 2 |
+ ----- + + ----- +
px(104)| 100 |然后,您执行命令

$


$ -
$ -


  * px = 7; 

尝试更改 px



在左侧,你将尝试将值7写入内存位置0.这是一件坏事;非常少的系统将允许你这样做而不崩溃,更少的将允许它没有任何负面影响(一些版本的HP-UX实际上工作良好)。



在右侧是应该发生。从 px 中拾取的值为100,因此值7写入该内存位置,根据需要更改 x



我经常找到图片(甚至原始的ASCII艺术图片,因为我没有鲁本斯或波提切利)帮助澄清概念。希望它为你清理了一点。


It has been a while that I used pointers and I just wanna quickly check how I can initialize an integer pointer?

a) int *tmpPtr = 0;

b) int *tmpPtr = null;

c) int a = 0;
   int *tmpPtr = &a;

EDIT

Thanks for all your answers so far. The funny thing is, that if I intitalize the pointer as follows, then the mem::copy operation works fine.

int tmp = 0;
int *tmpPtr = &tmp;
Mem::Copy((void*)tmpPtr, basepointer(), sizeof(int));

However, if I do it like this:

int *tmpPtr = 0;
Mem::Copy((void*)tmpPtr, basepointer(), sizeof(int));

then I get a crash during mem::copy...

Weird!

解决方案

Simple questions are fine, I think it's well established that SO is meant to be for all levels, not just an elite.

There is no null in C (unless you define it yourself). Initializing to a null pointer can be done in one of the following two ways:

int *p = 0;
int *p = NULL;

If you dereference p after that, you're likely to get an access violation (I believe it's undefined behavior according to the standard, so really, anything could happen, up to and including, total annihilation of the universe - it may even continue to run fine, but I wouldn't rely on it).

To get a pointer to a real integer, just use:

int a = 7;
int *p = &a;

using the address-of operator.

Re your edit, it's not weird at all, you just need to visualize it. Let's pretend that all variables are created starting at memory location 100 and integers and pointers are both 4 bytes in length. Breaking your two situations down to their simplest form:

                                  int x = 2;
int *px = 0;                      int *px = &x;

         +-----+                          +-----+
px(100): |   0 |                  x(100)  |   2 |
         +-----+                          +-----+
                                  px(104) | 100 |
                                          +-----+

Then you execute the command

*px = 7;

in an attempt to change the variable pointed to by px.

On the left-hand side, you will try to write the value 7 to memory location 0. That's a bad thing; very few systems will allow you to do that without crashing, even fewer will allow it without any adverse effects at all (some versions of HP-UX actually worked okay).

On the right side is what should happen. The value picked up from px is 100, so the value 7 is written to that memory location, changing x as intended.

I often find pictures (even primitive ASCII art ones, since I'm no Rubens or Botticelli) help clarify the concepts. Hopefully it's cleared it up a little for you.

这篇关于简单指针初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆