c - 指针ABC。错误: invalid type argument of unary ‘*’ (have ‘struct config’ )

标签 c pointers struct linked-list

我有一个基本问题。我现在应该对指针有足够的了解了。我认为 configData 是(结构体配置类型的)链表中的第一个链接,而 procNames 是指向结构体配置类型的链表中的第一个链接的指针。因此,如果我想说 procNames 等于 configData,那么我需要访问指向 configData 的指针,即 *configData。无论如何,我觉得我错过了一些东西。有人看到问题了吗?另外,我收到下一个错误:错误:一元“*”的类型参数无效(具有“struct config”)

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

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

//global variable
struct config configData;
//local variable
struct config *procNames;
//the problem (done locally) 
procNames = *configData;

最佳答案

我想你想要

procNames = &configData;

这会将指针 procNames 设置为结构体 configData 的地址。

您可以使用以下任一方式访问元素

procNames->count
procNames->lines[i].name  // Pointer to the 1st char of the name in the i'th config_line structure

configData.count
configData.lines[i].name

请记住,由于 lines 本身就是一个指针,因此您需要为每个 config_line 结构分配内存:

struct config_line thisLine;   // Declare a structure
procNames->lines = &thisLine;  // Point to it

// Declare a pointer to an array of structures, allocate memory for the structures
struct config_line *linePtr = malloc(NUM_STRUCTS * sizeof(struct config_line));
procName->lines[i] = *linePtr; // Points to 1st structure in the array

关于c - 指针ABC。错误: invalid type argument of unary ‘*’ (have ‘struct config’ ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9559957/

相关文章:

c - 使用 dup2 重定向时出现空白字符串

c - 我无法访问结构内部的数组

c - 编译和链接所有 C 文件的别名或命令

c - FILE类型的对象有哪些属性

java - 从 C 服务器向 Java 客户端发送消息

c++ - C++中的指针数组以及 "BUS ERROR"是什么

c - 由 2 个结构组成的链表。接入节点

c - 在 C 中释放指针时显示垃圾值

c - 声明指向从结构定义中接受结构的函数的指针?

c - 是否可以在C中动态定义结构