|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hello!
I was wondering why the following C code compiles: main() { int a[100]; int b= 4[a]; /* Looks like 4[a] is equivallent to a[4] ??? */ return b; } Any explanations/links that would me understand this would be welcome (I coundn't find that in the C standard). Cheers g |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
g36130@googlemail.com wrote:
> Hello! > > I was wondering why the following C code compiles: > > main() > { > int a[100]; > int b= 4[a]; /* Looks like 4[a] is equivallent to a[4] ??? */ > return b; > } > > Any explanations/links that would me understand this would be > welcome (I coundn't find that in the C standard). You could have found it in the C FAQ at http://www.c-faq.com/ It's question 6.11 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Feb 1, 1:04 pm, g36...@googlemail.com wrote:
> Hello! > > I was wondering why the following C code compiles: > > main() > { > int a[100]; > int b= 4[a]; /* Looks like 4[a] is equivallent to a[4] ??? */ > return b; > > } > > Any explanations/links that would me understand this would be > welcome (I coundn't find that in the C standard). You would better wonder why your code shouldn't compile. main has to return int. Evaluating uninitialized objects invokes undefined behavior. As for your question, x[y] is equal to *(x + y), x+y == y+x. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
<g36130@googlemail.com> wrote in message news
> I was wondering why the following C code compiles: > > main() > { > int a[100]; > int b= 4[a]; /* Looks like 4[a] is equivallent to a[4] ??? */ > return b; > } > > Any explanations/links that would me understand this would be > welcome (I coundn't find that in the C standard). > It's a silly quirk of the language. Arrays can be treated as pointers in most contexts, and pointers as arrays. a[4] = means *(a + 4). So it makes sense that 4[a] should mean *(4+a). But to write that in code is rather pointless. -- Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
vippstar wrote:
> On Feb 1, 1:04 pm, g36...@googlemail.com wrote: >> Hello! >> >> I was wondering why the following C code compiles: >> >> main() >> { >> int a[100]; >> int b= 4[a]; /* Looks like 4[a] is equivallent to a[4] ??? */ >> return b; >> >> } > You would better wonder why your code shouldn't compile. > > main has to return int. main() is equivalent to int main() in C90. > Evaluating uninitialized objects invokes undefined behavior. This doesn't mean it shouldn't compile. -- Army1987 (Replace "NOSPAM" with "email") |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
"Malcolm McLean" <regniztar@btinternet.com> writes:
[...] > Arrays can be treated as pointers in most contexts, and pointers as arrays. Um, sort of. Read section 6 of the comp.lang.c FAQ to understand what this really means. -- Keith Thompson (The_Other_Keith) <kst-u@mib.org> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
![]() |
| Outils de la discussion | |
|
|