c++ - error: cannot convert 'unsigned char*' to 'const int*' -
i'm writing function accept both unsigned char* , int*:
int foo(const int * a) however when pass unsigned char * foo(), has compiling error. thought unsigned char automatically casted int? ( smaller range variable casted larger one). work array pointer also?
sample code:
#include <iostream> using namespace std; int average(const int * array, int size) { int sum = 0; (int = 0; <size; i++) sum+=*(array+i); return sum/size; } int main() { unsigned char array[5] = {0,1,2,3,4}; std::cout << average(array, 5); return 0; } what best way write function accepts both unsigned char* , int*?
i thought unsigned char automatically casted int?
an unsigned char automatically cast int unsigned char* not automatically cast int*.
unsigned char c = 'a'; unsigned char* cp = &a; int = c; // allowed int* ip = cp; // not allowed if allowed, do:
*ip = int_max; but there isn't enough space @ &c hold number. end accessing unauthorized memory, lead undefined behavior.
Comments
Post a Comment