If I build a function with a introduced pointer like this:
int* c=new int[16];
And return it
return c;
How can I determine the size of c, (16), in my main(). I can't use sizeof because c isn't an array...
If I build a function with a introduced pointer like this:
int* c=new int[16];
And return it
return c;
How can I determine the size of c, (16), in my main(). I can't use sizeof because c isn't an array...
Since c
is a pointer to int (that's what int* c
means), what you get from sizeof(c)
is exactly the size of the pointer to int. That is why sizeof(c)/sizeof(int*)
gives you 1.
If you define c
as array, not the pointer:
int c[16];
you'll get its size.
You can't get number of elements in dynamically allocated array. It would work in this case:
int c[16];
int num_elements=sizeof(c)/sizeof(int);
In your case sizeof(c)
is probably 4 (size of pointer).