Why does it work?

O

Osiro

Somebody can explain to me why does the code below work in DEV-C++?


#include <stdio.h>
#include <stdlib.h>
int main(){
int vet[10], i=0;
for (i=0;i<10;i++)
vet=i*2;
for (i=0;i<10;i++)
printf("- %d -",i[vet]); // ' i ' is a counter!!!!

printf("\n\n\n");
system("PAUSE");
}
 
K

Keith Thompson

Osiro said:
Somebody can explain to me why does the code below work in DEV-C++?


#include <stdio.h>
#include <stdlib.h>
int main(){
int vet[10], i=0;
for (i=0;i<10;i++)
vet=i*2;
for (i=0;i<10;i++)
printf("- %d -",i[vet]); // ' i ' is a counter!!!!

printf("\n\n\n");
system("PAUSE");
}


The comp.lang.c FAQ is at <http://www.c-faq.com/>.
You've just asked question 6.11.
 
M

Malcolm McLean

Osiro said:
Somebody can explain to me why does the code below work in DEV-C++?


#include <stdio.h>
#include <stdlib.h>
int main(){
int vet[10], i=0;
for (i=0;i<10;i++)
vet=i*2;
for (i=0;i<10;i++)
printf("- %d -",i[vet]); // ' i ' is a counter!!!!

printf("\n\n\n");
system("PAUSE");
}

It is a little quirk of the language.

You can invert the order of the index and the array. Presumably it saved
Denis Ritchie a couple of lines of code when he was writing the first C
compiler.

As far as I know it is an entirely useless feature that has no place in
serious programs.
 
M

Mike Wahler

Osiro said:
Somebody can explain to me why does the code below work in DEV-C++?


#include <stdio.h>
#include <stdlib.h>
int main(){
int vet[10], i=0;
for (i=0;i<10;i++)
vet=i*2;
for (i=0;i<10;i++)
printf("- %d -",i[vet]); // ' i ' is a counter!!!!

printf("\n\n\n");
system("PAUSE");
}


vet is the same as *(vet + i) which is the same as *(i + vet)

But you should write code that's more obvious and readable,
and say what you mean, i.e. vet (index into vet).

-Mike
 
S

santosh

Osiro said:
Somebody can explain to me why does the code below work in DEV-C++?


#include <stdio.h>
#include <stdlib.h>

You don't use anything from stdlib.h
int main(){
int vet[10], i=0;
for (i=0;i<10;i++)
vet=i*2;


You are use the array vet without initialising it.
for (i=0;i<10;i++)
printf("- %d -",i[vet]); // ' i ' is a counter!!!!

Yes, that's a characteristic of C arrays. Internally array notation is
converted into it's equivalent pointer form. So vet would become *(vet +
i) which is also equivalent to *(i + vet) which is what i[vet] would
resolve to.

It is not a readable way though. It's better to write what you mean.

Also C++ style comments may not port to some C compilers, nor compile under
certain switches, specifically those that enable strict C90 conformance.
printf("\n\n\n");
system("PAUSE");

This might not work on many platforms, since the "PAUSE" command is not
available everywhere. A more portable alternative is:

getchar();

This should pause the program until a character is typed from the keyboard.
Of course, stdin might have been redirected or closed or input might
already be available, so it's not a robust solution. A busy loop is also
another possibility. The best course is to get rid of this need to pause
before termination.
 
M

Martien verbruggen

Osiro wrote:
int main(){
int vet[10], i=0;
for (i=0;i<10;i++)
vet=i*2;


You are use the array vet without initialising it.


But the array element values aren't being read. Something is being
stored. There isn't anything wrong with this.

Martien
 
C

CBFalconer

santosh said:
Osiro said:
Somebody can explain to me why does the code below work in DEV-C++?

#include <stdio.h>
#include <stdlib.h>

You don't use anything from stdlib.h
int main(){
int vet[10], i=0;
for (i=0;i<10;i++)
vet=i*2;


You are use the array vet without initialising it.


Not so. There is indentation missing, but that is part of the for
loop, and is initializing the array. The absence of blanks in the
code deserves calumny, though.
 
K

karthikbalaguru

Somebody can explain to me why does the code below work in DEV-C++?

#include <stdio.h>
#include <stdlib.h>
int main(){
int vet[10], i=0;
for (i=0;i<10;i++)
vet=i*2;
for (i=0;i<10;i++)
printf("- %d -",i[vet]); // ' i ' is a counter!!!!

printf("\n\n\n");
system("PAUSE");


That is allowed in C language.

Karthik Balaguru
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top