-1

Is there a way to assign a pointer value (not the dereferenced value, but the actual address of the variable) to another variable? For instance

int main(int argc, char **argv){
   unsigned int *p;
   size_t q;
   q = (size_t) p;
   printf("p = %x q = %x\n", p, q)
}

does not seem to do the job... I get 0,0 for each of these...

Rajat Mitra
  • 167
  • 1
  • 11
  • You haven't assigned anything to `p`. You could get any value printed. What were you expecting as output? – John Zwinck Oct 13 '19 at 03:36
  • @JohnZwinck p is a pointer variable on the stack it should have an address, he is trying to print the address. – Moshe Rabaev Oct 13 '19 at 03:37
  • @MosheRabaev: OP is printing the address stored in `p`. But that address was never assigned, so it could print anything. It's printing arbitrary garbage, and the fact that it printed 0 is just "luck." – John Zwinck Oct 13 '19 at 03:39
  • Thanks guys! as soon as p is initialized, I am getting the correct behavior. I added - int r; p = &r; and things worked normally. – Rajat Mitra Oct 13 '19 at 03:43
  • You might consider [this](https://stackoverflow.com/questions/23963269/can-someone-explain-how-pointer-to-pointer-works/23964156#23964156) answer. – Mahonri Moriancumer Oct 13 '19 at 03:49

1 Answers1

0

To print an address in c, you must send the address of variable &variable.

int main(void) {
   int i = 0;

   printf("The address of the variable is : %p", &i);
}
cerfio
  • 26
  • 1
  • 4