Jump to content
  • Advertisement
Sign in to follow this  
tomba

indexing a 2d array with pointer?

This topic is 4608 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

i have an array called char command[4][10] = {{"ATTACK!"},{"MAGIC"},{"ITEM"},{"RUN"}} i need to pass this as a parameter to a funtion called draw that takes in any 2d char with any size. so i thought of using a pointer index . draw(command); (this is what i pass) to the function void draw(char *para){};( this function draws each string ) i though of using *para as a index for command[0] thur command[4] but i can't do a pointer indexing for a 2d array. so is there any solution to using a pointer as a index for 2d array? If not, is there any other way to pass a 2d array of any size as a parameter?

Share this post


Link to post
Share on other sites
Advertisement
have you tried char **para?

EDIT: and wouldn't command have to be {"ATTACK!","MAGIC","ITEM","RUN"}?

Share this post


Link to post
Share on other sites
The parameter to the function should be a double-pointer (char **para). Then, you can index into it normally (para[0] is the first string, and so forth).



~BenDilts( void );

Share this post


Link to post
Share on other sites
You either want void draw(char parameters[4 /* optional */][10]){} or, if this is C++, something like:
std::vector< std::string > commands;
commands.push_back("ATTACK!"};
commands.push_back("MAGIC"};
commands.push_back("ITEM"};
commands.push_back("RUN"};
// ...
void draw(std::vector< std::string > parameters){} // or pass by reference or const reference.
Enigma

Share this post


Link to post
Share on other sites

void Draw(char (*p)[10])
{

//p is a pointer to an array of 10 character elements

}


Now just treat p as you would in main();


xeddiex.

Share this post


Link to post
Share on other sites
If you aren't using strict C I would instead recommend using something like this:

string command[4] = {"ATTACK!", "MAGIC", "ITEM", "RUN"}

Strings are much easier to manipulate and can be converted back to C-style strings if with a call to command[n].c_str().

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by xeddiex

void Draw(char (*p)[10])
{

//p is a pointer to an array of 10 character elements

}


Another alternative is void Draw(char (&p)[10]);

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
Original post by xeddiex

void Draw(char (*p)[10])
{

//p is a pointer to an array of 10 character elements

}

Another alternative is void Draw(char (&p)[10]);


[oh] Another alternative is:


int main(int argc, char **argv)
{
char table[2][10] = {
"string1",
"string2"
};

unsigned int _uaddr = (char (*)[10])table;
Draw(_uaddr);
}

void Draw(unsigned int _upaddr)
{
printf("%s\n", ((char(*)[10])_upaddr)[0]);
}

output: string1

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by xeddiex
Quote:
Original post by Anonymous Poster
Quote:
Original post by xeddiex

void Draw(char (*p)[10])
{

//p is a pointer to an array of 10 character elements

}

Another alternative is void Draw(char (&p)[10]);


[oh] Another alternative is:


int main(int argc, char **argv)
{
char table[2][10] = {
"string1",
"string2"
};

unsigned int _uaddr = (char (*)[10])table;
Draw(_uaddr);
}

void Draw(unsigned int _upaddr)
{
printf("%s\n", ((char(*)[10])_upaddr)[0]);
}

output: string1

But then C-style casting is often frowned upon by the C++ community, especially the committee. Although, the OP could really be using C, thus making my previous post and those of other posters irrelevant. By the way, temporary variable is unnecessary:


int main(int argc, char **argv)
{
char table[2][10] = {
"string1",
"string2"
};
Draw((char (*)[10])table);
}

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!