18

I am a teaching assistant for a C programming course, and I came across the following line of C code:

char str[] = "My cat's name is Wiggles.";
printf("%c %c %c %c\n", str[5], *(str + 5), *(5 + str), 5[str]);

I never came across the very last argument (5[str]) before, and neither did my professor. I don't think it's mentioned in K&R and C Primer Plus. I found this piece of code in a set of technical interview questions. Does anyone know why C allows you to access an array element that way also? I never heard of an index being outside the set of brackets and the name of an array inside the brackets.

Your help will be greatly appreciated!

2
  • 7
    This is a beaten-to-death FAQ. c-faq.com/aryptr/joke.html. Actually, the example you posted in your question contains the answer already. Commented Apr 5, 2011 at 1:14
  • 5
    Yes, array indexing is commutative. Rarely seen outside of IOCCC entries. If seen in production code, shoot the author on sight. Commented Apr 5, 2011 at 2:19

8 Answers 8

16

Perfectly valid C. From Wikipedia:

Similarly, since the expression a[i] is semantically equivalent to *(a+i), which in turn is equivalent to *(i+a), the expression can also be written as i[a] (although this form is rarely used).

Wacky, but valid.

Sign up to request clarification or add additional context in comments.

Comments

6

str[5] directly translates to *(str + 5), and 5[str] directly translates to *(5 + str). Same thing =)

Comments

4

It's basically just the way C works. str[5] is really equivelent to *(str + 5). Since str + 5 and 5 + str are the same, this means that you can also do *(5 + str), or 5[str].

It helps if you don't think of "5" as an index, but rather just that addition in C is commutative.

Comments

2

Similarly, since the expression a[i] is semantically equivalent to *(a+i), which in turn is equivalent to *(i+a), the expression can also be written as i[a] (although this form is rarely used).

http://en.wikipedia.org/wiki/C_syntax#Accessing_elements

Comments

2

Its all same. *ptr or ptr[0] actually means *(ptr+0). So whenever you write *ptr or ptr[0] it goes as *(ptr+0). Let say you want value at ptr[4] so it means you can also write it as *(ptr+4). Now whether you write it as *(ptr+4) or *(4+ptr), it's same. so just for understading if you can write *(ptr+4) as ptr[4] same way *(4+ptr) is same as 4[ptr]. Please go through http://en.wikipedia.org/wiki/C_syntax#Accessing_elements for more details.

Comments

2

if str is an array of type char, then we can access any index say i as below-

  1. str[i]
  2. *(str + i)
  3. char *p = str, then access the index i as p[i] or *(p+i)

Comments

1

It's a funky syntax for sure, but...

str[5] would mean *(str+5)

And

5[str] would mean *(5+str)

Comments

0

Example Code.

#include<stdio.h>
int main(){
    int arr[] = {1, 2, 3};
    for(int i = 0; i <= 2; i++){
        printf("%d\t", i[arr]);
    }
    return 0;
}

Output:1 2 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.