试着理解为什么这不起作用。我一直收到以下错误:
' - > nextNode'的左边必须指向class / struct / union / generic类型
(也是函数new_math_struct中带有 - >的所有行)
标头文件
#ifndef MSTRUCT_H
#define MSTRUCT_H
#define PLUS 0
#define MINUS 1
#define DIVIDE 2
#define MULTIPLY 3
#define NUMBER 4
typedef struct math_struct
{
int type_of_value;
int value;
int sum;
int is_used;
struct math_struct* nextNode;
} ;
typedef struct math_struct* math_struct_ptr;
#endif
C档
int get_input(math_struct_ptr* startNode)
{
/* character, input by the user */
char input_ch;
char* input_ptr;
math_struct_ptr* ptr;
math_struct_ptr* previousNode;
input_ptr = &input_ch;
previousNode = startNode;
/* as long as input is not ok */
while (1)
{
input_ch = get_input_character();
if (input_ch == ',') // Carrage return
return 1;
else if (input_ch == '.') // Illegal character
return 0;
if (input_ch == '+')
ptr = new_math_struct(PLUS, 0);
else if (input_ch == '-')
ptr = new_math_struct(MINUS, 0);
else if (input_ch == '/')
ptr = new_math_struct(DIVIDE, 0);
else if (input_ch == '*')
ptr = new_math_struct(MULTIPLY, 0);
else
ptr = new_math_struct(NUMBER, atoi(input_ptr));
if (startNode == NULL)
{
startNode = previousNode = ptr;
}
else
{
previousNode->nextNode = ptr;
previousNode = ptr;
}
}
return 0;
}
math_struct_ptr* new_math_struct(int symbol, int value)
{
math_struct_ptr* ptr;
ptr = (math_struct_ptr*)malloc(sizeof(math_struct_ptr));
ptr->type_of_value = symbol;
ptr->value = value;
ptr->sum = 0;
ptr->is_used = 0;
return ptr;
}
char get_input_character()
{
/* character, input by the user */
char input_ch;
/* get the character */
scanf("%c", &input_ch);
if (input_ch == '+' || input_ch == '-' || input_ch == '*' || input_ch == '/' || input_ch == ')')
return input_ch; // A special character
else if (input_ch == '\n')
return ','; // A carrage return
else if (input_ch < '0' || input_ch > '9')
return '.'; // Not a number
else
return input_ch; // Number
}
C文件的标头只包含对struct头的引用和函数的定义。语言C。
答案 0 :(得分:6)
由于math_struct_ptr
已经包含指针声明符,因此您无需在使用时指定它。放下*
:
math_struct_ptr ptr;
或写
struct math_struct *ptr;
答案 1 :(得分:2)
math_struct_ptr *是一个math_struct ** - 你正在创建一个指向嵌套星形指针的指针。你的typedef的优点是你不必在math_struct_ptr之后放一个*,所以你可以把它留下来
答案 2 :(得分:1)
你是typedef
typedef struct math_struct* math_struct_ptr;
因此,如果使用math_struct_ptr*
,则会获得指向指针的指针。在这种情况下,您可能只想要math_struct_ptr
。我不会用typedef
隐藏指针,但我看到很多人这样做。我发现它只是令人困惑。这可能是一个品味问题。
答案 3 :(得分:0)
他们是你的typedef解析,你将使用:
struct math_struct * *
而不是
struct math_struct *
直接使用math_struct_ptr typedef。