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

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'.

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)

issue with the conditional operator [duplicate]

This question already has answers here:
C conditional operator ('?') with empty second parameter [duplicate]
(6 answers)
Closed 9 years ago.
#include<stdio.h>
int main()
{
printf("%d\n", 4 ?: 8);
}
According to the C standard this program is invalid because it is missing an expression between the ? and :.But The interesting thing is that there is when I compile the code it is printing 4.how come it will print 4 rather than showing any compile error
This is a gcc extension.
x ? : y
is equivalent to
x ? x : y
See here for detail.

C if condition difference [duplicate]

This question already has answers here:
What is the difference between NULL, '\0' and 0?
(11 answers)
is NULL/false in C and C++ anything more than 0x0 / 0b0 / '\0' / 0 [duplicate]
(6 answers)
Closed 9 years ago.
While I was coding a program in C, I came up a question that I could not figure out. I was checking if a condition is met in an if statement but was wondering if there is any difference between the following:
if(ptr != NULL)
or
if(ptr)
To me, I feel like both of those are correct but in the C world, the second one would be used more and in the Java world, the first one is used more. Is one more correct then the other?
In C, anything that evaluates to 0 (zero) is "false", and anything non-zero is "true".
Thus when ptr is NULL, those two if conditions end up working the same way:
if (ptr != NULL) = if (0 != 0) = if (0)
and:
if (ptr) = if (0)
You'll get people debating which is better, but you'll see both in code. The first is more clear because it's more explicit. The second is shorter. Both are technically correct and equivalent.
Both are correct and equivalent.
A pointer alone evaluates to false if the pointer is NULL and to true otherwise.

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

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/

Resources