Tuesday, 15 July 2014

c - Storing and retrieving strings in Multidimensional array -



c - Storing and retrieving strings in Multidimensional array -

i have next code stores string-input user n times in multidimensional array. , print out sec element.

main() { // array store 10 strings, 20 characters long. char strstorage[10][20]; printf("\nenter how many strings: "); scanf( "%d" , &num); fflush(stdin); ( count = 0 ; count < num ; count++) { printf("enter string: "); gets(strstorage[count]); fflush(stdin); } printf("%s", strstorage[2]);

last line prints out garbage. user-input not visible within garbage hence either element access wrong or storage wrong. can help me regards problem?

thanks in advance...

strstorage[2] third string, if num less 3, won't initialize , contain garbage.

c arrays string

No comments:

Post a Comment