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.

5
  • 1
    Throw. The book. Away. Commented Aug 22, 2014 at 11:27
  • Ah, I had a mistake. The "pointer constant" in the first sentence have to be changed to "constant pointer". so the answers will be changed? Commented Aug 22, 2014 at 12:00
  • You should continue throwing the book away. Use it to practice your pitch or your hike, or make paper mache Halloween masks from it. Commented Aug 22, 2014 at 12:06
  • So could you recommend some books for me? Commented Aug 22, 2014 at 12:24
  • Have a look on our recommended book list. Commented Aug 22, 2014 at 13:14

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.