gpt4 book ai didi

c - 访问结构的副本。我收到错误 : request for member ‘count’ in something not a structure or union

转载 作者:太空宇宙 更新时间:2023-11-04 06:40:14 24 4
gpt4 key购买 nike

出于某种原因,我无法访问 procNames.count。我需要做的就是增加计数器。我不确定为什么。有人看到问题了吗?

struct config_line {
char name[MAX_WORD];
int time;
};

struct config {
struct config_line *lines;
int count;
};

//global variable
struct config configData;

// allocate memory to procNames
procNames = malloc(sizeof(struct config));
if ( procNames == NULL ) {
printf("problem allocating memory, for procNames. int procnanny(void)");
return 0;
}
//local variable
struct config *procNames;

procNames = &configData;
// the problem
procNames.count++;

最佳答案

使用:

procNames->count++;

procNames 是一个指针,因此您需要使用它或冗长的:

(*procNames).count++;

当 LHS 上的值是结构时,您只能使用 . 运算符。当您有一个指向结构的指针时,您必须使用 -> 或取消引用指针,然后应用 . 运算符,这需要括号和 * 因为 . 比一元(解引用)*.

绑定(bind)更紧密

关于c - 访问结构的副本。我收到错误 : request for member ‘count’ in something not a structure or union,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9560624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com