#include<iostream>
#include<conio.h>
#include<string.h>
#define OK 1
#define MAXSIZE 100
using namespace std;
typedef int Status;
typedef struct
{
char name[5];
float price;
}Book;
typedef Book ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LNode *A;
Status InitList(LNode *L)
{
L=new LNode;
if(!L)
exit(-1);
else
(*L).next=NULL;
return OK;
}
int main()
{
LNode *L;
InitList(*L);
cout<<L;
getch();
return 0;
}
这是我写的完整代码,就是生成了链表结点,还有初始化链表。
其中,
划线部分,两个参数我写的一样的,因为形参和实参要一致
但是却报错
--------------------Configuration: 线性表 - Win32 Debug--------------------
Compiling...
线性表.cpp
E:\数据结构\线性表.cpp(45) : error C2664: 'InitList' : cannot convert parameter 1 from 'struct LNode' to 'struct LNode *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Error executing cl.exe.
线性表.exe - 1 error(s), 0 warning(s)
为甚么呢?
这个问题是关于 * 的用法,用在声明的时候它指声明一个指针类型的变量,结合指针使用的时候指取指针所指的变量;
这里说的是你的参数不对,划线部分要保持参数类型一致,应为指针类型,把* 去掉即可
LNode *L;
InitList(L); 传入的才是LNode *
而
InitList(*L); 传入的是 LNode,不是 LNode *,*L是根据L指针得到目标上的值。
而且这么写,InitList无法将对L的改变作用到main上,正确的写法看我昨天给你的程序。