I must know array size to which a char pointer variable points.
But you know sizeof operation is not working.
char a[5];
char *b= a;
int c = sizeof(b);
printf("%d", sizeof(b)); // 8(64 bit cpu) printed, not 5 .
Thank you in advance.
I must know array size to which a char pointer variable points.
But you know sizeof operation is not working.
char a[5];
char *b= a;
int c = sizeof(b);
printf("%d", sizeof(b)); // 8(64 bit cpu) printed, not 5 .
Thank you in advance.
If some piece of code needs to know the size of an array, make sure the code that calls it tells it the size of the array.
No, once you convert it to a pointer (which is also done when passing the array as an argument to a function) you loose all size information about the array.
Pointers (which is what arrays decay to when you do things like these) don't "know" anything about the size of the data they're pointing at. You need to track it separately.
you cannot get that information, the only thing you could do is to embeddd the information if it is something you need:
int* array = malloc( sizeof(int) * (n + 1) );
*array = n;
++array;
then when you want to know the size
int n = *(array - 1);
same can be done with char or whatever type you need.
This may be because of of 4 byte alignment.
To confirm try a[8], a[10] etc