char a[255];
void test(char* a){
printf("%d %d %d \n",sizeof(a),sizeof(*a),sizeof(&a));
}
all of those don't get the real size of variable 'a';
char a[255];
void test(char* a){
printf("%d %d %d \n",sizeof(a),sizeof(*a),sizeof(&a));
}
all of those don't get the real size of variable 'a';
You cannot. You will have to pass the size as an additional parameter. Probably of type unsigned int
because a size cannot possibly be negative.
void test(char* a, unsigned int size)
{
// ...
}
Although it's the same end result, it would be even better to make your second argument a size_t, because that's the convention.
void test(char* a, size_t size)
{
// ...
}
You should pass it explicitly along with pointer to the first element.
char a[255];
void test(char* a, size_t num_elem){
/* do smth */
}
int main() {
test(a, sizeof a / sizeof a[0]);
return 0;
}
And sizeof a / sizeof a[0]
gives you a number of elements inside array, note that this trick won't work properly with pointers but only with arrays.