如何访问指针成员在C结构变量? [英] How to access pointer members in a Struct variable in C?

查看:107
本文介绍了如何访问指针成员在C结构变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是新的C,但我只是发现了,我必须处理的一个问题。
如何访问一个结构的成员是一个指向另一个结构?

I'm not new to C but I just found out a problem which I have to deal with. How do I access the member of a struct that is a pointer to another struct?

恩。

typdef struct {
   int points;
} tribute;

typedef struct {
    int year;
    tribute *victor;
} game;

int main(){
  tribute myVictor;
  myVictor.points = 10;  

  game myGame;
  myGame.year = 1994; // Runs fine
  myGame.victor = myVictor; // I want to point the victor member of the game struct to 
                            //myVictor object... But it gives me an error
} 

我怎么能纠正呢?我知道我应该所做的myGame变量的指针..但我问,如果我能在一个正常的结构体变量做到这一点。

How could I correct this? I know that I should've made the myGame variable as a pointer.. but I'm asking if I can do this in a normal struct variable.

推荐答案

尝试:

myGame.victor = &myVictor;

这篇关于如何访问指针成员在C结构变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆