Thread: String array question

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    25

    String array question

    How would I initialize this

    char **stringArray

    I am trying to figure out how to initialize it with my firts and last name, but everytime I try it won't work. I am assuming that char **stringArray is a pointer to a pointer of strings. If that is the case I shoudl be able to store different strings in there, or even prompt a user for the number of strings and size of the strings and use malloc to grab that memory so I cna manipulate it. But I guess my trouble is trying to figure out how to initialize and manipulate char **stringArray

    Thanks,
    Chris

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    /* pseudocode */
    static char **AllocateTwoD ( size_t x, size_t y )
    {
      char **a;
      size_t row;
      if ( ( a = malloc ( x * sizeof *a ) ) != NULL ) {
        for ( row = 0; row < x; row++ ) {
          if ( ( a[row] = malloc ( y * sizeof *a[row] ) ) == NULL ) {
            for ( row = 0; row < x; row++ )
              if ( a[row] != NULL ) free ( a[row] );
            return a;
          }
        }
      }
      return a;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How would I initialize this

    char **stringArray
    Oh that's easy:

    char ** stringArray = NULL;

    There, now it's initialized...

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >There, now it's initialized...
    Your method is a lot less complicated than mine quzah, maybe I should adopt it.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. two dimensional string array question
    By Hoser83 in forum C Programming
    Replies: 8
    Last Post: 02-07-2006, 08:15 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM