Difference between the address of the variable that points to an array and the value of that variable itself

StackOverflow https://stackoverflow.com//questions/25081368

Question

I have an array

int ar[5] = {1,2,3,4,5};
printf("%d",(ar==&ar));

The print statement returns true. But what if I do ar+1 and &ar+1. Where does that point to?

Also if I have

char *c[] = {"GeksQuiz", "MCQ", "TEST", "QUIZ"};
char **cp[] = {c+3, c+2, c+1, c};
char ***cpp = cp;

where does cpp+1 point to? Is it after cp i.e. cp[4] or does it point to cp[1]?

Was it helpful?

Solution

The address of an array corresponds to the location of the array object in memory, and has type "pointer to array". This location has the same value as the address of the first element of the array. The address of the first element of the array is what an array name will decay into when used in an expression. This explains why ar == &ar.

Now, as ar decays to the address of the first element, the ar + 1 is the address of the second element of the array. However, the value of &ar has type pointer to an array, and so &ar + 1 actually points after the ar array itself, which would be address of the object just past its last element (or &ar[5]).

Your c, cp, and cpp arrays can be pictorially represented like this:

                      "GeksQuiz"      "MCQ"    "TEST"       "QUIZ"
                      ^               ^        ^            ^
               c:     |               |        |            |
               +---+  |               |        |            |
     +-------->| *----+               |        |            |
     |         +---+                  |        |            |
     | +------>| *--------------------+        |            |
     | |       +---+                           |            |
     | | +---->| *-----------------------------+            |
     | | |     +---+                                        |
     | | | +-->| *------------------------------------------+
     | | | |   +---+
     | | | |
     | | | |   cp:
     | | | |   +---+
     | | | +-----* |<--+
     | | |     +---+   |
     | | +-------* |   |
     | |       +---+   |
     | +---------* |   |
     |         +---+   |
     +-----------* |   |
               +---+   |
                       |
                       |
               +---+   |
           cpp:| *-----+
               +---+

Since cpp is a pointer variable, cpp+1 is 1 greater than the value of that variable. The cpp variable contains the decayed value of cp (the address of its first element). So cpp+1 is the same as cp+1, which would correspond to &cp[1], or the address of the second element of cp.

OTHER TIPS

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

The expression ar has type "5-element array of int"; if it's not the operand of the & operator, it will be converted to type "pointer to int", and its value will be the address of the first element of the array.

In the expression &ar, ar is the operand of the unary & operator, so the conversion doesn't happen; in this case, the type of the expression is "pointer to 5-element array of int".

Both ar and &ar have the same value (the address of the first element of the array is the same as the address of the whole array), but the types are different:

Expression     Type         Decays to
----------     ----         ---------
        ar     int [5]      int *
       &ar     int (*)[5]   n/a

ar + 1 will point to the next int value (it's equivalent to writing &ar[1]). &ar + 1 will point to the next 5-element array of int (that is, it will point to the int value immediately following the last element of the array).

cpp + 1 will point to the next char ** element (equivalent to writing &cp[1]).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top