Is chars[4] and 4[chars] the same in C? Why? [duplicate] - c

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/

Related

What if array name is put in square brackets? [duplicate]

This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 1 year ago.
I saw such a piece of C code:
int main()
{
static int a[] = {7,8,9};
printf("%d", 2[a] + a[2]);;
return 0;
}
What does 2[a] mean here?
a[b] and b[a] are 100% equivalent in C. What you have there is a very unidiomatic way of writing a[2].
By way of more complete explanation, array subscript notation a[b] is also 100% equivalent to *(a + b), which may make the reason it works both ways clearer.

Type of a variable in language C [duplicate]

This question already has answers here:
typeof operator in C
(5 answers)
Closed 5 years ago.
How to know the type of a variable in C ?
I tried sizeof() but i'm not convinced by the answer and i don't know how to use typeof() in C.
Some help ?
If we have for example :
int i ;
then what is the type of ('A' + i). i though i can do something like
printf("%s", typeof('A' + i));
to display the right type of my expression but i don't kwon how work a gcc expression.
Help please !
Introspection/reflection and is not supported in C language. However, there is a typeof extension in GCC (not part of ANSI)

Is there any function in C similar to LIKE in SQL to compare 2 strings? [duplicate]

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?

not able to understand how compiler(gcc) is interpreting the command(c) and giving output of the statement [duplicate]

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.

5["abcdef"]; is it correct in c? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]
Is this instruction correct in c :
5["abcdef"]
If yes, what does it mean ?
I had this question in a c test.
Yes, it is correct, and means the same as "abcdef"[5], which evaluates to 'f'.
It is because a[b] == *(a+b) == *(b+a) == b[a] by definition.
Yes.
The [] operator is commutative; that's the same as "abcdef"[5], which returns 'f'.

Resources