C没有类似C++里的&引用,这个问题如何解决?
c语言吧
全部回复
仅看楼主
level 6
seerliuliu 楼主
#include<stdafx.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
char data[20];
int length;
}SqList;
void main()
{
void InitList(SqList *&L);
SqList* L;
InitList(L);
}
void InitList(SqList* &L) //初始化线性表
{
L=(SqList*)malloc(sizeof(SqList));
L->length=0;
}
在C语言言里面没有&引用,这个函数如果定义成void InitList(SqList*)它会报错,说使用了未定义的指针……
不过有些参考书的初始化就是写成void InitList(SqList*),这样的话,前面的main函数里的语句应该是怎么样的??
2012年05月26日 15点05分 1
level 11
引用和指针就是一码事,你定义成SqList**不就行了
2012年05月26日 15点05分 2
level 6
seerliuliu 楼主
不会转换啊。这个接触的少……
改了半天没改对。
2012年05月26日 15点05分 3
level 6
seerliuliu 楼主
如果不用&引用,把函数改成void InitList(SqList* L)
编译没有错,但是在执行的时候有错,说使用了未初始化的指针。但是这个函数的功能就是要初始化,尴尬啊。
2012年05月26日 15点05分 4
level 6
seerliuliu 楼主
求好心人……
2012年05月26日 15点05分 5
level 8
把这句放main前面声明
2012年05月26日 16点05分 6
level 6
seerliuliu 楼主
what??放在main前面有用么??main是程序的入口啊
2012年05月27日 01点05分 7
level 14
void InitList(SqList** L)
{
*L=(SqList*)malloc(sizeof(SqList));
(*L)->length=0;
}
InitList(&L);
2012年05月27日 02点05分 9
level 6
seerliuliu 楼主
vc2008这样不行……
清华C语言版本的教材上是
void InitList(SqList* L)
{
L=(SqList*)malloc(sizeof(SqList));
L->length=0;
}
但没写出全过程,所以我不知道他外面是怎么写的……
2012年05月27日 02点05分 10
level 6
seerliuliu 楼主
求帮助
到现在还没调好……
2012年05月27日 02点05分 11
level 6
seerliuliu 楼主
用指向指针的指针调试成功了。但是还是无法按照教材上的函数调试成功。难道教材写错了??
2012年05月27日 03点05分 12
level 15
[礼物]
2012年05月27日 03点05分 13
level 15
破教材可以扔了。
2012年05月27日 03点05分 14
level 6
seerliuliu 楼主
我也觉着怪啊,函数要写成这样,外面的主函数我怎么写都不行啊……
2012年05月27日 03点05分 15
level 15
VS2008报什么错?
调用按⑨L的方法改成InitList(&L);了没有?
2012年05月27日 04点05分 16
level 7
一种方法:
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
char data[20];
int length;
}SqList;
SqList* InitList(void); //初始化线性表
int main()
{
SqList* L;
L = InitList();
return 0;
}
SqList* InitList(void) //初始化线性表
{
SqList *L;
L=(SqList*)malloc(sizeof(SqList));
L->length=0;
return L;
}
2012年05月27日 04点05分 17
level 7
另一种方法
#include<stdio.h>
#include<stdlib.h>
typedef struct
{
char data[20];
int length;
}SqList;
void InitList(SqList **pL) ;//初始化线性表
int main()
{
SqList* L;
InitList(&L);
return 0;
}
void InitList(SqList **pL) //初始化线性表
{
*pL=(SqList*)malloc(sizeof(SqList));
(*pL)->length=0;
}
2012年05月27日 04点05分 18
level 6
seerliuliu 楼主
使用了未初始化的指针。
2012年05月27日 06点05分 19
level 6
seerliuliu 楼主
这种方法容易理解一些,也的确可以。不过教材上这个鸟函数的返回值是void……
2012年05月27日 06点05分 20
level 8
你的教材,我建议直接烧了,然后再到吧里精品区选一本买回来!
2012年05月27日 15点05分 21
1 2 尾页