Above code works under Ubuntu20.
Can someone please help me to understand the struct variable 'pandora' declaration and initialization issue?
Specifically, the issue is from line3 to line9, what is this declaration style? Any reference would be better.
It seems that I declared a struct variable 'pandora' , and then assigned values to this variable.
I was confused on two variables 'pandora' did not make the gcc complier to issue error.
Many Thanks!
Just found that this doesn't compile:
struct Pandora {
char *version;
int status;
} pandora = {"some str", 0};
struct Pandora pandora = { "0.0.5", 4};
then I realized that line3 to line9 may works just like this:
struct Pandora pandora;
struct Pandora pandora = { "0.0.5", 4};
the first pandora appears as a decleration, while the second is as a definition.
Many thanks!
This is what I have in my memory.
struct Pandora pandora; //declare a variable named pandora as struct Pandora type
//declare a variable named pandora with struct Pandora type, meanwhile the variable is initialized as //{'0.05' ,4}
struct Pandora pandora = { "0.0.5", 4};
I was confused something similar like below:
int a;
int a=3;
Above way of declare an integer variable would issue a gcc compiler error.
But, this pandora struct does not.
Anyway, I think the way you explained is one of ways to understand this issue.
Did you have any reference, or which books do introduce this fashion of struct declaration?
@blogger2020:
int a;
int a = 3;
works well when appears in file scope.
you can view the c89 standard draft here.
And, you might get some intuition from cfaq.
@jakio6:
Ok. Thanks for the reference, and I will check it.
So far, I have not found the reference, but the point is taken, which this is one of the coding styles in C.
Maybe, it is not clear in my memory, but it stays in C standard.
Anyway, Thanks for the answer.
I suppose u will understand that you see my code below:
#include <stdio.h>
struct Pandora
{
char* version;
int status;
} pandora;
int main()
{
printf("%d %s\n", pandora.status, pandora.version);
printf("%d %sin", pandorax.status, pandorax.version);
return 0;
}
struct Pandora pandora = { "0.0.4",4 };
// designated initialization
struct Pandora pandorax = { "0.0.2",0 };
However,the code cannot be compiled cause I don't declare a variable before using.So I comment the wrong codes and it will like this:
#include <stdio.h>
struct Pandora
{
char* version;
int status;
} pandora;
int main()
{
printf("%d %s\n", pandora.status, pandora.version);
//printf("%d %sin", pandorax.status, pandorax.version);
return 0;
}
struct Pandora pandora = { "0.0.4",4 };
// designated initialization
//struct Pandora pandorax = { "0.0.2",0 };
Opps,it can run with no error.And it shows the info I give it.As we all know,the global variable initialization will fininsh before running the main function.So I can make a conclusion:A variable name after the structure declaration aims to tell the compiler you should give me a seat and i will use it.
Many thanks!
It is like struct declaration and initialization issue.
Your answer brings my memory something like below:
'... to initialize a struct variable, key word 'strcut' and struct name have to be used; treat it like function declaration fashion...'
I could not remember which book brings this.
If you have, please do let us know.
@blogger2020: Glad to see it can give you some help.I am sorry,i have read few books about it yet.I don't know.