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]
Related
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;
This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 7 years ago.
why can't we compare two strings in c program directly.For example i have tried the following example
char *str="int";
if(str=="int")
printf("yes");
else
printf("no");
For the above I got output as "no"
I have tried the above code by using the same logic as if for integers
ie
int i=10;
if(i==10)
printf("same");
But when I have modified the above code like the following
if((strcmp(str,"int"))==0)
printf("yes");
I got the output as "yes"
What is the problem in the first stated code?
A "string" in C is just an array of chars. Comparing two arrays with == just compares their addresses, which are different for different arrays. (Literals may or may not be the same, actually, depending on the implementation.)
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:
Accessing arrays by index[array] in C and C++
(2 answers)
Closed 4 years ago.
#include<stdio.h>
main()
{
printf("%c\n",1-3+2["nexus"]);
}
The result is v. How does it turn out?
What does the indentation (square ones) do?
2["nexus"] is probably leading us to 3 element of the nexus considering it as an
array of characters[arr[2]=x in our case]
following is 1-3viz -2 added to x character ie added to to ascii value
and the corresponding element is v
cant exactly figure out what the square identation really mean but according to the oput this is possible way .
2["nexus"] is same as "nexus"[2], which correspond to the 2nd index or the 3rd element of this string. ( Which is 'x' here ).
In equation, 1-3+ 2["nexux"] is like 1-3+'x' that represent 'v'.
Note
When a string is declared like "nexus".
nexus[0] is 'n'
nexus[1] is 'e'
nexus[2] is 'x'
nexus[3] is 'u'
nexus[4] is 's'
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 9 years ago.
I am using Ubuntu 12.04lts with the GCC compiler. This program gives the result 10. Could you anybody please describe why this program gives the result like this?
#include <stdio.h>
void main(void)
{
int arr[1] = {10};
printf("\n%d\n\n", 0[arr]);
}
arr[0] gets internally expanded to *(arr+0). Similarly 0[arr] gets expanded to *(0+arr) which points to the same thing. Hence you see 10.
In general for an array or a pointer a, a[b] always means *(a+b) where a is the starting address of the array or pointer and b is the offset. Thus, a[b] and b[a] are equivalent.
below line means arr is int type array and it has size 1 and it is initialized with 10 ie index 0 has 10
int arr[1] = {10};
then next line printf statement printing the value of arr at index 0.
printf("\n%d\n\n",0[arr]);