Thread: Creating an array of Strings

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Question Creating an array of Strings

    Hi,

    In a program I'm writing I am attempting to create an array of Strings which is populated with values from a for loop.

    I've created the toy program below to illustrate the issue I'm having:

    Code:
    void fillPArray() {
      char *pArray[10];
    
      int i;
    
      for (i=0;i < 10;i++) {
        char aString[10];
        sprintf(aString, "%d", i);
        pArray[i] = aString;
      }
    
      for (i=0;i < 10;i++) {
        printf("%s\n", pArray[i]);
      }
    }
    I intended the above code to create a new String for every element of the char pointer array and to print out the values 0 ... 9. Currently it prints out all 9's.

    Could it be that the char array aString is being reinitialised on each entry of the loop, allocating the same memory; hence making all the pointers point to the same memory location?

    Whatever the issue is I can't figure out how to handle this properly.

    Any idea what's going wrong here?

    Thanks,

    Danny

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Vegenad View Post
    Could it be that the char array aString is being reinitialised on each entry of the loop, allocating the same memory; hence making all the pointers point to the same memory location?
    Yep! that is exactly what's goin' on.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Vegenad View Post
    Whatever the issue is I can't figure out how to handle this properly.
    Either malloc() new memory for each string (remember to free() it later) or
    Code:
    char aString[10][10];
    sprintf(aString[i], "%d", i);
    pArray[i] = aString[i];
    if you don't mind that the strings gets lost when aString goes out of scope.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Ah, wonderful. I didn't even consider explicit memory allocation. I come from a Java background and hence know nothing .

    Thank you very much for your help .

    I've pasted the (I think) working code below just in case it's any help to anybody passing through this thread:

    Code:
    void fillPArray() {
      char *pArray[10];
    
      int i;
    
      for (i=0;i < 10;i++) {
        pArray[i] = malloc(sizeof(char)*3);
        sprintf(pArray[i], "%d", i);
      }
    
      for (i=0;i < 10;i++) {
        printf("%s\n", pArray[i]);
        free(pArray[i]);
      }
    }

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Just one minor thing to note about your code. But maybe you are already aware of it.
    Code:
    pArray[i] = malloc(sizeof(char)*3);
    3 bytes is not enough to hold the string representation of the maximum value of an int. In this case it is safe to use 3 bytes as long as i never goes above 99 but remember to increase the size if you ever increase the max count of the loop.
    I am note sure, but I think a 32 bit int can hold up to 9 digits in base10.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Ah yes, good point. I'll bear that in mind.

    Cheers ,

    Danny

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating instances of objects in an array?
    By stumon in forum C# Programming
    Replies: 5
    Last Post: 10-27-2009, 07:37 PM
  2. 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
  3. creating an array of strings
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2004, 07:40 AM
  4. remove strings from array
    By ipe in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 04:53 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM