4

So I have to find out why specific values are printed out, and I've solved most of it but, I've got a problem with the last three.

I'd be happy for any help

int main(void)
{
    int myValues[] = { 9, 0, 12345, 1, 7, 2, 6, 3, 5, 4 };
    mess(&myValues[3]); //starts function mess
}

void mess(int *n)
{
    printf("mess :%d\n", *n++); //prints value of 3rd index (1) and sets pointer to fourth index
    printf("mess: %d\n", *++n); //sets n to 5th index and prints its value
    printf("mess: %d\n", -2[n]); //value: -3
    printf("mess: %d\n", (-2)[n]); //value: 1
    printf("mess: %d\n", n[-6]); //value: 32766
}

I just don't understand how the values -3, 1 and 32766 come to be

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

3 Answers3

4
printf("mess: %d\n", -2[n]); //value: -3

-2[n] is -(n[2]) (see here for an explanation on this quirk). At this point, n[2] gets you 3 so -n[2] is -3.

printf("mess: %d\n", (-2)[n]); //value: 1

This is [-2], which means 2 to the "left" of where you started out, which result in 1.

printf("mess: %d\n", n[-6]); //value: 32766

This goes to before the start of your array, and that's undefined behavior. It could do anything, but most likely it just prints some rubbish value by interpreting memory that it shouldn't access this way.

I'm not sure about how well defined the other statements of the code are. It's really bad practice, please don't write code like this. As you aptly put, it's a mess.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • well it's not my code, just some code from our prof. to see how deep our theoretical understanding to this subject is.... But still thanks your answer helps a lot! – Markus Fink Jan 04 '19 at 15:10
4

First, let's visualize the memory pointed to by n, after the execution of first two printf() statements:

int myValues[] = { 9, 0, 12345, 1, 7, 2, 6, 3, 5, 4 };
                                      ^
                                   // n

Let's see one by one

  • Statement 1: printf("mess: %d\n", -2[n]); //value: -3

    Check the operator precedence. -2[n] is parsed as -(2[n]). Thus, the - is the sign, 2[n] is the same as n[2] which is the value 3. Thus, the statement is the same as

    printf("mess: %d\n", -(n[2]) );       
    

    Visualization:

     int myValues[] = { 9, 0, 12345, 1, 7, 2, 6, 3, 5, 4 };
                                           ^     ^^
                                        // n     n+2
    
  • Statement 2: printf("mess: %d\n", (-2)[n]); //value: 1

    Here, n[-2] is the same as *(n-2). Result is the value at that index. (Check the above visualization).

    Visualization:

     int myValues[] = { 9, 0, 12345, 1, 7, 2, 6, 3, 5, 4 };
                                     ^     ^    ^
                          //        n-2    n   n+2
    
  • Finally, Statement 3: printf("mess: %d\n", n[-6]); //value: 32766

    As per the current content of the pointer n, the least accessible index is -5, attempting to access the memory location at an index -6 is accessing out of bounds, causes undefined behavior. The result cannot be justified.

    Visualization:

     int myValues[] =      { 9, 0, 12345, 1, 7, 2, 6, 3, 5, 4 };
                      ???    ^                  ^
           //         n-6   n-5                 n
    
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
3

First, remember that in C, array indexing is commutative - a[i] and i[a] yield the same result.

So, the line

printf("mess: %d\n", -2[n]); //value: -3

is equivalent to writing

printf( "mess: %d\n", -n[2] );

The postfix [] operator has higher precedence than the unary - operator, so the expression -2[n] is parsed as -(2[n]). You're indexing 2 elements from n (3), and negating the result.

In the following line,

printf("mess: %d\n", (-2)[n]); //value: 1

the expression (-2)[n] is equivalent to n[-2] - you're indexing 2 elements before n, which gives you 1.

In the line

printf("mess: %d\n", n[-6]); //value: 32766

you're trying to index 6 elements before n; unfortunately, that's outside the bounds of your array. At this point the behavior is undefined. You could get garbage output, your code code crash, or something else could happen.

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