InitList_Sq(SqList &L);与InitList_Sq(&L);的区别?

sunxlbutu 2009-02-09 02:02:46
这是实现顺序表的初始化c程序:
#include "stdio.h"
#include "stdlib.h"

#define OVERFLOW 0
#define OK 1

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef struct {
int *elem;
int length;
int listsize;
}SqList;

int InitList_Sq(SqList *L);

int main()
{
SqList L;

InitList_Sq(SqList &L);

system("pause");
return 0;

};

int InitList_Sq(SqList *L)
{
L->elem = (int *)malloc(LIST_INIT_SIZE * sizeof(int));
if(!L->elem) exit(OVERFLOW);

L->length = 0;
L->listsize = LIST_INIT_SIZE;

return OK;
}
其中 InitList_Sq(SqList &L); 出错:
--------------------Configuration: 线性表的顺序表示 - Win32 Debug--------------------
Compiling...
线性表的顺序表示.cpp
F:\MyProject\线性表的顺序表示\线性表的顺序表示.cpp(22) : error C2275: 'SqList' : illegal use of this type as an expression
F:\MyProject\线性表的顺序表示\线性表的顺序表示.cpp(14) : see declaration of 'SqList'
Error executing cl.exe.

线性表的顺序表示.obj - 1 error(s), 0 warning(s)

如果把 InitList_Sq(SqList &L); 改为 InitList_Sq(&L); 后编译就不会出错,这是为什么呢?
...全文
1935 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunxlbutu 2009-02-09
  • 打赏
  • 举报
回复
这么一回事啊
谢谢啦
daodan988 2009-02-09
  • 打赏
  • 举报
回复
1楼正解
雪鹰翔天 2009-02-09
  • 打赏
  • 举报
回复
函数调用弄明白了就好了....


int InitList_Sq(SqList *L); //函数声明.形参为指针

int main()
{
SqList L;

InitList_Sq(SqList &L); //此处是函数调用,传递实参,需要赋指针或地址,即&L

system("pause");
return 0;

};

int InitList_Sq(SqList *L)
{
L->elem = (int *)malloc(LIST_INIT_SIZE * sizeof(int));
if(!L->elem) exit(OVERFLOW);

L->length = 0;
L->listsize = LIST_INIT_SIZE;

return OK;
}
雪鹰翔天 2009-02-09
  • 打赏
  • 举报
回复

int InitList_Sq(SqList *L);//形参是个指针,只需要赋指针(地址)给它就可以了
爱摸鱼de老邪 2009-02-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 freedomzcd 的回复:]
C/C++ codeSqList L;//这里定义了InitList_Sq(SqList&L);
[/Quote]
如楼上所言,你重复定义了
雪鹰翔天 2009-02-09
  • 打赏
  • 举报
回复

SqList L; //这里定义了

InitList_Sq(SqList &L);

33,322

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧