首页 新闻 会员 周边 捐助

C struct

0
悬赏园豆:5 [已解决问题] 解决于 2021-10-14 21:38

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!

blogger2020的主页 blogger2020 | 菜鸟二级 | 园豆:257
提问于:2021-10-14 19:10
< >
分享
最佳答案
0

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.

收获园豆:3
jakio6 | 小虾三级 |园豆:1325 | 2021-10-14 19:42

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 | 园豆:257 (菜鸟二级) | 2021-10-14 20:35

@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 | 园豆:1325 (小虾三级) | 2021-10-14 20:55

@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.

blogger2020 | 园豆:257 (菜鸟二级) | 2021-10-14 21:36
其他回答(1)
0

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.

收获园豆:2
寂静的羽夏 | 园豆:2133 (老鸟四级) | 2021-10-14 20:21

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.

支持(0) 反对(0) blogger2020 | 园豆:257 (菜鸟二级) | 2021-10-14 20:52

@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.

支持(0) 反对(0) 寂静的羽夏 | 园豆:2133 (老鸟四级) | 2021-10-14 21:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册