user-0 提问: 求助，报指针错误问题

回复:
user: user-0
content: 引用 1 楼 cyfcsd 的回复:好像没有指针错误，但那个jobs[i]是一个结构体，不能直接用%s输出的，得改一下，用循环或者一个函数输出吧  Job  jobs[]={ 	{1,"法师","biubiubiu专家"}, 	{2,"刺客","千年刺杀专家"}, 	{3,"战士","我的肉提别厚"}, 	{4,"机甲师","靠机甲吃饭"}, 	{5,"射手","打哪知哪"}, };        这里我用for(j=0;j<5;j++) { 	printf("%s\n",jobs[j]); } 想输出这样的效果不行么：程序在我输入后，就报内存错误了 1,"法师","biubiubiu专家" 2,"刺客","千年刺杀专家" 3,"战士","我的肉提别厚" 4,"机甲师","靠机甲吃饭" 5,"射手","打哪知哪"

user: user-0
content: 引用 5 楼 cfjtaishan 的回复:#include<stdio.h> #include<stdlib.h>  typedef struct _job {     int id;     char * name;     char * desc; }Job;  typedef struct _ability {     int id;     char * name;     char * intro; }Ability;  typedef struct _pubTime {     int year;     int month;     int day; }PubTime;  typedef struct _hero {     int id;     char * name;     char sex;     Job job;     double hp;     double speed;     Ability abillity[5];     PubTime pubTime; }Hero;  void InputHero();  Hero heros[5]; Job  jobs[]={     {1,"法师","biubiubiu专家"},     {2,"刺客","刺杀专家"},     {3,"战士","我的肉提别厚"},     {4,"机甲师","靠机甲吃饭"},     {5,"射手","射得准"}, };  void InputHero() {     int i,j;     for(i=0;i<5;i++)     {         int jobchoice;         printf("第%d位英雄的信息：\n",i+1);         heros[i].id = i+1;         printf("英雄名称为：");         heros[i].name = (char*)malloc(50);         scanf("%s",heros[i].name);         getchar();/* add getchar() here */         printf("英雄性别为：");         scanf("%c",&heros[i].sex);         fflush(stdin);           for(j = 0; j < 5; j++)         {             printf("%s\n",jobs[j].name); /* error, jobs[j].name*/         }         printf("请选择职业：(1-5之间的整数)");         scanf("%d", &jobchoice);         heros[i].job = jobs[jobchoice - 1];         printf("%s当前职业为：《%s》\n",heros[i].name, heros[i].job.name); /* error job.name*/     } }  int main(int argc, char *argv[]) {     InputHero();      return 0; } 有几处错误和不合理的地方，现已修改，可以测试一下，试试！  问题解决，感谢！

user: user-1
content: #include<stdio.h> #include<stdlib.h>  typedef struct _job {     int id;     char * name;     char * desc; }Job;  typedef struct _ability {     int id;     char * name;     char * intro; }Ability;  typedef struct _pubTime {     int year;     int month;     int day; }PubTime;  typedef struct _hero {     int id;     char * name;     char sex;     Job job;     double hp;     double speed;     Ability abillity[5];     PubTime pubTime; }Hero;  void InputHero();  Hero heros[5]; Job  jobs[]={     {1,"法师","biubiubiu专家"},     {2,"刺客","刺杀专家"},     {3,"战士","我的肉提别厚"},     {4,"机甲师","靠机甲吃饭"},     {5,"射手","射得准"}, };  void InputHero() {     int i,j;     for(i=0;i<5;i++)     {         int jobchoice;         printf("第%d位英雄的信息：\n",i+1);         heros[i].id = i+1;         printf("英雄名称为：");         heros[i].name = (char*)malloc(50);         scanf("%s",heros[i].name);         getchar();/* add getchar() here */         printf("英雄性别为：");         scanf("%c",&heros[i].sex);         fflush(stdin);           for(j = 0; j < 5; j++)         {             printf("%s\n",jobs[j].name); /* error, jobs[j].name*/         }         printf("请选择职业：(1-5之间的整数)");         scanf("%d", &jobchoice);         heros[i].job = jobs[jobchoice - 1];         printf("%s当前职业为：《%s》\n",heros[i].name, heros[i].job.name); /* error job.name*/     } }  int main(int argc, char *argv[]) {     InputHero();      return 0; } 有几处错误和不合理的地方，现已修改，可以测试一下，试试！

user: user-2
content: 引用 3 楼 qq_37837743 的回复:[quote=引用 1 楼 cyfcsd 的回复:] 好像没有指针错误，但那个jobs[i]是一个结构体，不能直接用%s输出的，得改一下，用循环或者一个函数输出吧  Job  jobs[]={ 	{1,"法师","biubiubiu专家"}, 	{2,"刺客","千年刺杀专家"}, 	{3,"战士","我的肉提别厚"}, 	{4,"机甲师","靠机甲吃饭"}, 	{5,"射手","打哪知哪"}, };        这里我用for(j=0;j<5;j++) { 	printf("%s\n",jobs[j]); } 想输出这样的效果不行么：程序在我输入后，就报内存错误了 1,"法师","biubiubiu专家" 2,"刺客","千年刺杀专家" 3,"战士","我的肉提别厚" 4,"机甲师","靠机甲吃饭" 5,"射手","打哪知哪"[/quote] #include<stdio.h> #include<stdlib.h> typedef struct _job {     int id;     char * name;     char * desc; }Job; typedef struct _ability {     int id;     char * name;     char * intro; }Ability; typedef struct _pubTime {     int year;     int month;     int day; }PubTime; typedef struct _hero {     int id;     char * name;     char sex;     Job job;     double hp;     double speed;     Ability abillity[5];     PubTime pubTime; }Hero; void InputHero(); Hero heros[5]; Job  jobs[]={     {1,"法师","biubiubiu专家"},     {2,"刺客","刺杀专家"},     {3,"战士","我的肉提别厚"},     {4,"机甲师","靠机甲吃饭"},     {5,"射手","射得准"}, }; void InputHero() {     int i,j;     for(i=0;i<5;i++)     {         int jobchoice;         printf("第%d位英雄的信息：\n",i+1);         heros[i].id = i+1;         printf("英雄名称为：");         heros[i].name = (char*)malloc(50);         scanf("%s",heros[i].name);         printf("英雄性别为：");         scanf("%c",&heros[i].sex);         fflush(stdin);                  for(j=0;j<5;j++)         {             printf("%d %s %s\n",jobs[i].id, jobs[i].name, jobs[i].desc);         }         printf("请选择职业：(1-5之间的整数)");         scanf("%d",&jobchoice);         heros[i].job = jobs[jobchoice - 1];         printf("%s当前职业为：《%s》\n",heros[i].name,heros[i].job.name);     } }  int main() { InputHero();  return 0; }

user: user-3
content: 下面两个name少写了。 for(j=0;j<5;j++)  {         printf("%s\n",jobs[j].name);  } printf("请选择职业：(1-5之间的整数)"); scanf("%d",&jobchoice); heros[i].job = jobs[jobchoice - 1].name;

user: user-2
content: 好像没有指针错误，但那个jobs[i]是一个结构体，不能直接用%s输出的，得改一下，用循环或者一个函数输出吧