This question already has answers here:
Strange array initialize expression?
(3 answers)
Closed 6 years ago.
I am reading Bruce Dawson's article on porting Chromium to VC 2015, and he encountered some C code that I don't understand.
The code is:
char c[2] = { [1] = 7 };
Bruce's only comment on it is: "I am not familiar with the array initialization syntax used - I assume it is some C-only construct." So what does this syntax actually mean?
C99 allows you to specify the elements of the array in any order (this appears to be called "Designated Initializers" if you're searching for it). So this construct is assigning 7 to the second element of c.
This expression is equivalent to char c[2] = {0, 7}; which does not save space for such a short initializer but is very helpful for larger sparse arrays.
See this page for more information:
https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html
Its meaning would be
char c[2]={ 0, 7 }
OR you can say
char c[2];
c[0]=0;
c[1]=7;
Related
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed last year.
in the online course from my university I met the next interesting fact:
The executing of the next line will produce '\0':
5["Peklo"]
I also tried some different examples by passing different integers and string literals. I know that C strings are const char * pointers therefore that code is valid and will compile, but I can not figure out how the output calculates/depends on the passing integer value of the string indexer. If someone knows, can you explain to me in detail why 0["P"], 0["A"] and 1["A"] produces different results (80, 65, 0)?
5["Peklo"] === "Peklo"[5] == (char[]){'P','e','k','l','o','0'}[5] == 0 == '\0'
[0] [1] [2] [3] [4] [5]
This question already has answers here:
C Unions output unclear
(2 answers)
Closed 6 years ago.
I was at an interview and the following code was given to me:-
union mix
{
int i;
char c[2];
};
int main()
{
union mix key;
key.i=512;
printf("\n %d,%d,%d",key.i,key.c[0],key.c[1]);
return 0;
}
and when I came home and ran it on the system I got the output:- 512,0,2.
Can anyone explain it to me how it works or why is c[1]=2?
Edit :- all I know is that it is related to bit storage in the memory.
It's a union, meaning i and c occupy the same memory.
512 is 0x200, meaning first byte is zero, second is 2 (each byte takes two hex digits).
This of course depends on the endianness of your CPU.
This question already has answers here:
Regular expressions in C: examples?
(5 answers)
Closed 7 years ago.
I meant, something that we can use this way:
char string1[] = "???, buddy*\0";
char string2[] = "Hey, buddy, hello!\0";
if (like(string1, string2)
puts("strings are similar!");
else
puts("string are different!");
You want to use a regular expression library. See this question for the ANSI library information: Regular expressions in C: examples?
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 8 years ago.
Can anyone help me out, I am not getting how gcc is compiling the below statement and printing its output.:-
printf("%d",7["sunderban"]);
C allows to access array's elements in two ways (see Accessing arrays by index[array] in C and C++ and the answers) :
int v[5];
// 1)
v[2] = 33;
// 2)
2[v] = 44;
So, what happens in your case is that you access the 8th element of the string, and it is interpret as int by the printf.
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 7 years ago.
I've read this and don't believe it :) I've no compiler here to test.
In raw C, the [] notation is just a pointer math helper. Before [], you'd look for the fourth char in the block pointed to by ptr like:
*(ptr+4)
Then, they introduced a shortcut which looked better:
ptr[4]
Which transaltes to the earlier expression. But, if you'd write it like:
4[ptr]
This would translate to:
*(4+ptr)
Which is indeed the same thing.
Because a[b] is exactly the same as *(a+b), and + is commutatitve.
chars[4] is *(chars+4), and 4[chars] is *(4+chars)
http://c-faq.com/aryptr/joke.html
Try this to test compile: http://codepad.org/