C语言 如何定义指向结构的指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14475797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to define a pointer to a structure
提问by SLearner
I know this is a very basic problem, but I cannot move forward without it and its not clearly explained elsewhere.
我知道这是一个非常基本的问题,但没有它我就无法前进,而且在其他地方也没有明确解释。
Why is this programming giving me so many errors of undeclared identifier? I have declared it, though.
为什么这个编程给了我这么多未声明标识符的错误?不过我已经声明了。
These are the error i am getting.
这些是我得到的错误。
Error 2 error C2143: syntax error : missing ';' before 'type'
Error 3 error C2065: 'ptr' : undeclared identifier
Error 4 error C2065: 'contactInfo' : undeclared identifier
Error 5 error C2059: syntax error : ')'
Error 15 error C2223: left of '->number' must point to struct/union
and more...
和更多...
#include<stdio.h>
#include<stdlib.h>
typedef struct contactInfo
{
int number;
char id;
}ContactInfo;
void main()
{
char ch;
printf("Do you want to dynamically etc");
scanf("%c",&ch);
fflush(stdin);
struct contactInfo nom,*ptr;
ptr=(contactInfo*)malloc(2*sizeof(contactInfo));
nom.id='c';
nom.number=12;
ptr->id=nom.id;
ptr->number=nom.number;
printf("Number -> %d\n ID -> %c\n",ptr->number,ptr->id);
}
采纳答案by Veger
typedef struct contactInfo
{
int number;
char id;
}ContactInfo;
This code defines 2 things:
这段代码定义了两件事:
- a typenamed
ContactInfo - a
structnamedcontactInfo
- 一类命名
ContactInfo - 一个
struct有名的contactInfo
Notice the difference of the cand C!
注意的区别c和C!
In your code you are using a mixed combination of both, which is allowed (although confusing IMHO).
在您的代码中,您使用的是两者的混合组合,这是允许的(尽管恕我直言令人困惑)。
If you use the structvariant you need to explicitly use struct contactInfo. For the other variant (ContactInfo) you mustto omit the structpart as it is part of the type definition alteady.
如果您使用struct变体,则需要明确使用struct contactInfo. 对于另一个变体 ( ContactInfo),您必须省略该struct部分,因为它一直是类型定义的一部分。
So be careful with both different definitions of your structure. Best would be to only use either one of the variants.
所以要小心你的结构的两种不同定义。最好是只使用其中一种变体。
I do not have Visual Studio at hand, but the following (corrected) code compiles with gcc properly without any warnings:
我手头没有 Visual Studio,但以下(更正的)代码使用 gcc 正确编译,没有任何警告:
#include<stdlib.h>
typedef struct contactInfo
{
int number;
char id;
}ContactInfo;
void main()
{
ContactInfo nom,*ptr;
ptr=malloc(2*sizeof(ContactInfo));
}
(I left out the not so interesting/unmodified parts of the code)
(我省略了代码中不太有趣/未修改的部分)
回答by unwind
This:
这个:
ptr=(contactInfo*)malloc(2*sizeof(contactInfo));
is wrong, there's no type called contactInfo.
错了,没有类型叫contactInfo.
There's a struct contactInfo, which is typedef:d as ContactInfo. C is case-sensitive (and you must include the structunlike how it works in C++).
有一个struct contactInfo, 即typedef:d as ContactInfo。C 区分大小写(并且您必须包括struct它在 C++ 中的不同工作方式)。
Also note that the quoted line is better written as:
另请注意,引用的行最好写为:
ptr = malloc(2 * sizeof *ptr);
Since casting the return value of malloc()is a bad idea in C, I removed the cast. Also, repeating the type is a bad idea since it makes the risk of introducing error greater, so I removed that as well.
由于在 C 中转换返回值malloc()是一个坏主意,我删除了转换。此外,重复类型是一个坏主意,因为它会增加引入错误的风险,所以我也删除了它。
Note that sizeof *ptrmeans "the size of the the type that ptrpoints at" and is a very handy thing.
请注意,这sizeof *ptr意味着“ptr指向的类型的大小”并且非常方便。
回答by akp
Replace
代替
ptr=(contactInfo*)malloc(2*sizeof(contactInfo));
with
和
ptr=malloc(2*sizeof(struct contactInfo));
回答by Aniket Inge
C is a case sensitive language.
C 是一种区分大小写的语言。
ptr=(contactInfo)malloc(2*sizeof(contactInfo));
ptr=(contactInfo)malloc(2*sizeof(contactInfo));
which should be:
应该是:
ptr=malloc(2*sizeof(ContactInfo));
ptr=malloc(2*sizeof(ContactInfo));
回答by Tushar Mishra
struct contactInfo nom,*ptr;
ptr=(contactInfo*)malloc(2*sizeof(contactInfo));
here you are using contactInfo to typcast where as it should be struct contactInfo. and as you have typedef it into ContactInfo you can use that also.
在这里,您使用 contactInfo 来打字,因为它应该是 struct contactInfo。并且当您将其 typedef 到 ContactInfo 时,您也可以使用它。
struct contactInfo nom,*ptr;
ptr=(ContactInfo*)malloc(2*sizeof(ContactInfo));

