c中不能用引用的办法

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 '->'

原文地址:https://www.cnblogs.com/youxin/p/2205386.html