修改C语言中的全局结构变量



使用全局变量,printf不返回有效值。我尝试了这段代码,没有成功:

struct test {
   char *a;
   char *b;
}    
struct test test_main = {};
int modif_value(char *val) {
   test_main.a = val;
}
int main () {
   modif_value("1");
   printf ("value after modif is %s n", test_main.a);
   return 0;
}

嗯…这是正确的代码…

#include <stdio.h>
struct test {
char *a;
char *b;
};
struct test test_main;
void modif_value(char *val) {
test_main.a = val;
}
int main () {
char c='1';
modif_value(&c);
printf ("value after modif is %s n", test_main.a);
return 0;
}
  1. 别忘了…在struct定义之后,你应该使用(;)
  2. modif_value没有返回任何东西,所以在原型
  3. 中写入VOID
  4. 你应该传递一个地址给modif_value函数!不是角色!

最新更新