Jump to content
  • Advertisement
Sign in to follow this  
_WeirdCat_

char[32] as function parameter

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

so how to pass char[32] as func param? i mean char p[32];

 

void func(char * param); doesnt seem to work when used like func(p); or func(&p);

 

 

i rather not to have func(char param[32]); thats the main reason i ask

Share this post


Link to post
Share on other sites
Advertisement

original function had two parameters i didint set that second one that gave me the error but was a strange error related to char * thing, i reported the post already but there were some issues with server ill try to report this post again

Share this post


Link to post
Share on other sites

The syntax for array passing is crazy in C++ but this will do allow you to do this:

template <size_t charCount>
void stringCopy(char (&output)[charCount], const char* source)
{
}

I highly recommend you use a template on the array parameter here because this function now works with any sized arrays.

 

Share this post


Link to post
Share on other sites

ok so

 

inline int find_object_index(char * a, int len)
{


for (int i=0; i < OBJECT_LIST.len; i++)
if (nameIsEqual(a, OBJECT_LIST.container[i]->lname, len)) return i;


return -1;//not found
}
int obji = find_object_index(object[i].lname, 32);

where char lname[32];

 

didin't add , 32 as second parameter... end of story ;x

Share this post


Link to post
Share on other sites

Have you considered std::array<type, N>?

The main advantage is that it contains its own length and you can use it in range-for.

Unfortunately, being an owning object it causes you to shuffle memory around... the template version sort of solves the problem but I'm personally not a fan of that syntax.

 

 

Note: it is a very different thing from std::string.

Share this post


Link to post
Share on other sites

Have you considered std::array<type, N>?

The main advantage is that it contains its own length and you can use it in range-for.

Unfortunately, being an owning object it causes you to shuffle memory around... the template version sort of solves the problem but I'm personally not a fan of that syntax.

 

 

Note: it is a very different thing from std::string.

I agree the syntax for passing an array in C++ is crazy, the template only makes it so you can pass any size instead of for a fixed sized version of that function.

Share this post


Link to post
Share on other sites

 

void func(char * param); doesnt seem to work when used like func(p);

This is just a correct syntax, your wording "doesnt seem to work" implies a lot though. Without better describing your failure ....

 

int obji = find_object_index(object[i].lname, 32);

where char lname[32];

Always provide correct explicit length for examined string, not its allocated size, if you provide some constant allocated size instead of the real string value length, you will get bogus checks that will never equal to anything likely. You need to implement a length detection function, because even the iterated strings you examine against, will need to provide their length to check for comparement correctly. Or, you can make your implementation to not iterate a string in an issued explicit way, and instead initialize \0 byte behind explicit bytes, and always do a conditional check against this fact when comparing strings, or doing other operations.

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!