Jump to content
  • Advertisement
Sign in to follow this  
BrainlessDesigns

String arrays in c

This topic is 4850 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 ;) )

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!