How can I find the length of a string from an array of pointers to a string?
For example, if I want to find the length of the string "apple" then how can I calculate length of it? I tried many ways, but I couldn’t. If I do sizeof(str[0]), it returns the size of the pointer (4 bytes in my 32-bit device). Are they stored in the memory location next to each other or not?
const char *str[] = {
"apple", "ball", "cat", "dog", "mep", "helsdf"
};
Use strlen() from string.h
#include <stdio.h>
#include <string.h>
int main() {
const char *str[] = {
"apple", "ball", "cat", "dog", "mep", "helsdf"
};
printf("Length of \"%s\": %zu\n", str[0], strlen(str[0]));
return 0;
}