C语言 表达式必须有指针类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5611066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
expression must have pointer type
提问by aycan
I have a problem,the program gives an error like "expression must have pointer type".Can you help me ?
我有一个问题,程序给出了一个错误,比如“表达式必须有指针类型”。你能帮我吗?
struct stack{
int i_data;
char c_data;
struct stack *next;
}top;
void push_i(struct top *newptr,int info){
newptr=(struct top*)malloc(sizeof(top));
if(newptr!=NULL){
top->c_data=NULL;
newptr->i_data=info;
newptr->next=*top;
*top=newptr;
}
回答by Erik
- You're mixing the type
struct stackwith the variabletop - Your
topvariable isn't a pointer, you can't change what it points at. c_dataisn't a pointer, so don't assignNULLto it.- You're not using the passed
newptrfor anything useful - it should be a local variable,.
- 您正在将类型
struct stack与变量混合top - 您的
top变量不是指针,您无法更改它指向的内容。 c_data不是指针,所以不要分配NULL给它。- 您没有将传递的
newptr用于任何有用的东西 - 它应该是一个局部变量,。
This may work better:
这可能效果更好:
struct stack{
int i_data;
char c_data;
struct stack *next;
};
...
struct stack * top = NULL;
...
void push_i(int info){
struct stack * newptr=(struct stack*)malloc(sizeof(struct stack));
if(newptr!=NULL){
top->c_data=0;
newptr->i_data=info;
newptr->next=top;
top=newptr;
}
回答by PAntoine
You have several problems. The 1st is you are using the type toprather than the variable newptr.
你有几个问题。第一个是您使用的是类型top而不是变量newptr。
Also you might want to be using **newptr when passing the variable in.
此外,您可能希望在传入变量时使用 **newptr。
回答by Ingo
newptr->next = top
top = newptr
Remember, if x is declared T*, then x is the pointer, and *x is T. This is really not hard to understand. You want to assign the pointer, not overwrite where the pointer points.
记住,如果x被声明为T*,那么x就是指针,而*x就是T。这真的不难理解。您要分配指针,而不是覆盖指针指向的位置。
回答by Angelom
Look at your 'struct stack', thats the description of the structure. Next loop at top, that's an instance of your structure. You appear to be mixing the both. There shouldn't be any 'struct top' anywhere, they should be 'struct stack'.
看看你的“结构堆栈”,这就是结构的描述。顶部的下一个循环,这是您的结构的一个实例。您似乎将两者混合在一起。任何地方都不应该有任何“struct top”,它们应该是“struct stack”。

