0

The book that I have says that An array name is a pointer constant. So, I tried this :

int A[3][4] = {0};
A[0][0] = 1;
A[1][0] = 2;
A[2][0] = 3;
printf("A : %x\n", A);
printf("*A : %x\n", *A);

I expected the result of first printf is the address of A and the other is 1. because, I thought array name is a pointer constant and the result would be *(address of A). but, the results have same value; address of A. do you know why? please give me some advice.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294

4 Answers4

1

First, arrays are not pointers! Your book is wrong. Arrays and pointers share some operations, and array name can be automatically converted to a pointer to its first element in some cases, but remember they are different.

Second, in your code, A is a 2-dimensional array, i.e, an array of arrays. so *A, which is the same as A[0], is its first subarray. To access A[0][0], you need **A.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
1

Arrays are not pointers. Except when it is the operand of the sizeof or unary & operators, or is a string literal used to initialize another array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and the value of the expression will be the address of the first element of the array.

Given the declaration of A, all of the following are true:

Expression        Type            Decays to        Value
----------        ----            ---------        -----
         A        int [3][4]      int (*)[4]       &A[0][0]
        *A        int [4]         int *            &A[0][0]
        &A        int (*)[3][4]   n/a              &A[0][0]
      A[i]        int [4]         int *            &A[i][0]
     *A[i]        int             n/a              A[i][0]
     &A[i]        int (*)[4]      n/a              &A[i][0]

A decays to an expression of type "pointer to 4-element array of int". A[i] decays to an expression of type "pointer to int. The address of the first element of the array is the same as the address of the array itself, so the expressions

A
*A
&A
A[0]
&A[0]
&A[0][0]

all evaluate to the same value, but the types of the expressions will be different.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

Array and pointers are not same. The main difference is you cannot perform the pointer increment or decrement on the array name. You can think of array as constant pointer. If you want to print the element of an array then try **A or *(A + i) in loop where i is the loop index

Adarsh
  • 883
  • 7
  • 18
0

Don't confuse with array's and pointers! Both are different things! and also array is not a pointer! But array name represents base address of array!

In your code-

int A[3][4];

It is a 2D array so you need to dererence two times. If you dererence one time it will fetch the address of the array only. A, A[0], *A, &A, &A[0] all will represent the starting address of it.

Try -

printf("%d\n",**A);

or

printf("%d\n",A[0][0]);

or

printf("%d\n",*A[0]);
Sathish
  • 3,740
  • 1
  • 17
  • 28