一个很简单的链表问题,请帮忙看一下!
#include <stdio.h>
#include <stdlib.h>
typedef struct Lnode{
int data;
struct Lnode *next;
}Link,*pLink;
void main()
{
pLink *s;
s = (pLink)malloc(sizeof(Link));
s->data = 1;
printf("%d\n", s->data);
free(s);
}
编译出错:test.c: In function ‘main’:
test.c:12: warning: assignment from incompatible pointer type
test.c:13: error: request for member ‘data’ in something not a structure or union
test.c:14: error: request for member ‘data’ in something not a structure or union
很不明白,请各位帮忙看一下是怎么回事,多谢了!
[解决办法]
pLink *s;改为Link *s;就OK了 指针用的时候要注意
[解决办法]
pLink本身就是一个结构体指针pLink s
[解决办法]
指针级别问题
pLink的指针指针级别是Lnode*,s是指向pLink的指针,因此s的指针级别为Lnode**(二级指针)。
可以将s降为一级指针(Link*或者pLink):
#include<stdio.h>#include<stdlib.h>#include<malloc.h>typedef struct Lnode{ int data; struct Lnode *next;}Link,*pLink;int main(){ pLink s; s=(pLink)malloc(sizeof(Link)); s->data=1; printf("%d\n",s->data); free(s); system("pause"); return(0);}