String arrays in c

Started by
4 comments, last by BrainlessDesigns 18 years, 8 months ago
hi there, i am not a newbie in programming but one thing always confuses me. 2d string arrays... i know that something like this char stringArray[numberOfStrings][maxLengthOfString + 1]; reserves space for numberOfString strings with the length of maxLengthOfString. fine. i want to do: const char *words[NUMWORDS] = { "Word_1", "Word_22", "Word_39", ... "Word_X" }; question1: is now automatically enough memory reserved for the strings, in cause of the pointer or have i to do: const char words[NUMWORDS][MAX_WORD_LENGTH + 1] = { "Word_1", "Word_22", "Word_39", ... "Word_X" }; question2: how can i pass this array to a function and can work with it? my idea is: #define 2ND_WORD 1 void myFunction(char *stringArray[],NUMWORDS) { char *myArrayPointer[]; /*can i store it here to a pointer?*/ /*i.e.*/ strcpy(myWord,myArrayPointer[2ND_WORD]); } please give me help - i don't know which is the right way ***i need the array outside in a header file and want to access it via pointer to that array from a class*** many thx 4 ur answers alex
_________________BR4iNL355 D35iGN5_________________
Advertisement
Answer to question 2:

I believe what you are actually creating is a double pointer (const char**). An array of pointers, pointing to an array of pointers. Try that.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Answer to Q.1:
Yes.
From C++ Tutorial:
Quote:As in the case of arrays, the compiler allows the special case that we want to initialize the content at which the pointer points with constants at the same moment as declaring the variable pointer:

char * terry = "hello";

in this case static storage is reserved for containing "hello" and a pointer to the first char of this memory block (that corresponds to 'h') is assigned to terry.


Answer to Q.2:
What's wrong with your idea? you passed a pointer to strcpy.

pex.

[Edited by - pex22 on August 9, 2005 6:20:02 PM]
pex.
all i want is an access to an string array outside of the header file. i want to pass only one pointer, which is able to access every string in cause of small main memory . i try to program the gameboy advance - i have a dictionary which contains up to 60000 words which are max. 13 characters long.

i tried:

const char *dictwords[] = {
"Word1",
"Word2",
...
"WordX"
};

class CDictionary
{
private:
char **words;
public:
void init(char *words[],u16 numWords);
};

void CDictionary::init(char *words[],u16 numWords)
{
/*create pointer to string array*/
this->words = words;

for(u16 i=0; i < numWords; i++)
printf("Word from Dictionary: %s\n",this->words);
}

does this work???

Rattrap
thx 4 your fast reply
greets alex
_________________BR4iNL355 D35iGN5_________________
Yep. What you have is an array of const char*, rather than a const pointer to char[] - thus, it decays to char** when passed, and all is well.

In the static storage of the app, you will have all the string literals stored somewhere, and at runtime, the array will be filled with pointers to the string literals.

So basically you are on the right track. (The hard part comes when you have to actually manipulate some "string" data ;) )
thx zahlman! i found a solution by myself:

const char dictwords[NUM_WORDS][MAX_WORD_LENGTH + 1] = {
"WORD1"
"WORD666"
...
"WORD911"
};

function call:
--------------
CDictionary dict;

dict.init&(dictwords[0][0],numWords);


void CDictionary::init(const char *words,u32 numWords)
{
const char *pActWord; /*pointer to actual word in dict*/

pActWord = words;

/*access every word in dictionary*/
for(u32 i=0L; i < numWords; i++)
{
pActWord += i * (MAX_WORD_LENGTH + 1);
printf("Word[%d] = \"%s\"",i,pActWord);
}
}


it's not very elegant, but works for me. zahlman:
yes the std::string class is much better than using char, but i programmed mostly in c, so i often mix c with c++ ...

greetz
alex
_________________BR4iNL355 D35iGN5_________________

This topic is closed to new replies.

Advertisement