0

So I came across this question sometime ago and couldn't figure out the question for it, but it left me wondering as to why it works. I don't have the question in front of me, but I'll try my best to repeat it from memory.

Why are the outputs of these two pieces of code the exact same in C? One of them is obviously wrong.

Code 1:

int main()
{
   int a;
   int b[] = {1,2,3,4};
   for(a = 0; a<4; a++)
      printf("%i\n",b[a]);
   return;
}

Code 2:

int main()
{
   int a;
   int b[] = {1,2,3,4};
   for(a = 0; a<4; a++)
      printf("%i\n",a[b]);
   return;
}

The output of both pieces of code is:

1
2
3
4

Someone told me it has something to do with a stack and memory locations, but the explanation wasn't clear enough. Can anyone help explain this more clearly?

ericbn
  • 10,163
  • 3
  • 47
  • 55
Mo2
  • 1,090
  • 4
  • 13
  • 26
  • 3
    [C array arithmetic](http://stackoverflow.com/questions/381542/with-c-arrays-why-is-it-the-case-that-a5-5a) – pablo1977 Sep 07 '14 at 01:37
  • The second one is a syntax error. Even if I imagine double quotes around the format string. You cannot subscript a variable of type `int` like that. – 5gon12eder Sep 07 '14 at 01:37
  • These codes won't compile. Edited. – ericbn Sep 07 '14 at 01:38
  • Because in the weird mind of C `a[b]` is the same as `b[a]`. – Hot Licks Sep 07 '14 at 01:40
  • @pablo1977 thank you. I knew it had to have been answered before, but I wasn't sure what to search for. – Mo2 Sep 07 '14 at 01:40
  • @5gon12eder what would the correct syntax be then? – Mo2 Sep 07 '14 at 01:40
  • 1
    Actually, the syntax is correct. The only problem it's the lack of double quotes in printf(). – pablo1977 Sep 07 '14 at 01:41
  • 2
    Neither program is remotely correct. `b` is not an array in either, so the initializer for `b` is out of order. Functions returning an `int` should not contain `return` with no value. The `printf()` statements need double quotes. We'll assume both start with `#include `; if they don't, they should. – Jonathan Leffler Sep 07 '14 at 01:45
  • @JonathanLeffler: You are right. I read wrongly and believed that the `int` array was well declared. But it's wrong too. – pablo1977 Sep 07 '14 at 01:47
  • Even if we do choose to ignore the syntax errors... the array *is* initialized. By the initializer. – Alex Celeste Sep 07 '14 at 01:49
  • 2
    By the time you're looking at this comment, some of the problems in the code may have been fixed by one or more edits (there's an edit pending that fixes the most egregious syntax problems). The only difference between the programs is the use of `b[a]` vs `a[b]` in the `printf()` statement. That is covered by the duplicate question. – Jonathan Leffler Sep 07 '14 at 01:49
  • @JonathanLeffler yup, got it. All I needed was that link. Wasn't sure what to search for. This question can be closed now. – Mo2 Sep 07 '14 at 01:51
  • 1
    @Mo2 Sorry, my first comment was not very good. As @JonathanLeffler has meanwhile answered, the problem is the declaration of `b` as an `int` (rather than an `int[]`). Once that is fixed, the behavior is as explained by other answers. If compiling your code, my compiler only gave me a warning about the bogus initializer but an error on the line where that `int` is subscripted using *another* `int`. – 5gon12eder Sep 07 '14 at 01:52
  • -1: As Jonathan says, there are several errors in the code, giving a very bad question for SO. – pablo1977 Sep 07 '14 at 01:53
  • 5gon12eder Thank you. @pablo1977 I guess the question had syntax errors when it was presented then. It definitely did not have the array brackets when instantiated. – Mo2 Sep 07 '14 at 01:56
  • @Mo2: Please read all the details that Jonathan Leffler has pointed out in his comment. There are a lot of mistakes. – pablo1977 Sep 07 '14 at 01:59
  • @pablo1977 already did. – Mo2 Sep 07 '14 at 02:08

1 Answers1

3

It is simple pointer equivalence in C:

b[a] = *(b + a) = *(a + b) = a[b]

Here is a useful link that offers a more in depth explanation [pointer tutorial]

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85