2012-03-04 98 views
2

我有一个基本问题。现在我应该足够了解指针。我看到它的方式configData是链接列表中的第一个链接(类型为struct config),而procNames是指向类型为struct config的链接列表中的第一个链接的指针。所以如果我想说procNames等于configData,那么我需要访问指向configData的指针*configData。无论如何,我想我失去了一些东西。任何人都看到了问题?另外,我得到了一个错误:错误:invalid type argument of unary ‘*’ (have ‘struct config’)指针ABC。错误:一元'*'的无效类型参数(有'结构配置')

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; 

回答

3

我想你想

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 
+0

不会是procName-> lines [i] = * linePtr? – 2012-03-05 00:03:37

+1

@PaulKar。是。好 - 赶快 - 谢谢! – 2012-03-05 02:15:32

2

根据你的你正在尝试做的说明,你需要采取configData的地址(写在最后一行& configData)。在最后一行中你要做的是取消引用configData,编译器不会让你这么做,因为configData不是一个指针(它不会存储地址)。

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

相关问题