我将特定的指针分配给另一个char序列(字符串)时遇到问题;
#include<stdio.h>
void setInformations(char *informations[], char *contests_name[]);
int main(void) {
char *informations[]={"","","",""};
char *contests_name[]={"cycle_race","swim","running race"};
int totalTime=0;
setInformations(informations,contests_name);
getch();
return 0;
}
void setInformations(char *informations[], char *contests_name[]) {
printf("Competitor's name: ");
scanf("%s",*informations[0]);
printf("%s",informations[0]);
}
我知道问题是将字符串分配给char类型的指针。我该如何替换它(strcpy?)
最佳答案
从scanf中取出*:
scanf ("%s", informations[0]);
...并为信息数组分配空间
char* informations[NUMBER_OF_INFORMATIONS][MAX_NAME_LENGTH] = {{'\0'}};
因此scanf将有空间放入信息。