我正在使用visualstudio 2015在C中制作一个控制台应用程序,以便用户可以输入他们想要制作的糖果量以及甜蜜的名称但是有问题,问题是当程序试图读取时程序崩溃并且不打印任何内容的数组中的字符串,但是如果我更改它以使用%c打印单个字符,它将打印出字符串的第一个字符,例如,如果我输入2个糖果和字符串&#39 ;碎石机&#39;和棒棒糖&#39;如果我使用%s作为字符串,它会崩溃但是如果我对字符使用%c它将完成它的工作并打印&#39; J&#39;和&#39; L&#39;分别在不同的行上,任何想法我怎么能用字符串的%s说明符来处理它?</ p>
代码如下:
#include <stdio.h>
/*Declares the variable needed*/
int sweet_number;
char sweet_name[999];
int main(void) {
/*Prompts the user to enter the number of sweets and saves it to sweet_number*/
printf("Please enter the number of sweets:\n");
scanf("%d", &sweet_number);
/*for loop to enter the name of the sweet into the array*/
for (int i = 0; sweet_number > i; i++) {
printf("What is the name of the sweet?\n");
scanf("%s", &sweet_name[i]);
}
/*Prints array to screen*/
for (int j = 0; sweet_number > j; j++){ /* <- This is where code fails to run*/
printf("%s\n", sweet_name[j]);
}
return 0;
}
答案 0 :(得分:3)
您必须使用二维数组。 sweet_name
是一个数组(1-D),每个索引最多可以存储一个字符而不是字符串。更改以下行
char sweet_name[999];
到
char sweet_name[999][100];
答案 1 :(得分:1)
好的,我会建议你做一些更高效的事情,那就是使用双指针。通过这样做,您可以解决2D阵列版本存在的一些问题
第一个问题是,如果用户想插入超过999个甜点,你会怎么做。你的阵列无法容纳它们
其次,如果用户输入的名称大于100个字符,您会怎么做。同样,您的2D阵列无法容纳它。而且,尽管用户可能输入的名称大于100个字符,但大多数用户输入的内容远远少于此字符,现在对于每个字符串,当您可能只需要大约50个字符时,您已分配了100个位置。
所以,让我们来处理这些问题。
我可能会做这样的事情:
#include <stdio.h>
#include <string.h> // for strcpy(), strlen()
#include <stdlib.h> // for malloc()
int main(void) {
char **sweet_names; // make a double pointer(or you might see that as array of pointers
char reader[300]; // this variable will help us read every name into the sweet_names
int sweet_number;
int i, j;
// Your code here to get the number of sweet_names
/*Prompts the user to enter the number of sweets and saves it to sweet_number*/
printf("Please enter the number of sweets:\n");
scanf("%d", &sweet_number);
// Now that you know the sweet_number, allocate as much memory as you need.
// And that can be more than 999 sweet names
sweet_names = (char **) malloc(sweet_number * sizeof(char *));
// i.e. make a character pointer to sweet_number character pointers.
// Again, some of your code here
for (i = 0; sweet_number > i; i++) {
printf("What is the name of the sweet?\n");
scanf("%s", reader); // read into the reader
sweet_names[i] = (char *) malloc(strlen(reader) + 1); // allocate as much memory as you need for the current string, the one just read into reader
strcpy(sweet_names[i], reader); // copy contents of reader to sweet_names[i]
}
// Again, your code to print the names
for (j = 0; sweet_number > j; j++){
printf("%s\n", sweet_names[j]);
free(sweet_names[j]); // free every string you allocated, it is good practice
}
// Finally, free the sweet_names pointers. Generally, when you allocate
// dynamically, which is what we do with malloc(), it is a good practice
// to free what you allocate becuase otherwise stays in memory and then
// memory leaks are created. There is a lot to say about C and memory
// but anyway, remember to free
free(sweet_names);
return 0;
}
最后,由于reader
,现在程序再次限制只能读取最多300个字符的名称,但这也是你可以处理的内容,这是为了分配一个疯狂的内存量读者,像1000000
字符,然后释放它。