麻烦问下大佬们我这段程序哪有问题,编译没有报错,但是输出的是乱码。
#include<stdio.h>
#include<string.h>
struct x
{
char name[10];
int age;
char address[10];
char sex[10];
}student1={"星哥",20,"天水","女"};
int main()
{
struct x student1;
printf("%s\n",student1.name);
return 0;
}
你在定义x结构体的同时,已经定义了一个x型的全局变量student1并赋了初值,然而,你的main函数里又定义了同名的局部变量student1并没有赋初值。main函数执行时,这里的student1是未赋初值的,所以student1.name的值并不是"星哥",你以%s输出它,自然会显示为“乱码”。
改正方法是,既然你已经定义了全局变量,main函数里就不要再定义了。
#include <stdio.h>运行结果