Home > Blockchain >  Meaning of int a[10]; int *p = a 9;
Meaning of int a[10]; int *p = a 9;

Time:05-07

I am currently trying to understand pointers in C but I am having a hard time understanding this code:

int a[10]; 
int *p = a 9;
while ( p > a )
    *p-- = (int)(p-a);

I understand the code to some degree. I can see that an array with 10 integer elements is created then a pointer variable to type int is declared. (But I don't understand what a 9 means: does this change the value of the array?).

It would be very helpful if someone could explain this step by step, since I am new to pointers in C.

CodePudding user response:

When used in an expression1, the name of an array in C, 'decays' to a pointer to its first element. Thus, in the expression a 9, the a is equivalent to an int* variable that has the value of &a[0].

Also, pointer arithmetic works in units of the pointed-to type; so, adding 9 to &a[0] means that you get the address of a[9] – the last element of the array. So, overall, the p = a 9 expression assigns the address of the array's last element to the p pointer (but it does not change anything in that array).

The subsequent while loop, however, does change the values of the array's elements, setting each to the value of its position (the result of the p - a expression) and decrementing the address in p by the size of an int. (Well, that what it's probably intended to do; but, as mentioned in the comments, the use of such "unsequenced operations" – i.e. the use of p-- and p - a in the same statement – is actually undefined behaviour because, in this case, the C Standard does not dictate which of those two expressions should be evaluated first.)

To avoid that undefined behaviour, the code should be written to use an explicit intermediate, like this:

int main()
{
    int a[10];
    int* p = a   9;
    while (p > a) {
        int n = (int)(p - a); // Get the value FIRST ...
        *p-- = n;             // ... only THEN assign it
    }
    return 0;
}

1 There two exceptions: when that array name is used as the operand of a sizeof operator or of the unary & (address of) operator.

CodePudding user response:


int a[10]; 

This declares an array on e.g. the stack. a represents the starting address of the array. The declaration tells the compiler that a will hold 10 integers. C assumes you know what you are doing so it is up to you to keep yourself in that range.


int *p = a 9;

p is declared a pointer e.g. like a RL street address. When you add an offset to a an offset is added to the address a. The compiler converts the offset like 5 to bytes 5*sizeof(int) so you don't need to think about that, so your p pointer is now pointing inside the array at offset 9 - which is the last int in the array a since index starts at 0 in C.


while( p > a )

The condition says that do this while the address of what p is pointing to is larger than the address where a is.


*p-- = (int)(p-a);

here the value what p points to is overwritten with a crude subtraction between current p and starting address a before the pointer p is decremented.

  •  Tags:  
  • c
  • Related