开发者

storing an integer constant other than zero in a pointer variable

开发者 https://www.devze.com 2022-12-23 05:36 出处:网络
int main() { int *d=0; printf(\"%d\\n\",*d); return 0; } this works fine. >cc legal.c > ./a.out 0 if i change the statement int *d=0; to int *d=1;
int main()
{

int *d=0;
printf("%d\n",*d);

return 0;
}

this works fine.

>cc legal.c
> ./a.out
0

if i change the statement int *d=0; to int *d=1; i see the error.

cc: "legal.c", line 6: error 1522: Cannot initialize a pointer with an integer constant other than zero.

so its obvious that it will allow only zer开发者_运维百科o.i want to know what happens inside the memory when we do this int *d=0 which is making it valid syntax.

I am just asking this out of curiosity!


I'm surprised that you didn't get a SEGFAULT when you ran your code. The *d in the printf statement is dereferencing the pointer. To answer your question, though, C++ allows 0 to be given as a default initializer for any object, which is why it can be used to initialize the pointer to null (0 and null are identical). With the value of 1, you are asking the compiler to convert an integer to a pointer, which requires an explicit cast.


When initializing a pointer with 0, that 0 is implicitly converted to a null-pointer. How that null-pointer looks depends on your platform, the compiler will use the correct binary value.

When you try to initialize the pointer with 1 (or any other non-zero integer) the compiler doesn't know to convert this value to a valid pointer and issues a warning.


You are creating a pointer variable called d on the stack which is said to "point to an integer". You then assign that pointer variable to 0 which makes it point to memory address 0x0 which valid (and the same as NULL in C).

To make this clearer, int *d = 0 is the same as:

   int *d;
   d = 0;       // set it to address 0

If you want to point to an integer 1 then you need this:

   int x = 1;
   int *d = &x; // "set it to 'address of x'"


Incase of *d = 0, your integer pointer d is getting initialized to 0 which is valid. Basically you are declaring an integer pointer so it makes sense to initialize a pointer.

But, you don't want to initialize the pointer, rather initialize memory pointed to by it, which is incorrect.

When you do *d = 1, the pointer values becomes 1, and when your printf statement gets executed, it will try to access the value at address 1 which will not be allowed.

Hope this helps.


In your "working" example you are dereferencing a null pointer and the language is putting whatever bits it finds as the argument to printf. That it works at all is a totally implementation dependent feature of your compiler and machine and will likely segfault in another implementation.

That the code works seems to be an indication that your compiler is doing something odd behind the scenes in attempt to "protect" coders from a very common error; that's a bad idea. I'd love to see what assembly is generated by your compiler with cc -s


In ISO-C99, there are two types of null pointer constants: integer constant expression of value 0 - eg 0, 1 - 1, (int)0.0 - and such expressions cast to void * - eg (void *)0, which is often used to define NULL.

Converting a null pointer constant to an arbitrary pointer type yields a null pointer of that type. This conversion is implicit, but may actually involve address translation as the standard doesn't require null pointers to have the bit-representation 0.

This conversion is also defined for function pointer types, even if it's normally illegal to convert object pointers to function pointers:

void (*foo)(void) = (void *)0;           // valid
void *bar = 0;                           // valid
void (*baz)(void) = (void (*)(void))bar; // invalid even with explicit cast

This also means that you can use 0 to initialize any scalar type without casting, and it's the only value for which this is true: Converting 0 to pointer types will always yield a null pointer, whereas converting any other integral value is possible, but requires explicit casting, has an implementation-defined result and might fail due to alignment or address space restrictions.

0

精彩评论

暂无评论...
验证码 换一张
取 消