char * to int*
Question:
I have the following in my code:
char *someChars
int *someCharLength
I have a function which takes in :
int *myFuction(int *in, int inCount, int outCount);
It basically takes the bits and do a rot 13, how do I safely pass my char string in?
Solution:
Hmm…. that function makes more sense, but is there any chance “your bad” is bad? I would expect one of the following:
uint32 * myFuction( uint8 *in, uint32 inCount, uint8 **out );
uint32 myFuction( uint8 *in, uint32 inCount, uint8 ** out );
The first doesn’t make much sense, since the size should be returned simply as a uint32 rather than a uint32 *. The second one seems extremely likely.
I’m fairly certain your return must be a uint32 or uint32 *. Otherwise, your maximum byte count allowed on the return is 255 bytes, and since your input allows up to 4GB, nothing can compress your input to be that small.
Call me presumptious, but I’m going to give the examples assuming it returned either uint32 or uint32 *.
I’m going to start using unsigneds, to help avoid some of the typecasting.
If it returns a uint32*:
unsigned char * someChars;
unsigned someCharLength;
unsigned char * returnedChars;
unsigned * returnedLength;
and you would call the function as follows:
returnedLength = myFuction( someChars, someCharLength, &returnedChars );
then access the size of the out via *returnedLength
If it returns a uint32:
unsigned char * someChars;
unsigned someCharLength;
unsigned char * returnedChars;
unsigned returnedLength;
and you would call the function as follows:
returnedLength = myFuction( someChars, someCharLength, &returnedChars );
then access the size of the out via returnedLength
Note, this implies that the function creates new memory for the returned value. At some point, it is your responsibility to free the memory:
free( returnedChars );













Comments (0)
Trackbacks - Pingbacks (0)
Leave a Reply