adding array into array of strings in c.....
adding array into array of strings in c
Collapse
X
-
Tags: None
-
You can't add to an arrray in C. Arrays have a fixed size.
To add to an array of strings you need to create a new array that's bigger, copy the old array to it and then add the address of your added string.
Ugly.
Consider using a linked list.Comment
-
-
-
If you use a 2D array for strings:
then you are are limited to 100 strings and each string must be 19 characters or less.Code:char array[100][20];
That is, there are now two limits in your code.
Try to code so that your program does not have built-in limits.
Note also that you can't add a string to this array. All you can do is copy a string to this array and overwrite the one that's already in there. Plus, if the string copied is more than 19 characters, you will corrupt memory and crash your program.
Adding a string requires and array:
But now you have to copy the first array to this one and then copy the added string to this one. And you haven't added a string to an array. All you have done is create a larger array and populated it.Code:char array[101][20];
It boils down to the fact that you can't add elements to arrays. Therefore, you use a linked list instead.Comment
-
Comment