Question

I put this code into eclipse and run it

main()
{
  int *p, *q, *r;
  int a = 10, b = 25;
  int c[4] = {6,12,18,24};

  p = c;

  printf("p = %d\n" ,p);
}

the output I get is p = 2358752

what is this number supposed to represent? Is it the address of the variable?

If what i'm saying above is true would my answer to the following question be correct?

so lets say the following are stored at the following locations

address      variables

5000         p

5004         q

5008         r

500C         a

5010         b

5014        c[0]

5018        c[1]

501C        c[2]

5020        c[3]

so would would the line

p = c;

be 5014?

Was it helpful?

Solution 2

Yes, p is the address of c, which is the same as the address of c[0]. And yes, in your second example, p would be equal to 5014.

OTHER TIPS

int *p,

The above statement defines p to be a pointer to an integer. In the below statement, c is implicitly converted to a pointer to the first element of the array a.

p = c;
// equivalent to
p = &c[0];

Therefore, p contains the address of the first element of the array. Also, the conversion specifier to print an address is %p.

printf("p = %p\n", (void *)p);
// prints the same address
printf("c = %p\n", (void *)c);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top