简体   繁体   中英

C language printing strings from array in a loop

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?

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;

}

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

char sweet_name[999];

to

char sweet_name[999][100];

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:

#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM