Copying into an array of strings

This is a discussion on Copying into an array of strings within the C Programming forums, part of the General Programming Boards category; The following function is supposed to read the words in a file (separated by LFs) into an array of string: ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    3

    Copying into an array of strings

    The following function is supposed to read the words in a file (separated by LFs) into an array of string:

    Code:
    int fLoadWords(char *file, char **array) {
    
      FILE *fp;
      char string[WORDSIZE];
      char *p;
      int i=0;
    
      fp = fopen(file, "r");
    
      while(fgets(string, WORDSIZE, fp) != NULL) {
              if ((p = strchr(string, '\n')) != NULL)
                            *p = '\0';
              strcpy(array[i], string);
              i++;
      }
      for (i=0; i < WORDS; i++)
            printf("************* Words: %i:%s ************\n", i,array[i]);
    
      return 0;
    }
    But it causes me to coredump at the strcpy.

    The structure passed into array is defined as:
    Code:
    strWords = (char **) calloc (WORDS, sizeof (char *));
    ...
    iErrCode = fLoadWords(strFile, strWords);
    I thought about trying:
    Code:
    array[i] = string;
    instead of the strcpy, but that doesn't work because it just points all the pointers to string.

    Also, when I have a bunch of lines like:
    Code:
    array[0]='word1';
    array[1]='word2';
    etc...
    it works fine, so that makes me think the struct is fine.

    Help is appreciated!!

    M.E.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by mettle
    The structure passed into array is defined as:
    Code:
    strWords = (char **) calloc (WORDS, sizeof (char *));
    ...
    iErrCode = fLoadWords(strFile, strWords);
    Could you show a little more of the '...'?
    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
    Jun 2006
    Posts
    3

    More of the code

    I was asked to include more of what was omitted.
    There isn't much relevant stuff there, but:
    Code:
    char **strWords;
    char strFile[30];
    float x;
    
    int i,j=0;
    int iErrCode = 0;
    int iTokSum = 1;
    int iPrev = -1;
    int iWordSlot = 0;
    int iWordsIn = 0;
    int iToken[WORDS];
    
    strWords = (char **) calloc (WORDS, sizeof (char *));
    printf("ARGS: %s %s %s\n", argv[1], argv[2], argv[3]);
    strcpy(strFile, argv[1]);
    WORDS = atoi(argv[2]);
    MAXTOKENS = atoi(argv[3]);
    
    /* Load Words from file */
    iErrCode = fLoadWords(strFile, strWords);
    In the header file:
    Code:
    int WORDS = 16;
    int WORDSIZE = 12;
    int fLoadWords(char *file, char **array);
    Hope that helps!!

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    The problem is that a char pointer is not a string. You allocate an array of char pointers, which are all set to null by calloc(). Then you strcopy() string to that null pointer.

    What you need to do is allocate memory for each word like this:
    Code:
    for(i=0;i<WORDS;i++)
         strWords[i]=malloc(MAX_WORD_SIZE);
    Or you can allocate the memmory for the string inside fLoadWords like this:
    Code:
    ...
      while(fgets(string, WORDSIZE, fp) != NULL) {
              if ((p = strchr(string, '\n')) != NULL)
                            *p = '\0';
              array[i]=malloc(strlen(string)+1);
              strcpy(array[i], string);
              i++;
      }
    ...
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    3
    Makes perfect sense! Thank you!

    So when I did array[0]="word", it worked because no memory allocation was necessary; I was just directing the pointer to a memory block already allocated for storing "word"?

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Quote Originally Posted by mettle
    Makes perfect sense! Thank you!

    So when I did array[0]="word", it worked because no memory allocation was necessary; I was just directing the pointer to a memory block already allocated for storing "word"?
    Yep. Literal strings are just cost char *'s.

    But because literal strings are in read only memmory, you would not be able to change the string, exept to make it point to something else.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 01:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 11:56 AM
  3. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  4. Passing an Array of Strings from VB to a C DLL
    By mr_nice! in forum Windows Programming
    Replies: 9
    Last Post: 03-08-2005, 06:16 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21