顺序表编译问题
下面这段程序以xx.c命名有错误,但以xx.cpp就没有错误,到底是什么原因?程序环境是C-Free
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW 0
#define LIST_INIT_SIZE 100
#define LISTINCEEMENT 10
typedef struct
{
int *elem;
int length_h;
int listsize;
}Sqlist;
int InitList_Sq(Sqlist &L)
{
L.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem)
exit(OVERFLOW);
L.length_h=0;
L.listsize=LIST_INIT_SIZE;
return OK;
}
int CreateList_Sq(Sqlist &L)
{
int i;
printf("input the datas:");
for(i=0;i<L.length_h;i++)
scanf("%d",&L.elem[i]);
return OK;
}
int main()
{
//clrscr();
int i,n;
Sqlist L;
InitList_Sq(L);
printf("\ninput the length of the lsit L:");
scanf("%d",&n);
L.length_h=n;
CreateList_Sq(L);
printf("output the datas:");
for(i=0;i<L.length_h;i++)
printf("%d",L.elem[i]);
return 0;
}
以xx.c运行,编译出现下面错误:
--------------------Configuration: mingw5 - CUI Debug, Builder Type: MinGW--------------------
Compiling C:\Users\zhenyi\Desktop\222.c...
[Error] C:\Users\zhenyi\Desktop\222.c:17: error: syntax error before '&' token
[Error] C:\Users\zhenyi\Desktop\222.c:19: error: `L' undeclared (first use in this function)
[Error] C:\Users\zhenyi\Desktop\222.c:19: error: (Each undeclared identifier is reported only once
[Error] C:\Users\zhenyi\Desktop\222.c:19: error: for each function it appears in.)
[Error] C:\Users\zhenyi\Desktop\222.c:26: error: syntax error before '&' token
[Error] C:\Users\zhenyi\Desktop\222.c:30: error: `L' undeclared (first use in this function)
Complete Compile C:\Users\zhenyi\Desktop\222.c: 6 error(s), 0 warning(s)
什么原因?
[解决办法]
C没有引用,只能改成指针。
[解决办法]
&在C里是指针、而在C++里为引用、、、
[解决办法]
啊,我也遇到你这种情况啊。
[解决办法]
同意1楼,在 C 里,
int CreateList_Sq(Sqlist &L)
是非法的,因为C没有"引用"之说.
而在 C++ 中,上面的表示是合法的.
[解决办法]
InitList_Sq(L);应当改为InitList_Sq(&L);
CreateList_Sq(L);应当改为CreateList_Sq(&L);
你再试试吧