char[32] as function parameter

Started by
12 comments, last by rip-off 8 years, 5 months ago

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

Advertisement


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

I don't know what you mean. https://goo.gl/C6QczF

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

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.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Don't report your post for deletion -- even if it's just a small mistake, you're solution might benefit future members with similar problems.

- Jason Astle-Adams

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

Or you could just a std::string and avoid the hassle :)

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

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.

Previously "Krohm"

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.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

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.

This topic is closed to new replies.

Advertisement