0

what is the difference between the statements int *p = (int*) i; and int *q=&i; Here is whole program

#include<stdio.h>
int main(){
     int i;
     int *p = (int*) i; 
     printf("\n p is %d ",p);
     int *q = &i;
     printf("\n q is %d ",q);
     return 0;
 }

the output obtained is p is 22092 q is 1002476148 i think here both p and q are storing the address of i , but they are showing different values, can somebody explain why it is happenning?

  • so if (int *) i is returning the value in i then what is the actual use of this (int *), like where it is used –  Oct 27 '18 at 17:54
  • since `i` is a default initialized, it will contain random value, which is being stored to pointer `p` too, that is why you are getting two values. – Nishant Singh Oct 27 '18 at 18:02

3 Answers3

2

First: pointers must be cast to (void*) and printed with %p. %d prints an int in base 10. That is,

#include <stdio.h>

int main(){
    int i;
    int *p = (int*) i; 
    printf("\n p is %p ", (void*)p);
    int *q = &i;
    printf("\n q is %p ", (void*)q);
}

Now let's try to compile the program with these changes: only 2 errors:

% gcc ptr.c -Wall -Wextra
ptr.c: In function ‘main’:
ptr.c:5:14: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
     int *p = (int*) i;
              ^
ptr.c:5:14: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
     int *p = (int*) i;
              ^~~~~~~~

Both pertanining to the int *p = (int*) i;; the second says that the value of i is used but we didn't set any value to i (this causes undefined behaviour) and the other that we're trying to convert an integer to a pointer, and the integer doesn't have the same amount of bits as a pointer has on this platform.

I.e.

int *q = &i;

initializes pointer to int q with the address of variable i, whereas

int *p = (int*) i; 

interprets the garbage value contained in i, in an implementation-defined manner, as an address, and initializes p with that.

Not quite equal.

1

Here

int *p = (int*) i; /* i is not initialized */

pointer p gets assigned with value of i which is some garbage data & tries to cast some junk data to int* type and assigned to p. If you tries to dereference p it gives segmentation fault & causes undefined behavior.

And Here

int *q = &i;

pointer q is assigned with valid address.

Also while printing pointer variable use %p instead of %d format specifier for e.g

 printf("\n q is %p ",(void*)q);

About assigning integer values to a pointer like int *p = (int*) i , C standard says

6.3.2.3 Pointers

(5) An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

Achal
  • 11,821
  • 2
  • 15
  • 37
  • Read [Can I assign any integer value to a pointer variable directly?](https://stackoverflow.com/questions/45766142/can-i-assign-any-integer-value-to-a-pointer-variable-directly). – Achal Oct 27 '18 at 18:09
0

According to the C standard ISO/IEC 9899:2011, subclause 6.3.2.3:

An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

It follows that when you cast i to pointer-to-int type, what it holds is generally undefined.

Neb
  • 2,270
  • 1
  • 12
  • 22