What does the line starting with a double quote in C mean?

I was asked in an interview what the next line in C does? In my opinion, the following line does not matter:

"a"[3<<1];

Does anyone know the answer?

+5
source share
5 answers

Surprisingly, this makes sense: it is indexing into an array of characters representing a string literal. By the way, this particular index has a value 6that goes beyond the literal and therefore the behavior is undefined.

You can build an expression that works on the same basic pattern:

char c = "quick brown fox"[3 << 1];

will have the same effect as

char c = 'b';
+13
source

Think about it:

"Hello world"[0] 

there is 'H'

"Hello world" - . char . "Hello world"[0] .

+3

. : a[b] , *(a+b). ( , .)

+3

"a" - , 'a' 0. 3 << 1 - 3*2 = 6, 7- 2 . undefined.

( , , undefined , .)

+2

"some_string" [i] i- . 3<<1 6. , "a"[3<<1] 6- "a".

In other words, the code invokes undefined behavior (and therefore, in a sense, doesn’t really make sense), since it accesses the char array outside the bounds.

+1
source

All Articles