dimanche 21 décembre 2014

Why define struct in variable?


Consider the following program:


Many people when they want to use a struct, they create a new variable as:



struct structureName variableName


While it works when you just define it as:



structureName variableName


My teacher always uses the first method. My question is how do they differ? Do I ever need to specify struct before defining my variableName. Here is an example to explain my question:



struct example {
int n;
char c;
};

int main() {
example o;
o.c = 'c';
o.n = 5;
printf("%c", o.c);
printf("%d\n", o.n); //this works

struct example ex; // this versus "example o" without using struct keyword
ex.c = 'e';
ex.n = 7;
printf("%c", ex.c);
printf("%d", ex.n); //this works

return 0;
}




Aucun commentaire:

Enregistrer un commentaire