[英]C language printing strings from array in a loop
我正在使用visualstudio 2015在C中创建一个控制台应用程序,以便用户可以输入他们想要制作的糖果的数量以及糖果的名称,但是有一个问题,问题是当程序尝试从中读取字符串时程序崩溃的数组并且不打印任何内容,但是,如果我将其更改为使用%c打印单个字符,它将打印出字符串的第一个字符,例如,如果我输入2个sweets以及字符串“ Jawbreaker”和“ Lollipop” '如果我将%s用作字符串,它将崩溃,但是如果我将%c用作字符,它将完成其工作并分别在不同的行上打印'J'和'L'。 %s字符串说明符?
代码如下:
#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;
}
您必须使用二维数组。 sweet_name
是一个数组(1-D),每个索引最多可以存储一个字符而不是字符串。 更改以下行
char sweet_name[999];
至
char sweet_name[999][100];
好的,我建议您做一些更有效的事情,那就是使用双指针。 通过这样做,您可以解决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个字符,然后释放它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.