1

For example let's say you have an array a and a pointer p.Here it is how it goes.

void main() {
    int a[10];
    int *p;
    for(i = 0;i <=10;i++)
        a[i] = (i + 1) * 2;
    p = &a[0];
    printf("%d",a[4]);
    printf("%d",p[4]);
}

How are they equal ?

jianu81
  • 105
  • 1
  • 2
  • 7

6 Answers6

5

How come the first element of an array is equal to the array ?. Lets say you have an integer array like int arr[5]; then according to your question headline

  • first element of an array will be arr[0] which is value of arr[0] and
  • array means arr and arr names represents base address of the array. So arr and arr[0] are not same. arr is base address & arr[0] is value.

For your particular case, integer array a looks like below & all elements of array are stored in consecutive memory location. Assume array base address is 0x100(some memory location)

 a[0]   a[1]  a[2]  a[3]  ........................................ a[9]
  ------------------------------------------------------------------------
 |  2  |  4  |  6  |  8  |  10  |  12  |  14  |  16  |  18  |  20  | 22  |
  ------------------------------------------------------------------------
0x100   0x104  0x108 ..                                         ..    
 a
LSB                                                                   MSB

So here a means 0x100, by assuming base address of a is 0x100. Now when you do

p = &a[0]; /* here you are setting p to point to one of the places in the array a and that is a[0] */ 

here p is pointing to first element of a i.e 0x100 as below

       a[0]   a[1]  a[2]  a[3]  ........................................ a[9]
      ------------------------------------------------------------------------
     |  2  |  4  |  6  |  8  |  10  |  12  |  14  |  16  |  18  |  20  | 22  |
      ------------------------------------------------------------------------
    0x100   0x104  0x108  0x112 0x116..                                         ..    
     a
     |
     p   

Now when you print a[4] it prints 10 which is quite simple as expected & it expands like below

a[4] = *(a + 4) /* here you can say that array name a is converted to a pointer to its first element */
     = *(0x100 + 4*4 ) /* multiplied by 4 ? bcz a is int array & each element size is 4 byte */
     = *(0x116) /* value at 0x116 memory location */
     = 10

And when you print p[4] it expands like below

p[4] = *(p + 4)
     = *(0x100 + 4*4) /*multiplied by 4 because int pointer increments by 4 bytes*/
     = *(0x116) ? /* it prints value at 0x116 location which 10 */
     = 10

Also while assigning values to array elements in the for loop, you are trying to access a[10] which is out of boundary & causes undefined behavior. In the below code block condition part should be i<10 instead of i<=10 as you declared a[10] and array index starts from zero.

for(i = 0;i <=10;i++) {  /* make it i<10 */
        a[i] = (i + 1) * 2;
}

Finally void main() { /* code */ } is bad practice and its not according to C standards specification. Use int main(void) { } instead as specified in C standard n1256 draft.

5.1.2.2.1 Program startup

1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;9) or in some other implementation-defined manner.

Achal
  • 11,821
  • 2
  • 15
  • 37
  • Just a small note: `0x100 + 16` would be `0x110`, and the sequence above should also be `...0x108, 0x10c, 0x110...` – xoudini Jun 02 '18 at 10:46
  • OS gives different address(in hexa decinmal so I took 0x), just to explain Op I took base address of `a` as `0x100`, its not `0001 0000 0000` its just `100` i.e some address. – Achal Jun 02 '18 at 10:49
  • Yes, that wasn’t my point. It’s just that the prefix `0x` signifies hexadecimal notation, so the math in the answer doesn’t quite add up. – xoudini Jun 02 '18 at 10:55
2

Array definition: An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

When you take the address of the first element of the array (&a[0]), you will get the exact same value as a. (however you will lose the size information, as &a[0] is a pointer to the memory, where a is actually the array)

This is because a[0] actually translates to *(a + 0), where a is the pointer to the memory address your array resides at. So &a[0] becomes &(*(a + 0)) or "the address of the content of the address a + 0", which is the same as "the address a"

Similarly, a[4] translates to *(a + 4).

I hope this clarifies things :)

EDIT:

I just found this page, where you can read more about it: https://www.le.ac.uk/users/rjm1/cotter/page_59.htm

EDIT 2: Clarified the difference between &a[0] and a

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
  • 4
    "This is because an array is basically a pointer (but with some size information)." - incorrect definition – Alex Lop. Jun 02 '18 at 09:48
  • 1
    `&a[0]` is not exactly the same as `a`. Just try using both expressions with `sizeof`. – nwellnhof Jun 02 '18 at 09:49
  • @AlexLop.Can you edit the post with the correct definition? I don't know it :) – Quaisaq Anderson Jun 02 '18 at 09:58
  • "however you will lose the size information" - what do you mean by that? – Alex Lop. Jun 02 '18 at 10:03
  • 1
    An array is absolutely not a pointer! A pointer is itself a variable holding the memory address of an object. An array is a memory region large enough to hold a string of objects of same size. The only way to access elements is through the use of the very first element address displaced by the amount of object size multiplied by its position in the array. That's why C in some cases permit to legally use base address as a pointer. – Frankie_C Jun 02 '18 at 10:04
  • ... where `a` is converted to an expression that is a pointer to the first element of `a`. – Antti Haapala -- Слава Україні Jun 02 '18 at 10:09
1

Arrays are stored in contagious memory and &a[0] is a pointer reference to first element. Now if you want to get pointer reference to second that would be (address of first element + sizeof(int)). Now you can access it's value by *(address of first element + sizeof(int). This is called pointer arithmetic. You must refer to a good book to learn more about it.

Ashok Kumar
  • 121
  • 8
0

You assigned pointer ‘p’ to point the local array ‘a’, so from now on ‘p’ and ‘a’ are the same array, every change you make to ‘p’ will influance directly on ‘a’ and vice versa.

p = &a[0];
Dennis Vash
  • 50,196
  • 9
  • 100
  • 118
0

In a[10], a is a pointer which points to the address of first element of the array. So When you assigned:

p = &a[0];

P is also storing the same address at that of a and point to same element. So any manipulation made to p will be reflected in a.

amrender singh
  • 7,949
  • 3
  • 22
  • 28
0

When you write p=&a[0]; is the same as p=a; now p and a are pointers on the beginning of the same array

Mike
  • 45
  • 5