PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Why does this compile?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Why does this compile?

Réponse
 
LinkBack Outils de la discussion
Vieux 01/02/2008, 11h04   #1
g36130@googlemail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Why does this compile?

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
  Réponse avec citation
Vieux 01/02/2008, 11h25   #2
Mark Bluemel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why does this compile?

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
  Réponse avec citation
Vieux 01/02/2008, 12h21   #3
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why does this compile?

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.
  Réponse avec citation
Vieux 01/02/2008, 13h14   #4
Malcolm McLean
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why does this compile?

<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


  Réponse avec citation
Vieux 01/02/2008, 14h03   #5
Army1987
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why does this compile?

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")
  Réponse avec citation
Vieux 01/02/2008, 17h35   #6
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Why does this compile?

"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"
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h18.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13602 seconds with 14 queries