【问题标题】:How to define a pointer to a structure如何定义指向结构的指针
【发布时间】:2023-03-18 07:16:01
【问题描述】:

我知道这是一个非常基本的问题,但是没有它我就无法继续前进,而且它在其他地方也没有得到清楚的解释。

为什么这个编程给了我这么多未声明标识符的错误?不过我已经声明了。

这些是我得到的错误。

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

还有更多...

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

}

【问题讨论】:

  • 永远不要在 stdin 上调用 fflush - 它是 UB。
  • 也许你应该多注意阅读C书
  • @PaulR 为什么会这样?我读过,每当您需要使用 scanf 从 unser 获取两个输入时,您都会执行 fflush 以删除管道中的 \n?
  • @shashlearner: fflush 仅对输出或更新流有效 - 对输入流使用 fpurge,或者只调用 getchar() 丢弃不需要的字符。
  • @PaulR 好的。但是代码仍然有问题。我已经去掉了typecast并使用了sizeof *ptr,仍然有20个错误!

标签: c pointers structure


【解决方案1】:
typedef struct contactInfo
{
    int number;
    char id;
}ContactInfo;

这段代码定义了两件事:

  1. 一个名为ContactInfo类型
  2. struct 名为 contactInfo

注意cC 的区别!

在您的代码中,您使用的是两者的混合组合,这是允许的(尽管恕我直言令人困惑)。

如果您使用struct 变体,您需要明确使用struct contactInfo。对于其他变体 (ContactInfo),您必须省略 struct 部分,因为它是类型定义的一部分。

因此,请注意结构的两种不同定义。最好只使用其中一种变体。


我手头没有 Visual Studio,但以下(更正的)代码可以使用 gcc 正确编译而没有任何警告:

#include<stdlib.h>

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


void main()
{
    ContactInfo nom,*ptr;
    ptr=malloc(2*sizeof(ContactInfo));    
}

(我省略了代码中不太有趣/未修改的部分)

【讨论】:

  • 我试过了,但它不起作用。我认为还有其他问题吗?
  • 我在我的答案中添加了一个可以使用 gcc 正确编译的代码的固定版本,以显示所需的更正
【解决方案2】:

这个:

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

错了,没有名为contactInfo的类型。

有一个struct contactInfo,即typedef:d 为ContactInfo。 C 区分大小写(与在 C++ 中的工作方式不同,您必须包含 struct)。

还要注意,引用的行最好写成:

ptr = malloc(2 * sizeof *ptr);

自从casting the return value of malloc() is a bad idea in C,我删除了演员表。此外,重复类型是一个坏主意,因为它会增加引入错误的风险,所以我也将其删除。

注意sizeof *ptr的意思是“ptr指向的类型的大小”,是一个很方便的东西。

【讨论】:

  • 但是如果我不类型转换malloc,它不会返回char类型吗?
  • 好的,所以我删除了类型转换,但它仍然向我显示相同的错误。我正在使用 VS 2010 并将编译器更改为 c\tc 我仍然不明白这里有什么问题 @unwind 。不过谢谢!
【解决方案3】:

C 是区分大小写的语言。

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

应该是:

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

【讨论】:

  • contactInfoContactInfo 是两个不同的东西。所以除了区分大小写之外,还有一点要牢记!
  • 我同意,contactInfo 是一个结构标记,ContactInfo 是该结构的 typedef .. 如果我是对的@Veger
  • 没错,因此我认为区分大小写并不是这里唯一的问题。
【解决方案4】:

替换

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

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

【讨论】:

  • 我试过了,但还是不行。 VS 2010 编译器搞砸了吗?几乎所有的错误都指向 ptr=(contactInfo*)malloc(2*sizeof(contactInfo));删除类型转换的行也会给程序添加更多错误:/
  • 好的 - 如果你修正了你的答案,我会很乐意删除反对票。
【解决方案5】:
struct contactInfo nom,*ptr;
ptr=(contactInfo*)malloc(2*sizeof(contactInfo));

在这里,您使用contactInfo 来进行类型转换,因为它应该是struct contactInfo。并且由于您已将其 typedef 到 ContactInfo 中,因此您也可以使用它。

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

【讨论】:

    猜你喜欢
    • 2010-10-05
    • 2021-10-16
    • 2018-10-20
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    相关资源
    最近更新 更多