-1

EDIT: Thanks for the link to the previous StackExchange question, the answer is that a[b] is defined as *(a+b), such that "a"[0] = 0["a"] = *("a"+0). I guess this works because "a" is a pointer? So I can't say:

int i;
i[0];

But I can say:

int i;
i["a"];

I'm trying to understand how in the heck this code compiles and what is happening. Any hints would be greatly appreciated. Also, if you know of a good reference for understanding what's really going on in the C language feel free to drop it on me.

Code is here:

int i;
main()
{
int j;
int k;
j=i["a"];  //printf says j is 97!
k=i["b"];  //printf says k is 98!
}

So... what the heck is going on?! I declared "i" as an integer and then I treat it as an array with no compilation error/warning and I enter a string as the argument to the array and still no problems? Anyone have any ideas? Thanks in advance.

hft
  • 1,245
  • 10
  • 29

2 Answers2

3

Characters can be implicitly converted to integers.

The number you see corresponds to the bits used in the character encoding. In this case it looks like the characters are encoded in ascii which gives these particular decimals. http://en.wikipedia.org/wiki/ASCII If you look up the codes for a and b in there you will see the corresponding decimal representation: a=97 b=98.

When you write int i outside of main it gets initialized to 0 (I'm not entirely sure if the C standard mandates that, I'm just looking that up now, but in any case many compilers do this). Then when you write i["a"] this is the equivalent of 0["a"] which is in turn *(0 + "a") which is "a"[0]. When you define something like "a" in your code you are getting a string literal so that means it is a pointer to char. So "a"[0] is just the first character of the char array "a" which is just the character a. If the character 'a' is being represented by 97 then you get 97 as the result when you take the integer representation.

Community
  • 1
  • 1
shuttle87
  • 15,466
  • 11
  • 77
  • 106
3

Yep, this is actually valid C, as Michael mentioned in the comments. array[1] is equivalent to *(array + 1) which is equivalent to 1[array].

In your case, i["a"] is equivalent to 0["a"] which is equivalent to "a"[0] which is "a". Of course, 97 is ASCII for 'lowercase a'.

More on C's index operator here: http://boredzo.org/pointers/#indexing

By the way, if you're using gcc or clang, you can compile your code with -Wall -Wextra -pedantic to see all compiler warnings (rather more than you'd bargain for, I think).

Wander Nauta
  • 18,832
  • 1
  • 45
  • 62
  • Should probably be noted that this depends on the value of "i" being NULL. This can't always be assumed safely. Just works out in this case. – Happington Jun 02 '14 at 19:06
  • `int i` is defined in the global scope, I believe that means it's always 0 though I'll have to check. – Wander Nauta Jun 02 '14 at 19:07
  • Static variables are indeed always initialized to 0, per the C standard. See http://stackoverflow.com/a/14049791/182402 – Wander Nauta Jun 02 '14 at 19:09
  • Ah, of course, I forgot that globals are implicitly declared as static. That wouldn't be guaranteed if that i was inside the function scope, only because it's global. Sorry! – Happington Jun 02 '14 at 19:10
  • Reading your comment again, that's probably exactly what you meant by 'in this case'. Sorry... – Wander Nauta Jun 02 '14 at 19:10