1

I read this on the internet that an array variable points to the first element of an array. The example of the code is given below :

#include <stdio.h>
int main()
{
    int i,a[10];
    for ( i = 0; i <= 9; ++i )
    {
        printf ("The address of the array element %d is = %p\n",i+1, &a[i]);
    }
    
    printf ("The address of the &a is = %p\n", &a); 
    
    printf ("The address of &a[0] is = %p\n", &a[0] );
    
    printf ("The address of a is = %p", a);
    
    return 0;
}

according to the internet source : The array variable a and the array element a[0] both have the same address because the array variable name a points to the first element of the array, i.e. a[0]. My question is :

  1. Is the array variable a a pointer, since it's pointing to the first element of the array?
    • If the array variable a is a pointer, then shouldn't the address of the pointer a and the variable a[0] (to which the pointer a is pointing) differ? (here we get the same address)

    • If the array variable a is not a pointer then how shall we explain the code given below?

a[0] = *a
a[1] = *a+1
a[2] = *a+2
a[3] = *a+3
a[4] = *a+4
a[5] = *a+5

I suppose this is a very long question but please, do tell me the answer as I have wrecked my mind over this and now I want to beat my head against the wall. Also I am new to stack overflow.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Array is *not* a pointer, but it is *decaying* to a pointer in certain circumstances. As for your examples, it is just a syntactic sugar defining that `a[x]` is the same as `*(a+x)`, regardless of the `a` being array or a pointer. Moreover, one can write `x[a]` while `x` is integer and `a` is array or pointer, and it will be the same as `a[x]`. – Eugene Sh. Dec 07 '21 at 17:23
  • Good question, but it's been asked before. Here's one potential duplicate: [Is an array name a pointer?](https://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer) – Caleb Dec 07 '21 at 17:26
  • Basic C questions have been answered many times here. Use the search bar - it will save you time spent writing this question – 0___________ Dec 07 '21 at 17:31
  • The relationship between arrays and pointers is one of the most fundamental and most important things to learn in all of C. Unfortunately it can also be one of the most confusing, and hardest to learn. Statements like "an array is a pointer to its first element", though well-intentioned and popular, are unfortunately perfectly disastrous in terms of actually educating new learners. Whatever page it was you "read on the internet", close that window and delete that link and try to forget what you read there. – Steve Summit Dec 07 '21 at 17:32
  • Read this instead: An array is an array, and a pointer is a pointer. But when you use an array in an expression, like `f(array)` or `x = array + 1`, where you need the array's value, what you get is not the whole array, what you get is a pointer to the array's first element. That new pointer is not "the array"; that new pointer is something that squirted out when you tried to take the array's value. – Steve Summit Dec 07 '21 at 17:34
  • The key concept here is that in C "array" is not an actual memory-occupying entity. 0-size array takes 0 bytes in memory. Memory-wise, array is merely a bunch of elements that sit next to each other. "Array" is a way of thinking of a particular memory area, not particular data stored in this memory. So in this regard, the simplification "array is just an address of it's first item" works. But not the other way around. – Agent_L Dec 07 '21 at 17:36
  • Another key concept is that in C all the metadata (that is the type of data) are stored in the type of your pointer. Data in memory has no knowledge of it's type whatsoever. The content of a pointer is always same - numerical memory address. But it is the type of the pointer that makes a difference between `int*` and `float*`. So, `int[10]` and `int*` can have same numerical value, but one carries information that there are 9 more after the target and the other doesn't. – Agent_L Dec 07 '21 at 17:37

1 Answers1

-2

Arrays are not pointers. To be sure run this simple program

#include <stdio.h>

int main( void )
{
    int a[10];

    printf( "sizeof( a ) = %zu\n", sizeof( a ) );
    printf( "sizeof( &a[0] ) = %zu\n", sizeof( &a[0] ) );
}

Its output might look like

sizeof( a ) = 40
sizeof( &a[0] ) = 8

But array designators used in expressions with rare exceptions are indeed implicitly converted to pointers to their first elements.

From the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

As for your question

If the array variable a is not a pointer then how shall we explain the code given below?

a[0] = *a
a[1] = *a+1
a[2] = *a+2
a[3] = *a+3
a[4] = *a+4
a[5] = *a+5

then after reading the provided quote from the C Standard it is clear that the array designator a in this expression *a is converted to pointer to its first element. That is you have in fact *( &a[0] ) or just a[0]. And the above records are equivalent to

a[0] = a[0]
a[1] = a[0]+1
a[2] = a[0]+2
a[3] = a[0]+3
a[4] = a[0]+4
a[5] = a[0]+5

As for your question about addresses then the expression &a and &a[0] have the same value because it is the value of the address of the first byte occupied by the array. However types of the expressions are different. The expression &a has the type int ( * )[10] and the expression &a[0] has the type int *.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    Judging from the question, OP has just started learning C. C standard uses language which definitely will only cause even more confusion. BTW it one of the most common duplicates – 0___________ Dec 07 '21 at 17:33
  • That's not what OP asked about. – Agent_L Dec 07 '21 at 17:36