Unsigned char * data as return type of a function in c++ or c -
this question has answer here:
i using visual studio 2010 clr(common language run time). want return unsigned char * compress function main function both unsigned char * , char * giving erro
"error 7 error c2440: '=' : cannot convert 'char *' 'unsigned char *' f:\4-2\thesis\project\database compression main\database compression 2\db_comp_main.cpp 21 "
unsigned char* compressor(char *data) { unsigned char *compressed_string; //with process had compressed string of data variable unsigned char* in variable compressed_string , printed here. want return here. return compressed_string; } int main() { unsigned char *main_data; main_data=compressor("muhammad ashikuzzaman.student khulna university of engineering , technology bangladesh");// when click error error list cursor put here beside str visual studio. printf("%s",main_data); }
need return unsigned char * type data compressor function. please help.
change of data type (function argument *data
or local variable *main_data
) match type. try this
unsigned char *main_data;
or....
simple type casting work make working anyway:
unsigned char* compressor(char *data) { unsigned char *compressed_string; compressed_string = (unsigned char*) data; return compressed_string; } int main() { char *main_data; main_data=(char*) compressor("muhammad ashikuzzaman.student khulna university of engineering , technology bangladesh"); printf("%s",main_data); }
or...
(changing char*
)
there 3 char types: (plain) char
, signed char
, unsigned char
. char
8-bit integer* (there's no dedicated "character type" in c language) , in sense, signed
, unsigned char
have useful meaning (generally equivalent uint8_t
, int8_t
). when used character in sense of text, use char
(also referred plain char). typically signed char
can implemented either way compiler. think using char*
safe here.
working example (considering char *
data type):
char* compressor(char *data) { char *compressed_string; compressed_string = "compressed"; // compressed_string = data; return compressed_string; } int main() { char *main_data; main_data=compressor("muhammad ashikuzzaman.student khulna university of engineering , technology bangladesh"); printf("%s",main_data); }
also, simplicity can use pointer pointer
void compressor(char **data) { *data = "compressed"; // assuming compressed data smaller in length of main data, buffer overflow won't occur } int main() { char *main_data = "muhammad ashikuzzaman.student khulna university of engineering , technology bangladesh"; compressor(&main_data); printf("%s", main_data); return 0; }
edit
as asked alternative pass reference, there no way except pass reference regarding passing array function argument. because when write void func(char str[])
may think you're performing pass value. g++ compiler optimize statement void func(char &str[0])
avoid coping whole array function parameter. in scenario, passing reference.
Comments
Post a Comment