什么是“向前声明”和“typedef结构X'和'结构X'之间的区别? [英] What is 'forward declaration' and the difference between 'typedef struct X' and 'struct X'?
问题描述
我在C编程初学者,我知道结构类型声明和typedef结构的声明之间的区别。我碰到知道一个答案说,如果我们定义诸如结构:
typedef结构
{
一些成员;
} struct_name;
然后,它会像提供一个别名匿名结构(因为它是不是有一个标签名)。因此,它不能被用于正向声明。我不知道什么是向前声明的意思。
另外,我想知道,下面的code:
typedef结构NAME
{
一些成员;
} struct_alias;
有名称之间有什么区别
和 struct_alias
?或者,都等于为struct_alias是结构名的别名?
此外,我们可以声明类型的变量结构名称
这样的:
struct_alias变量1;
和/或类似:
结构名变量2;
或类似:
NAME variable3;
结构
时,你需要有循环结构声明前置声明可能是有用的。例如:
结构A {
结构的b * b_pointer;
INT℃;
};结构B {
结构体一个* a_pointer;
无效* D;
};
在结构A
声明,它不知道的结构规格b
,但你可以转发引用它。
当您的typedef匿名结构,然后编译器将不允许你使用它之前的typedef的名字。
这是非法的:
结构A {
B * b_pointer;
INT℃;
};typedef结构{
结构体一个* a_pointer;
无效* D;
} B://结构b体从未声明或定义
这虽然是合法的:
结构A {
结构的b * b_pointer;
INT℃;
};typedef结构B {
结构体一个* a_pointer;
无效* D;
} B://结构b的定义,并有一个别名类型称为B
所以是这样的:
typedef结构B B;
// B型referes一个尚未定义的类型结构b结构A {
B * struct_b_pointer;
INT℃;
};结构B {
结构体一个* a_pointer;
无效* D;
};
这:
的typedef INT B:结构A {
结构的b * struct_b_pointer;
b b_integer_type;
INT℃;
};结构B {
结构体一个* a_pointer;
无效* D;
};//结构b和b是两个不同类型的所有在一起。注:这是不是在C ++允许
I am a beginner in C programming and I know the difference between struct type declaration and typedef struct declaration. I came across to know an answer saying that if we define a struct like:
typedef struct
{
some members;
}struct_name;
Then it will be like providing an alias to an anonymous struct(as it is not having a tag name). So it cant be used for forward declaration. I don't know what the "forward declaration" means.
Also, I wanted to know that for the following code:
typedef struct NAME
{
some members;
}struct_alias;
Is there any difference between NAME
and struct_alias
? Or are both equal as struct_alias is an alias of struct NAME?
Furthermore, can we declare a variable of type struct NAME
like these:
struct_alias variable1;
and/or like:
struct NAME variable2;
or like:
NAME variable3;
struct
forward declarations can be useful when you need to have looping struct declarations. Example:
struct a {
struct b * b_pointer;
int c;
};
struct b {
struct a * a_pointer;
void * d;
};
When struct a
is declared it doesn't know the specs of struct b
yet, but you can forward reference it.
When you typedef an anonymous struct then the compiler won't allow you to use it's name before the typedef.
This is illegal:
struct a {
b * b_pointer;
int c;
};
typedef struct {
struct a * a_pointer;
void * d;
} b;
// struct b was never declared or defined
This though is legal:
struct a {
struct b * b_pointer;
int c;
};
typedef struct b {
struct a * a_pointer;
void * d;
} b;
// struct b is defined and has an alias type called b
So is this:
typedef struct b b;
// the type b referes to a yet undefined type struct b
struct a {
b * struct_b_pointer;
int c;
};
struct b {
struct a * a_pointer;
void * d;
};
And this:
typedef int b;
struct a {
struct b * struct_b_pointer;
b b_integer_type;
int c;
};
struct b {
struct a * a_pointer;
void * d;
};
// struct b and b are two different types all together. Note: this is not allowed in C++
这篇关于什么是“向前声明”和“typedef结构X'和'结构X'之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!