【问题标题】:accessing structure variable through pointer通过指针访问结构变量
【发布时间】:2014-04-29 18:39:13
【问题描述】:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct male_node
{
    int mv1,mv2,mv3;
}male;

typedef struct movie_name
{
    char mvnm[20];
    struct male *ml;
}movie;

main()
{
    movie mov1;
    mov1.ml=(male*)malloc(sizeof(male));
    mov1.(*ml).mv1=1;//ERROR
    mov1.ml->mv1=1; //ERROR
}

如何通过mov1和ml访问mv1变量 我试图通过 ml 访问 mv1,它是一个指向结构的指针,它又是一个结构变量,但它显示错误。

【问题讨论】:

  • 你能发布返回的错误吗?
  • 我知道你没有必须int前面加上int,但是当我看到它不见了时,我的心很痛。
  • 现代 C 真的还允许省略返回类型吗?
  • @RobK:不,现在需要。

标签: c pointers structure


【解决方案1】:

这看起来不对:

struct male *ml;

您已经使用typedefmale 定义为struct male_node,所以说struct male 没有意义。相反,试试这个:

typedef struct movie_name
{
    char mvnm[20];
    male *ml;
} movie;

这应该可以解决您的问题,并且您应该能够做到这一点:

mov1.ml->mv1=1;

【讨论】:

  • 谢谢。其实我没有注意到这一点。现在可以正常使用了。
  • mov1.(*ml).mv1=1;(*mov1.ml).mv1=1;
猜你喜欢
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多