How do you create an arrays of strings in C++

Started by
2 comments, last by Dark Star 23 years, 11 months ago
Hi everyone, I just wanted to know how to create an array of strings like an array of floats or integers in C++. I read somewhere that it is done like this: char *stringArray[30]; but I have tried this and sometimes when I write a string to one element in the array, the string gets placed into all the other elements in some mysterious way. I have checked my code and cannot find any reason why writing a string to one specific array element would result in it getting written to the other array elements too. Also how I go about declaring an array of strings of the length 80 characters. For example I would like to create an array of strings that holds up tp 40 strings each of the length of 256 characters. Any help on this subject would be great. Thanks in advance. Dark Star.
---------------------------------------------You Only Live Once - Don't be afriad to take chances.
Advertisement
char arrayOfStrings[40][256];

holds 40 strings
each string up to 256 chars long


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
quote:

char *stringArray[30];

but I have tried this and sometimes when I write a string to one element in the array, the string gets placed into all the other elements in some mysterious way. I have checked my code and cannot find any reason why writing a string to one specific array element would result in it getting written to the other array elements too.



With this, you have declared an array of 30 pointers to array of characters (that can eventually be interpreted as a string). You have not allocated the physical space where the characters will be stored. That is, if do something like

    strcpy(stringArray[0], "Hello, World!"); 


... you''ll probably end with storing the string in a memory chunk your application does not own. Result: General Protection Fault or Segmentation Violation.

You have to allocate the memory for the string, for example

    stringArray[0] = strdup("Hello, World!"); 


allocates the space for the string and fill it with the passed one. It''s almost the same as

    stringArray[0] = calloc(sizeof(char), strlen("Hello, World!"));    strcpy(stringArray[0], "Hello, World!"); 


Remember to free the memory when finished

    free(stringArray[0]); 


or you cause a lot of memory leaks.

quote:

Also how I go about declaring an array of strings of the length 80 characters. For example I would like to create an array of strings that holds up tp 40 strings each of the length of 256 characters.



You can allocate it statically, i.e.

    char pcStrings[40][257]; 


the you can do things like

    strncpy(pcStrings[0], "Hello, World!", 256); 


Note the use of strncpy that enables to control the max length of the copied string. If the string is longer than 256 characters and strncpy "cut" it you have to put the null-terminator

    pcStrings[0][256] = ''\0''; 



Hope it helps.


Karmalaa

---[home page] [[email=karmalaa@inwind.it]e-mail[/email]]
And just to finish up the lesson

Totally dynamic:

char** szStrings = NULL;

szStrings = new char[40];

for ( int c = 0; c < 40; c++ )
szStrings[c] = new char[256];

for ( c = 0; c < 40; c++ )
delete [] szStrings[c];

delete [] szStrings;


Edited by - mitchw on June 7, 2000 11:01:43 AM

This topic is closed to new replies.

Advertisement