2

When I print ar and &ar[0][0] as pointers, I see the same value. so I'm expecting them to be interchangeable, However, that is not the case:

enter image description here

int main(void)
{
    #define rowsCount 4
    #define colsCount 3
    int ar[rowsCount][colsCount] =
    {
        {0, 1, 2},
        {3, 4, 5},
        {6, 7, 8},
        {9, 10, 11},
    };
    int rowIndex = 1;
    int colIndex = 5;


    printf("TEST: %p %p\n", ar, &ar[0][0]);
    printf("%d\n", ar[rowIndex][colIndex]);
    printf("%d\n", *(&ar[0][0] + rowIndex * colsCount + colIndex));
    printf("%d\n", *(ar + rowIndex * colsCount + colIndex));

    return 0;
}
Youssef13
  • 3,836
  • 3
  • 24
  • 41
  • The compiler generates a warning for the fourth `printf` "C4477: 'printf' : format string '%d' requires an argument of type 'int', but variadic argument 1 has type 'int *' " – Weather Vane Nov 13 '19 at 10:16
  • You can change the last print to `printf("%d\n", *((int*)ar + rowIndex * colsCount + colIndex));` – klutt Nov 13 '19 at 10:16
  • Also relevant : [Difference between array and &array\[0\]](https://stackoverflow.com/questions/33035068/difference-between-array-and-array0). – Sander De Dycker Nov 13 '19 at 10:16
  • @WeatherVane, I'm working on codeblocks with GNU GCC and I have 0 warnings in it. Not sure what compiler/IDE are you using. Also an explanation is preferred. – Youssef13 Nov 13 '19 at 10:19
  • @klutt, thanks for the fix. but in case of saying just ar, what is the type of the pointer if it's not `int *` ? – Youssef13 Nov 13 '19 at 10:19
  • `ar` is not a pointer. It's an array. – klutt Nov 13 '19 at 10:22
  • @SanderDeDycker, I don't think the linked question answers mine. – Youssef13 Nov 13 '19 at 10:23
  • @klutt, don't arrays decay to pointers ? – Youssef13 Nov 13 '19 at 10:23
  • MSVC compiled from a command line, no IDE. There should be a setting for you somewhere, or an option. The `ar` is a different type from `&ar[0][0]`, and so the pointer arithmetic works differently. `sizeof ar` is `48` so `ar + 1` would add `48` to the address. – Weather Vane Nov 13 '19 at 10:24
  • @Youssef13 Yes, they sometimes do. – klutt Nov 13 '19 at 10:25
  • @Youssef13 : the answers to the linked question explain that while the memory address is the same, the type is different for both expressions. This in turn means that `sizeof` and pointer arithmetic will work differently for them. That should answer your question of why they're not interchangeable - if it doesn't, you might want to clarify your question. – Sander De Dycker Nov 13 '19 at 10:27
  • @WeatherVane, I printed `ar` and `ar + 1`, the difference was 12 only not 48. so it seems adding 1 jumps to the next row instead of the next element. Am I right ? – Youssef13 Nov 13 '19 at 10:33
  • @SanderDeDycker, Thanks for clarification. I understood that arithmetic is different now. but is what I'm saying in the previous comment correct ? – Youssef13 Nov 13 '19 at 10:36
  • @Youssef13 In this case, it decays to a pointer of type `int (*)[3]`. – klutt Nov 13 '19 at 10:38
  • @klutt, Thanks! this really clarified much of the thing to me. so that clarifies why even *(ar + 1) doesn't print the element at ar[1][0] – Youssef13 Nov 13 '19 at 10:45
  • It also clarifies the warning stated by @WeatherVane. – Youssef13 Nov 13 '19 at 10:45
  • @Youssef13 What it clarifies is why you ALWAYS should enable warnings when the code is not behaving the way you want. – klutt Nov 13 '19 at 11:14
  • @klutt, I've left codeblocks with its default settings. and I sometimes get warnings, but not sure why not all warnings. is there a specific compiler flag I should enable ? – Youssef13 Nov 13 '19 at 11:16
  • `-Wall -Wextra` is a good start. You could also add `-pedantic`. It can also be a good thing to specify your desired C version (usually a modern one unless you have reasons to do otherwise) with `-std=c18` – klutt Nov 13 '19 at 11:19
  • @klutt, They were disabled, but even after enabling them I still don't get warnings for it. – Youssef13 Nov 13 '19 at 11:22
  • @Youssef13 Then it's a bug or you're doing something wrong. That's something you could post a question about. :) – klutt Nov 13 '19 at 11:25

0 Answers0