所以我的问题在这里:
char *input;
char *takenpositions[18] ={"A0","A0","A0" /* etc. */};
int k;
for(k = 0; k < 18; k++) {
scanf("%s",&input);
/* ...
The program is doing other things with input here, then i want to put it
into the array in place of the A0s. I tried strncpy, and other things but
maybe i couldn't use it correctly.
...
*/
takenpositions[k] = input;
}
我无法找到答案,因为它太容易了,或者我只是跛脚。
答案 0 :(得分:1)
正如我在评论中提到的,你需要为&#34;输入&#34;分配内存。可能这就是你要做的事情。
#define MAX_STR_LEN 256
char *input;
char *takenpositions[18] ={0}; //Initialize all pointers to NULL (0).
int k;
for(k = 0; k < 18; k++) {
input = malloc(sizeof(char)*(MAX_STR_LEN+1)); //Allocate memory
char scanfString[32] = ""; //32 characters should be sufficient for scanf string.
//To limit number of character inputs use string "%<limit>s" in scanf()
sprintf(scanfString, "%%%us", MAX_STR_LEN);
scanf(scanfString, input);
/*
Your code.
*/
takenpositions[k] = input; //Save pointer.
}