简体   繁体   English

指针ABC。 错误:一元'*'的类型参数无效(具有'struct config')

[英]Pointers ABC. error: invalid type argument of unary ‘*’ (have ‘struct config’)

I have a basic problem. 我有一个基本问题。 I should know enough about pointers by now. 我现在应该对指针足够了解。 The way I see it configData is the first link in a linked list (of the type struct config) while procNames is a pointer to the first link in a linked list of the type struct config. 我看到的方式configData是链表(结构配置类型)中的第一个链接,而procNames是指向链表的结构配置类型中第一个链接的指针。 So if I want to say that procNames is equal to configData then I need to access the pointer that points to configData which is *configData . 因此,如果我想说procNames等于configData,那么我需要访问指向configData的指针,该指针是*configData Anyhow I think I am missing something. 无论如何,我认为我缺少了一些东西。 Anyone sees the problem? 有人看到问题了吗? Also, I get the next error: error: invalid type argument of unary '*' (have 'struct config') 另外,我得到下一个错误:错误: invalid type argument of unary '*' (have '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;

I think you want 我想你要

procNames = &configData;

This sets the pointer procNames to the address of the structure configData . procNames指针procNames设置为结构configData的地址。

You can access the elements using either 您可以使用以下任一方式访问元素

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

or 要么

configData.count
configData.lines[i].name

Remember that, since lines is itself a pointer, you'll need to allocate memory for each config_line structure: 请记住,由于lines本身就是一个指针,因此您需要为每个config_line结构分配内存:

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

or 要么

// 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

Based on your description of what you are trying to do, you need to take the address of configData (write &configData on the last line). 根据您对要执行的操作的描述,您需要使用configData的地址(在最后一行写&configData)。 What you are trying to do on the last line is dereference configData, which the compiler will not let you do since configData is not a pointer (it does not store an address inside). 在最后一行尝试做的是取消对configData的引用,因为configData不是指针(它不在内部存储地址),编译器不会让您这样做。

The error message is fairly clear on this. 该错误消息对此很清楚。 Unary * takes a single pointer as an argument, but the argument used of type struct config, and not a pointer. 一元*使用单个指针作为参数,但是该参数使用的是struct config类型,而不是指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM