C语言中函数数值传递问题

YOURFISH 2008-05-02 09:00:19
最近做一些题的时候,总是很疑惑一些问题,实在想不通了,希望知道的大虾,不吝赐教下
帮我看看我在代码中的注解,有空的话,帮我回答下,谢谢

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define FAIL 0;
#define OK 1;

typedef struct Node{
int count;
char *word;
struct Node *lchild,*rchild;
}Node,*BitTree;
//BitTree 去定义一个变量时,那个变量就是一个指针变量吧,
//例如 BitTree bt; 效果等同 Node *bt吧


int SearchBS(BitTree root,char *keyword,BitTree *p)
{
BitTree pre;
*p=NULL;
pre=root;
while(pre)
{
if(strcmp(pre->word,keyword)==0)
{
*p=pre;
return OK;
}
else
{
*p=pre;
pre=(strcmp(pre->word,keyword)>0 ? pre->lchild : pre->rchild);
}
}
return FAIL;
}
//BitTree定义出来的变量就是个指针变量
//为什么还要定义成指向指针的指针变量呢?
//如果定义为int InsertBS(BitTree root,char *keyword)而不是下面这种
//不是可以通过地址传值吗,来改变Main中的root变量,何必还要用指向指针的指针变量了,实在想不通
//我也测试过了,如果定义为上面那种就出错.
int InsertBS(BitTree *root,char *keyword)
{
BitTree p,s;

if(SearchBS(*root,keyword,&p)==0)
{
s=(Node *)malloc(sizeof(Node));
if(!s) return FAIL;
s->word=(char *)malloc(strlen(keyword));
strcpy(s->word,keyword);
s->count=1;
s->lchild=NULL;
s->rchild=NULL;
if(p==NULL) *root=s;
else
{
if(strcmp(p->word,keyword)>0) p->lchild=s;
else
p->rchild=s;
}
}
else
p->count++;

return OK;

}

void InorderSort(BitTree root)
{
if(root==NULL) return;
InorderSort(root->lchild );
printf("%s(%d)",root->word,root->count);
InorderSort(root->rchild );
}
void main()
{
char ch,keyword[40]={0},buf[40]={0};
int i=0,tag=0;
BitTree root=NULL;
FILE *file;
file=fopen("in.txt","r");
if(!file) printf("Open file Failse!!!!");
while(!feof(file))
{
ch=fgetc(file);
if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))
{
buf[i++]=ch;
tag=1;
}
else if(tag==1)
{

buf[i]='\0';
strcpy(keyword,buf);
strcpy(buf,"");
i=0;
tag=0;
if(InsertBS(&root,keyword)==0) return;
}
}
fclose(file);
InorderSort(root);
}


我在网上查了下,
有一篇文章说C语言中函数传值方式有三种
1,值传递
2,地址值传递
3,引用传递
上面两种,我知道
但是第三种却是第一次听到
于是把他文章里面关于引用传递的例子
拿来做测试在TC2.0,VC6.0中,均有报错,不知道是不是真的有引用传递
例子代码如下:

#include<stdio.h>
void change(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("X:%d,Y:%d",x,y);
}
void main()
{
int a=4,b=6;
change(a,b);
printf("a:%d,b:%d",a,b);
getch();
}

...全文
192 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Treazy 2008-05-03
  • 打赏
  • 举报
回复
c中有引用吗?

你看的什么文章?

值,指针,引用这3种传递方式

最后一种在c中是不存在的
gezichong 2008-05-03
  • 打赏
  • 举报
回复
如果用.cpp文件,则是正确的.
gezichong 2008-05-03
  • 打赏
  • 举报
回复
引用是C++引入的新语言特性,是C++常用的一个重要内容之一,正确、灵活地使用引用,可以使程序简洁、高效。

TC中指出的错误是
Error E:\TURBOC2\TEMP.C 3: Declaration syntax error

是C文件.C语言中没有引用.
YOURFISH 2008-05-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 chlaws 的回复:]
我在网上查了下,
有一篇文章说C语言中函数传值方式有三种
1,值传递
2,地址值传递
3,引用传递 //引用是在C++中的,C中没有引用



C/C++ code
//这代码没报错正确的,只不过少了个头文件
//#include<conio.h>
#include<stdio.h>
void change(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("X:%d,Y:%d",x,y);
}
void main()
{
int a=4,b=6;
c…
[/Quote]
不会吧,我用TC2.0VC6.0把你的代码进行重新编译,错误还是和原来的一个样呀
TC中指出的错误是
Error E:\TURBOC2\TEMP.C 3: Declaration syntax error
VC6.0中指出错误是
E:\C\temp.c(3) : error C2143: syntax error : missing ')' before '&'
E:\C\temp.c(3) : error C2143: syntax error : missing '{' before '&'
E:\C\temp.c(3) : error C2059: syntax error : '&'
E:\C\temp.c(3) : error C2059: syntax error : ')'
E:\C\temp.c(13) : warning C4047: 'initializing' : 'int *' differs in levels of indirection from 'const int '
E:\C\temp.c(13) : warning C4047: 'initializing' : 'int *' differs in levels of indirection from 'const int '
E:\C\temp.c(14) : warning C4013: 'change' undefined; assuming extern returning int
Error executing cl.exe.
l999358 2008-05-03
  • 打赏
  • 举报
回复
引用其实就是给变量起另外一个名字,说穿了就是逗自己玩的,嘿嘿.
chlaws 2008-05-03
  • 打赏
  • 举报
回复
我在网上查了下,
有一篇文章说C语言中函数传值方式有三种
1,值传递
2,地址值传递
3,引用传递 //引用是在C++中的,C中没有引用




//这代码没报错正确的,只不过少了个头文件
//#include<conio.h>
#include<stdio.h>
void change(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("X:%d,Y:%d",x,y);
}
void main()
{
int a=4,b=6;
change(a,b);
printf("a:%d,b:%d",a,b);
getch();
}


chenzhp 2008-05-03
  • 打赏
  • 举报
回复
//BitTree定义出来的变量就是个指针变量
//为什么还要定义成指向指针的指针变量呢?
//如果定义为int InsertBS(BitTree root,char *keyword)而不是下面这种
//不是可以通过地址传值吗,来改变Main中的root变量,何必还要用指向指针的指针变量了,实在想不通
//我也测试过了,如果定义为上面那种就出错.

因为在函数InsertBS(BitTree *root,char *keyword)
中,s分配了地址,并且将s的地址赋值给了root,如果不用指向指针的指针,那么root的内容(也就是地址)是不能改变的,只是改变root的一个副本。所以要利用二维指针来修改地址。
我是风 2008-05-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 brookmill 的回复:]
引用传递是C++的
[/Quote]
blh 2008-05-02
  • 打赏
  • 举报
回复
[Quote=引用楼主 YOURFISH 的帖子:]
最近做一些题的时候,总是很疑惑一些问题,实在想不通了,希望知道的大虾,不吝赐教下
帮我看看我在代码中的注解,有空的话,帮我回答下,谢谢
C/C++ code#include<stdio.h>#include<stdlib.h>#include<string.h>#defineFAIL 0;#defineOK 1;typedefstructNode{intcount;char*word;structNode*lchild,*rchild;
}Node,*BitTree;//BitTree 去定义一个变量时,那个变量就是一个指针变量吧,//例如 BitTree bt; 效果等同 Node *bt吧…
[/Quote]

网上的文章不一定都准确。。。。
引用传递实际上类似于指针,是C++引入的新语法
brookmill 2008-05-02
  • 打赏
  • 举报
回复
引用传递是C++的

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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