【问题标题】:Pointers ABC. error: invalid type argument of unary ‘*’ (have ‘struct config’)指针 ABC。错误:一元“*”的类型参数无效(具有“结构配置”)
【发布时间】:2012-03-22 12:43:09
【问题描述】:

我有一个基本问题。我现在应该对指针有足够的了解了。我看到它的方式 configData 是链接列表中的第一个链接(类型 struct config),而 procNames 是指向类型 struct config 的链接列表中的第一个链接的指针。因此,如果我想说 procNames 等于 configData,那么我需要访问指向 configData 的指针,即*configData。无论如何,我认为我错过了一些东西。有人看到问题了吗?另外,我得到下一个错误:错误: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;

【问题讨论】:

    标签: c pointers struct linked-list


    【解决方案1】:

    我想你想要

    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
    

    【讨论】:

    • 不会是 procName->lines[i] = *linePtr 吗?
    【解决方案2】:

    根据您对您正在尝试做的事情的描述,您需要获取 configData 的地址(在最后一行写 &configData)。您在最后一行尝试做的是取消引用 configData,编译器不会让您这样做,因为 configData 不是指针(它不存储地址)。

    错误信息对此相当清楚。一元 * 将单个指针作为参数,但使用的参数是 struct config 类型,而不是指针。

    【讨论】:

      猜你喜欢
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      相关资源
      最近更新 更多