c中不能用引用的办法
2011-10-10 12:42 youxin 阅读(682) 评论(0) 收藏 举报c语言不能用引用,可以用指针来代替,sample程序如下,
c++程序
#include<iostream>
using namespace std;
typedef struct Str{
int i;
int j;
}str;
int giveinfo(str &str);
int main(){
str str;
giveinfo(str);
str.i = 89;
printf("%d\n",str.i);
printf("%d\n",str.j);
return 0;
}
int giveinfo(str &str){
str.i = 9;
str.j = 20;
return 0;
}
函数参数改成指针,调用函数的参数要取地址,才能相对应,
c语言
#include<stdio.h>
#include<stdlib.h>
typedef struct Str{
int i;
int j;
}str;
int giveinfo(str *str);
int main(){
str str;
giveinfo(&str);
str.i = 89;
printf("%d\n",str.i);
printf("%d\n",str.j);
return 0;
}
int giveinfo(str *str){
str->i= 9;
str->j = 20;
return 0;
}
不知道str.i=9,str.j会提示 left operand points to 'struct', use '->'
浙公网安备 33010602011771号