Originally Posted by
HungryMan
Yes, a friend has to do it this way for homework and i want to help him
.
But i think there is also a good reason for this.
Because you want to store arbitrary many and different strings.
Just a thought... if you know less about C than your friend, are you really helping him?
In any case a linked list of individual characters is a patently bad idea. As has already been pointed out you will end up using between 3 and 5 times as much memory and you won't have a useable string in the entire mess.
In C a string is an array of characters, stored contiguously, ending in a null (0) character. All of C's library functions (strcpy(), strlen(), etc.) count on this to work... By storing the data in an unexpected way you totally trash any hope of using any of the standard library functions on your data.
You may want to look into using a standard char array as your input buffer then a linked list to store each string ... perhaps placing a line of text in each struct. At least then you would preserve the ability to use string functions on a line by line basis.
Code:
typedef struct tLines
{ char *Line;
void *Next }
Lines;
int main (void)
{ char Buffer[128]; // line buffer
Lines *first = NULL, *current= NULL;
While (fgets(Buffer,127,stdin))
{ // add new line;
current->Next = malloc(sizeof(lines));
current = current->Next;
current->Next = NULL;
current->Line = strdup(Buffer);
// diagnostic
printf("%s",current->Line); }
return 0; }
This is just an example of form, it won't compile, don't even bother trying... but I think you can get the idea from it.