I am making a console application in C using visualstudio 2015 so that a user can enter the amount of sweets they wish to make and the name of the sweet however there is a problem, the problem is that when the program tries to read a string from the array the program crashes and doesnt print out anything, however if I change it to print a single character using %c it will print out the first character of the string for example if I enter 2 sweets and the strings 'Jawbreaker' and 'Lollipop' It will crash if I use %s for the string however if I use %c for a character it will do its job and print 'J' and 'L' respectively on different lines, Any ideas how I could get this working with the %s specifier for the strings?
我在做一个控制台应用程序在C语言中使用visualstudio 2015,这样用户可以输入他们希望的糖果和甜的名字但是有一个问题,问题是,当程序试图读取一个字符串数组的程序崩溃,不打印任何东西,但是如果我改变它打印一个字符使用% c将打印字符串的第一个字符例如如果我输入2糖果和字符串“大块硬糖”和“棒棒糖”它会崩溃如果我使用% s然而如果我使用% c字符串的字符将完成其工作并打印“J”和“L”分别在不同的线,任何想法如何得到这个工作的% s说明符字符串?
The code is below:
下面的代码是:
#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;
}
2 个解决方案
#1
3
You have to use an 2-dimensional array. sweet_name
is an array(1-D), each index can store at most one character not a string. Change the following line
你必须使用二维数组。sweet_name是一个数组(1-D),每个索引最多只能存储一个字符而不是字符串。改变以下行
char sweet_name[999];
to
来
char sweet_name[999][100];
#2
1
OK, I would advice you doing something more efficient and that is to use a double pointer. By doing that, you can solve some problems that the 2D array version has.
The first problem is, what do you do if the user wants to insert more than 999 sweets. Your array can't hold them.
Second, what do you do if the user enters a name that is bigger than 100 characterrs. Again, your 2D array can't hold it. And also, although there is the possibility for the user to enter a name bigger than 100 characters, most users will enter much less than this and now for every string, you have allocated 100 positions when you probably only need about 50.
So, let's deal with these problems. I would probably do something like this:
好的,我建议你做一些更有效率的事情那就是使用双指针。通过这样做,您可以解决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;
}
Finally, now the program again has the limitation to read only names up to 300 characters because of reader
, but this is something that you can also deal with, and this is to allocate a crazy amount of memory for reader, like 1000000 characters and then free it.
最后,由于reader的原因,现在这个程序只能读取300个字符,但这也是你可以处理的,这是为了给reader分配大量的内存,比如1000000个字符,然后释放它。
#1
3
You have to use an 2-dimensional array. sweet_name
is an array(1-D), each index can store at most one character not a string. Change the following line
你必须使用二维数组。sweet_name是一个数组(1-D),每个索引最多只能存储一个字符而不是字符串。改变以下行
char sweet_name[999];
to
来
char sweet_name[999][100];
#2
1
OK, I would advice you doing something more efficient and that is to use a double pointer. By doing that, you can solve some problems that the 2D array version has.
The first problem is, what do you do if the user wants to insert more than 999 sweets. Your array can't hold them.
Second, what do you do if the user enters a name that is bigger than 100 characterrs. Again, your 2D array can't hold it. And also, although there is the possibility for the user to enter a name bigger than 100 characters, most users will enter much less than this and now for every string, you have allocated 100 positions when you probably only need about 50.
So, let's deal with these problems. I would probably do something like this:
好的,我建议你做一些更有效率的事情那就是使用双指针。通过这样做,您可以解决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;
}
Finally, now the program again has the limitation to read only names up to 300 characters because of reader
, but this is something that you can also deal with, and this is to allocate a crazy amount of memory for reader, like 1000000 characters and then free it.
最后,由于reader的原因,现在这个程序只能读取300个字符,但这也是你可以处理的,这是为了给reader分配大量的内存,比如1000000个字符,然后释放它。