passing character array to a function?!?! 1 error!! :(
hi all!
I've spent almost all day trying to get rid of one bloody error! arghhhh!!!!
#include cstring
#include stdlib.h
#include string.h
............
unsigned char *a, *b;
void get(unsigned char* hdata)
{
strcpy( b, hdata); //error!!!!! I am trying to copy hdata into b!!
}
void init(void)
{ .....
.....
a = LoadBitmapFile("image1.bmp", &bitmap1InfoHeader); //no errors
get(a); //no errors
.......etc..
}
the compiler gives me error:
error C2664: 'strcpy' : cannot convert parameter 1 from 'unsigned char *' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
Function "LoadBitmapFile" returns unsigned char* bitmap image data stores in the variable "a" which is passed to the "get" function in which I am trying to copy the whole array "a" into array "b" of same type! Am I doing something wrong??
Please help as I am already getting a headache!
Edited by - robert_s on September 3, 2001 12:49:19 PM
Edited by - robert_s on September 3, 2001 12:52:49 PM
Well, you could typecast the parameters to char*, i.e:
However, you probably shouldn't use strcpy at all. strcpy is only for copying actual string data, in your case the data is not a string even though it's an array of characters. You should use memcpy to copy arrays of arbitrary data. In your case:
This is necessary because strings in C/C++ are NUL-terminated character arrays, meaning that strcpy will only copy characters until it finds a character with ascii value zero (the end-of-string marker). In your case the bitmap data can contain zeros anywhere, therefore strcpy cannot be used.
Edited by - Dactylos on September 3, 2001 1:09:16 PM
strcpy( (char*)b, (char*)hdata);
However, you probably shouldn't use strcpy at all. strcpy is only for copying actual string data, in your case the data is not a string even though it's an array of characters. You should use memcpy to copy arrays of arbitrary data. In your case:
memcpy(b, hdata, number_of_chars_in_array_pointed_to_by_hdata);
This is necessary because strings in C/C++ are NUL-terminated character arrays, meaning that strcpy will only copy characters until it finds a character with ascii value zero (the end-of-string marker). In your case the bitmap data can contain zeros anywhere, therefore strcpy cannot be used.
Edited by - Dactylos on September 3, 2001 1:09:16 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement