简体   繁体   English

如何定义指向结构的指针

[英]How to define a pointer to a structure

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);

}
typedef struct contactInfo
{
    int number;
    char id;
}ContactInfo;

This code defines 2 things: 这段代码定义了两件事:

  1. a type named ContactInfo 一个名为ContactInfo类型
  2. a struct named contactInfo 一个名为contactInfostruct

Notice the difference of the c and C ! 注意cC的区别!

In your code you are using a mixed combination of both, which is allowed (although confusing IMHO). 在你的代码中,你使用两者的混合组合,这是允许的(虽然混淆恕我直言)。

If you use the struct variant you need to explicitly use struct contactInfo . 如果使用struct变量,则需要显式使用struct contactInfo For the other variant ( ContactInfo ) you must to omit the struct part as it is part of the type definition alteady. 对于另一个变体( 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) (我遗漏了代码中不那么有趣/未经修改的部分)

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 . 有一个struct contactInfo ,它是typedef :d作为ContactInfo C is case-sensitive (and you must include the struct unlike how it works in C++). 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. 由于在C中强制转换malloc()的返回值是一个坏主意 ,我删除了强制转换 Also, repeating the type is a bad idea since it makes the risk of introducing error greater, so I removed that as well. 此外,重复该类型是一个坏主意,因为它会导致引入错误的风险更大,所以我也删除了它。

Note that sizeof *ptr means "the size of the the type that ptr points at" and is a very handy thing. 请注意, sizeof *ptr表示“ ptr指向的类型的大小”,这是一个非常方便的事情。

Replace 更换

ptr=(contactInfo*)malloc(2*sizeof(contactInfo));

with

ptr=malloc(2*sizeof(struct contactInfo));

C is a case sensitive language. C是区分大小写的语言。

ptr=(contactInfo)malloc(2*sizeof(contactInfo));

which should be: 应该是:

ptr=malloc(2*sizeof(ContactInfo));

    struct contactInfo nom,*ptr;
    ptr=(contactInfo*)malloc(2*sizeof(contactInfo));

Since you have defined a typedef, the first statement above can be written as below, though what you have written is also correct but that does not effectively use the typedef you defined. 由于您已经定义了一个typedef,上面的第一个语句可以写成如下所示,尽管您编写的内容也是正确的但是没有有效地使用您定义的typedef

  ContactInfo nom,*ptr;

Also, since C is case sensitive language,use as you defined in typedef. 此外,由于C区分大小写的语言,请按照您在typedef中的定义使用。 Capitalise C in contactinfo contactinfo大写C

The typecasting you are doing should not be done and considered to be a bad practise. 你正在做的typecasting不应该被认为是一种不好的做法。 As void * is automatically and safely promoted to any other pointer type in this case of malloc . 作为void *自动,安全地提升到在这种情况下任何其他指针类型malloc

ptr=malloc(2*sizeof(ContactInfo));
struct contactInfo nom,*ptr;
ptr=(contactInfo*)malloc(2*sizeof(contactInfo));

here you are using contactInfo to typcast where as it should be struct contactInfo. 在这里你使用contactInfo来打字,因为它应该是struct contactInfo。 and as you have typedef it into ContactInfo you can use that also. 并且当你将其输入到ContactInfo中时,你也可以使用它。

    struct contactInfo nom,*ptr;
    ptr=(ContactInfo*)malloc(2*sizeof(ContactInfo));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM