const char *array[] = {"ax","bo","cf"};
tried
printf("size of array = %lu\n", sizeof(const char*));
result != 3
also
printf("size of array = %lu\n", sizeof(array));
result != **DESIRED ANSWER** = 4
NOTE... I have read related questions on here but none had a relation with my question......
sizeof(char *) is the size of the pointer, so normally 4 for 32-bit machine, and 8 for 64-bit machine.
sizeof(char) is 1. Not because a char has one byte (8 bits), but because that's what the standard defines.
Using C library function strlen() method: The C library function size_t strlen(const char *str) computes the length of the string str up to, but not including the terminating null character.
To get the size of a const char
pointer:`
printf("%zu\n", sizeof(const char *));
To get the size of the array array[]
:
const char *array[] = {"ax","bo","cf"};
printf("%zu\n", sizeof array);
To get the number of elements in the array array[]
, divide the size of the array by the size of an array element.
const char *array[] = {"ax","bo","cf"};
// Size of array/size of array element
printf("%zu\n", sizeof array / sizeof array[0]);
// expect 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With