Passing an int to a function that expects char*

I want to pass an int to a function that expects a char *. Programming in Linux, so can't use itoa.
The function header looks like this:

void swapEndian(char *pElement, unsigned int uNumBytes)

and calling the function with an int (I know, this won't work..)
int numLine;
swapEndian(numLine, sizeof(int));

So what must I do to numline to pass it to this function?

Last edited on
Try the following

swapEndian( reinterpret_cast<char *>( &numLine ), sizeof(int));
reinterpret_cast is C++. Maybe this was not the correct forum to ask the question as I'm programming C, but if anyone can give me a C solution, will be appreciated.
In C there's only one cast expression, so that's what you'd use:

    int numLine;
    swapEndian((char*)&numLine, sizeof numLine);

Note that, since you're on Linux, you can deal with the endian-ness issues using the POSIX functions htonl and ntohl: instead of writing your own.
If your function requires a character string, then you could use sprintf() to make the int into a string.
Topic archived. No new replies allowed.