我试图以几种方式从函数返回字符串,但是没有成功。 每次我遇到相同的错误。 谢谢您的帮助。 代码:
strcpy(temp,get_name());
printf("%s\n",temp);
scanf("%d",&i);
}
free(name_list);/*free the allocation of memory*/
return 0;
}
char *get_name()
{
int i=0;
char the_chosen_name[MAX_STRING_SIZE];
int rand_name = rand() % NUMber_OF_STRING;
while (name_list[rand_name][i] != '\0')
the_chosen_name[i] = name_list[rand_name][i];
the_chosen_name[i] = '\0';
//strcpy(the_chosen_name,name_list[rand_name]);
return the_chosen_name;
}
编辑: 用另一种方式: option3 WITHOUT A MORE VAR
name_list是字符串的全局数组
谢谢