在C中修改全局结构变量

时间:2015-10-08 18:28:16

标签: 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;
}

1 个答案:

答案 0 :(得分:1)

好吧......这是正确的代码......

#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. 不要忘记...在结构定义之后你应该使用(;)
  2. modif_value不返回任何内容,因此在原型中写入VOID
  3. 你应该将地址传递给modif_value函数!不是人物!