-
string arrays
I am having trouble writing input from stdin to an array of strings in C. Forgive me if this is trivial but I really hate dealing with strings, they seem easy but never work for me. Here is some code fragments
Code:
char *buf, **data;
int i=0;
data = calloc( ELEM_INIT, CHARS_INIT );
buf = malloc( MAXBUF );
assert( buf != NULL && data != NULL);
while( fgets( buf, MAXBUF-1, stdin ) != NULL && i < ELEM_INIT)
{
strcpy( data[i], buf );
i++;
}
I get seg faults in the strcpy call. I have tried using memcpy/memmove with n=strlen(buf) but the same problem occurs. Any hints?
Thanks
-
-
Thankyou so much, you have no idea how much this was @%*#ing me :)
It all works now, guess I need to learn a bit more about malloc and pointers, maybe another day...