Thursday, April 29, 2010

Access a string within quotation by array index

Today, I found something interesting. This is not very important, but, I astonished seeing this at first and was thinking "does it work !!!".

In C, you can access a string withing a quote by array indexes. It will be clear if I give an example.

Say, you have a string "man" you can access it with array index without assigning the string to any character array.

I mean, you don't need to :

char* str[10];
strcpy(str,"man");

you can just access like "man"[0], "man"[1] , "man"[2] etc.

So, just try a little program and see what happens:


int main(){

printf("%c","man"[0]);
printf("%c","man"[1]);
printf("%c\n","man"[2]);

return 0;
}