Thread: string arrays

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    19

    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

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    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...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM