看如下代码:(这段代码用.c文件存放,用C编译器编译)
#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct SQ{
int *elem; //存储空间基址
int length;
int listsize;
}SqList,pSqList;
void InitList_Sq(SqList &Lt)
{
Lt.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(!Lt.elem)
exit(0);
Lt.length=0;
Lt.listsize=LIST_INIT_SIZE;
}
void main()
{
SqList L1;
InitList_Sq(L1);
printf("%d",L1.length);
}
请问:这段代码有什么问题吗???
//////////////////////////////////////////
有问题!!!!!!!!!!!!!
在哪???????????????
我们编译发现,会出现如下错误:
--------------------Configuration: sxb - Win32 Debug--------------------
Compiling...
sxb.c
E:/VC6.0 Workspace/ccc/sxb.c(12) : error C2143: syntax error : missing ')' before '&'
E:/VC6.0 Workspace/ccc/sxb.c(12) : error C2143: syntax error : missing '{' before '&'
E:/VC6.0 Workspace/ccc/sxb.c(12) : error C2059: syntax error : '&'
E:/VC6.0 Workspace/ccc/sxb.c(12) : error C2059: syntax error : ')'
E:/VC6.0 Workspace/ccc/sxb.c(23) : warning C4013: 'InitList_Sq' undefined; assuming extern returning int
Error executing cl.exe.
sxb.obj - 4 error(s), 1 warning(s)
///////////////////////////////////////////
解决方案:
1,将文件改为.cpp格式
或2,去掉引用,改为指针,这样的话,其他地方也要改该
原因是:在C语言中没有引用这个概念,不存在引用,在C++中才有
本文分析了一段使用C语言编写的代码中存在的问题,主要探讨了由于C语言不支持引用而导致的编译错误,并给出了相应的解决方案。

被折叠的 条评论
为什么被折叠?



