[英]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.