1

In many tutorial videos and explanations of C programming, for representing arrays like int a[5] = {9,2,1,3,4}; Tutors/teachers always make a separate box for storing the address of first element of array i.e address of 9 in this case.

Which means that the total memory consumed by the array should be (memory consumed by the constant pointer a to store the address of first element + 5*(sizeof(int)).

If that's the case then why this program which is mentioned below gives such an output.

Program

#include <stdio.h>
int main() 
{ 
    int a[5] = {6,1,2,9,3}; 
    int * p = a;
    printf("a is %u\n", a); 
    printf("&a is %u\n", &a);
    printf("&a+1 is %u\n", &a+1);
    printf("p is %u\n", p); 
    printf("&p is %u", &p); 
    return 0; 
} 

Output

a is 2128206832
&a is 2128206832
&a+1 is 2128206852
p is 2128206832
&p is 2128206824

Here a and &a give the same address. Which shouldn't be the case at all when looking at the representation mostly used by the people to represent arrays in C. According to their diagram representation of array, it should have given different values for a and &a, just like p and &p. So how array is truly represented inside of the memory? Sorry for such a trivial doubt, but I am confused.

  • And does a create a separate memory space to store the address of first element of array?
  • If yes then why a and &a is giving same value?
  • It should have differed just like p and &p.
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • _does a creates a separate memory space to store the address of first element of array?_ No, arrays are always in contiguous areas – David Ranieri Apr 03 '21 at 07:39
  • 1
    Don't be shy. Show us your diagram proving your point. Also, `printf` formating char for pointers is `p` not `u`. `printf("p is %p\n", p);` – Chef Gladiator Apr 03 '21 at 07:44
  • Maybe you confuse arrays and jagged arrays: https://en.wikipedia.org/wiki/Jagged_array – David Ranieri Apr 03 '21 at 07:45
  • **Your code is wrong**. Compile it with [GCC](http://gcc.gnu.org/) invoked as `gcc -Wall -Wextra -g`. On my Linux x86-64 desktop, an `int` takes 32 bits or 4 bytes, but a pointer needs 64 bits or 8 bytes! See [this C reference](https://en.cppreference.com/w/c) – Basile Starynkevitch Apr 03 '21 at 07:45
  • 1
    @ChefGladiator `printf("p is %p\n", (void *)p);` a cast is required. – David Ranieri Apr 03 '21 at 07:46
  • I have ran the code in online compiler like ideone. It ran fine without errors. I know that I could have used %p as format specifier but %u is also fine, I guess. – Rahulkumar Jha Apr 03 '21 at 07:49
  • And a good compiler could (sometimes) [optimize](https://en.wikipedia.org/wiki/Optimizing_compiler) your code. A code can run without errors and have [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior). Read a good C standard like [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) or better. So the fact that your code sometimes run does not mean anything. At last, some pointers are not in memory, but just in some [processor register](https://en.wikipedia.org/wiki/Processor_register) – Basile Starynkevitch Apr 03 '21 at 07:50
  • 1
    a can be seen as a pointer to a[0] (ie an int) and &a can be seen as a pointer to an array of 5 int. You can find answer to your question here : https://www.geeksforgeeks.org/whats-difference-between-array-and-array-for-int-array5/amp/ – Ptit Xav Apr 03 '21 at 07:53
  • @DavidRanieri oh? I did not know that. [My code shows](https://godbolt.org/z/7ea14r3qE) no difference? – Chef Gladiator Apr 03 '21 at 07:57
  • @RahulkumarJha It seems to me [it is `%p`](https://en.cppreference.com/w/c/io/fprintf) :) – Chef Gladiator Apr 03 '21 at 08:01
  • @ChefGladiator https://stackoverflow.com/q/9053658/1606345 – David Ranieri Apr 03 '21 at 08:02

1 Answers1

1

No, a does not create a separate memory to store the address of array. &a gives address of array and a gives address of a[0]. Both &a and a are same a because the address of the first element is same as address of the array. int a[5] does precisely what it looks like , generates array of 5 elements. When you create p , you create a separate variable which has a value that &a[0]. You may look into this for more clarity. You should also look at this