1
int a[5] = {1,2,3,4,5};                                           
int *ptr=(int *)(&a+1);
printf("%d,%d", *(a+1),*(ptr-1));

the result is 2,5. here is my question: why &a equal to a? a pointer to the first element in the array,so is it right to think &a is get the address of the pointer a? why type of &a is int(*)[5] ? this means &a<=> a[0][5]? if i write this:

int b = 1;
int *cx = &b;
int *dx = (int *)(&cx + 1);
printf("%d  %d", *(cx), *(dx -1));

there will have a question. what is special of the array name in c?

Sathish
  • 3,740
  • 1
  • 17
  • 28
Joiner
  • 81
  • 9
  • 2
    There is no "the pointer a". `a` is an array. `&a` is the address of the array `a`. `&a` does not equal `a` . `*(a+1)` accesses the second element, and `*(ptr-1)` accesses the last element. The difference between `a` and `cx` is that `a` is an array, but `cx` is a pointer. Arrays and pointers are different. – M.M Jul 23 '14 at 05:05
  • i use Xcode execute printf("%p,%p", a, &a); the result is 0xbfffca30,0xbfffca30. a is an array and the first element'address is store in a. so &a is double pointer,is that right? – Joiner Jul 23 '14 at 05:19
  • 1
    no. There is no "double pointer". `a` is not a pointer. Your printf causes undefined behaviour; to use `%p` you must cast a pointer to `(void *)` . `a` and `&a` are equal after this conversion, but not before it. [C FAQ link](http://c-faq.com/aryptr/index.html) – M.M Jul 23 '14 at 05:21
  • http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c – AndersK Jul 23 '14 at 05:23
  • i ever think a is a pointer that store address of the first element.i do not think array is a type said int(*)[5] . it is hard to me to consider the type of two-dimension array. thank you @Matt McNabb – Joiner Jul 23 '14 at 05:38
  • 1
    I'm sure this is a duplicate many times over, but I'm too lazy to track it down. 1. Arrays are not pointers. 2. Array *really* are not pointers. 3. Read section 6 of the [comp.lang.c FAQ](http://www.c.faq-com). 4. Did I mention that arrays are not pointers? – Keith Thompson Jul 23 '14 at 05:42

3 Answers3

1

a[0][5] is not valid. You are assuming that a is a pointer. a is not a pointer. It is an array. It decays to a pointer in expressions.

&a is a constant address value that points to the array a.

user1969104
  • 2,340
  • 14
  • 15
0

Array is a collection of similar datatypes stored in contiguous memory location.

case 1: Normally array name represents the Base Address of the array. According to this a and gives the base address. You Know &a gives The base address

case 2: A pointer to the first element in the array, Gives you the Base address of the array.

int a[5] = {1,2,3,4,5};
int *ptr;
ptr=a;  // Here p holds the address of the first element in the array
ptr=ptr+1; // address incremented to next element. now it will point to 2
printf("%d\n",*ptr); // it prints 2.

case 3: From your code Normally a and &a are starting address. a+1 means in will point to the second element in the array. But &a+1 means it will point to the next array. it will increment the whole array size(that means in your case 20 bytes).

int a[5] = {1,2,3,4,5}; // consider base address is 1000. so &a+1 is 1020. that is next array(but here you did not have).
int *ptr=(int *)(&a+1); // you are assigning 1020 address to ptr. for checking print the address of ptr
printf("%d,%d", *(a+1),*(ptr-1)); // as i told a+1 will point to 2nd element. so it prints 2. 
//ptr is pointing to 1020, you are decreasing one element so it point to 1016. the value at 1016 is 5.
//(how? 1 is 1000 means, 2 is 1004, 3 is 1008..... 5 is 1016(because integer needs 4 bytes of address for each elements)).

Remember in array elements are stored in Contiguous memory locations.

int b = 1; // consider address of b is 1000
int *cx = &b; // assingning to *cx
int *dx = (int *)(&cx + 1); // &cx is pointer address(it is not 1000, so consider &cx is 2000)
// &cx+1 means it points to 2004, and you are assigning to *dx
printf("%d  %d", *(cx), *(dx -1)); // here *(cx) will print 1. but *(dx-1) means it point to 2000. we dont know what value is there. 
// so it will print some garbage value.
Sathish
  • 3,740
  • 1
  • 17
  • 28
  • so what is the type of &cx? if my system is 64bit and the int is 32bit,then(if &cx is 2000) &cx + 1 = 2004 or &cx + 1 = 2008? – Joiner Jul 23 '14 at 06:19
  • @keepmove If int is 32bit means, &cx + 1 = 2004. If int is 16bit means, &cx + 1 = 2002. – Sathish Jul 23 '14 at 06:36
  • i think &cx is double pointer, so it is a pointer type , if my system is 64bit,&cx + 1 = 2008. why i am wrong? – Joiner Jul 23 '14 at 06:57
  • @keepmove `&cx + 1` value is depends on sizeof(int). – Sathish Jul 23 '14 at 07:45
  • but address is not a int. why &cx + 1 value is depends on sizeof(int); – Joiner Jul 23 '14 at 09:12
  • this: `int **p = &cx;` in the 64bit system does the p is 8byte? so &cx +1 => p +1 is add 8? i am wrong again? – Joiner Jul 23 '14 at 09:16
  • @keepmove what system and os you are using?. Try in 32bit system. – Sathish Jul 23 '14 at 09:20
  • sorry, I just want to make a metaphor to express what i think – Joiner Jul 23 '14 at 09:54
  • @keepmove i have explained what i came across while studying. for more info surf! – Sathish Jul 23 '14 at 10:42
  • _if my system is 64bit,&cx + 1 = 2008. why i am wrong?_ - You are not wrong, Sathish is when saying _`&cx + 1` value is depends on sizeof(int)._ Rather, it depends on sizeof cx, i. e. sizeof (int *). – Armali Apr 17 '15 at 08:46
0

First off a is array name which is nothing but a pointer (Address) to the first element of the array, a would contain value of memory location where first element of array is stored.

&a and a will give you same value as it is just a pointer.

Consider you are a class teacher and have gone to museum with students, you have to keep track of 100 students, in the same way there are more teachers with more students in the museum. So after spending a while when you have get them all together. It would be difficult to identify children and pull them together. In the same way if array name does not exist then it would always be difficult to keep track of your elements. Luckily, there is always a mentor boy in class who always keep the students together and if you find him you will find all 100 students :). Same way we have pointer name which stores the address of the first element. Only using that you can get all the elements of array. It means to keep track of 100 elements (Students) I only need to keep one address (array name or mentor name) in my mind rather than all. Hence tracking and accessing becomes really simple.

Sorry to bore you with my story but It could give you a feel as to how special arrays are.

You can run the below code to get a clear idea about how it works in memory level:

int a[5] = {1,2,3,4,10};                                           
int *ptr=(int *)(&a+1);
printf("%d\n",a);
printf("%d\n",&a[0]);
printf("%d\n",&a[1]);
printf("%d\n",&a[2]);
printf("%d\n",&a[3]);
printf("%d\n",&a[4]);
printf("%d\n",(&a+1)); 
printf("%d\n",(ptr-1));
printf("%d,%d", *(a+1),*(ptr-1));

Hope it helps

Waman
  • 1,179
  • 7
  • 16