For a char []
, I can easily get its length by:
char a[] = "aaaaa"; int length = sizeof(a)/sizeof(char); // length=6
However, I cannot do like this to get the length of a char *
by:
char *a = new char[10]; int length = sizeof(a)/sizeof(char);
because, I know, a
here is a pointer, such that length
here will be always be 4
(or something other in different systems).
My question is that how can I get the length of a char *
afterwards? I know someone may challenge me that you already know its 10
because you just created it. I want to know this because this step of getting its length may come long long way from its creation and I don't want to come long long way back to check this number. Moreover, I also want to know its real length.
To be more specific
length=5
?length=10
?for the following example:
char *a = new char[10]; strcpy(a, "hello");
first, the char variable is defined in charType and the char array in arr. Then, the size of the char variable is calculated using sizeof() operator. Then the size of the char array is find by dividing the size of the complete array by the size of the first variable.
Use the strlen Function to Find Length of Char Array In some scenarios, char arrays that were initialized or stored as the null-terminated character strings can be measured for size using the strlen function, which is part of the C standard library string utilities.
The strlen() Function The strlen() accepts an argument of type pointer to char or (char*) , so you can either pass a string literal or an array of characters. It returns the number of characters in the string excluding the null character '\0' .
You can't. Not with 100% accuracy, anyway. The pointer has no length/size but its own. All it does is point to a particular place in memory that holds a char. If that char is part of a string, then you can use strlen
to determine what chars follow the one currently being pointed to, but that doesn't mean the array in your case is that big.
Basically:
A pointer is not an array, so it doesn't need to know what the size of the array is. A pointer can point to a single value, so a pointer can exist without there even being an array. It doesn't even care where the memory it points to is situated (Read only, heap or stack... doesn't matter). A pointer doesn't have a length other than itself. A pointer just is...
Consider this:
char beep = '\a'; void alert_user(const char *msg, char *signal); //for some reason alert_user("Hear my super-awsome noise!", &beep); //passing pointer to single char! void alert_user(const char *msg, char *signal) { printf("%s%c\n", msg, *signal); }
A pointer can be a single char, as well as the beginning, end or middle of an array...
Think of chars as structs. You sometimes allocate a single struct on the heap. That, too, creates a pointer without an array.
Using only a pointer, to determine how big an array it is pointing to is impossible. The closest you can get to it is using calloc
and counting the number of consecutive \0 chars you can find through the pointer. Of course, that doesn't work once you've assigned/reassigned stuff to that array's keys and it also fails if the memory just outside of the array happens to hold \0
, too. So using this method is unreliable, dangerous and just generally silly. Don't. Do. It.
Another analogy:
Think of a pointer as a road sign, it points to Town X. The sign doesn't know what that town looks like, and it doesn't know or care (or can care) who lives there. It's job is to tell you where to find Town X. It can only tell you how far that town is, but not how big it is. That information is deemed irrelevant for road-signs. That's something that you can only find out by looking at the town itself, not at the road-signs that are pointing you in its direction
So, using a pointer the only thing you can do is:
char a_str[] = "hello";//{h,e,l,l,o,\0} char *arr_ptr = &a_str[0]; printf("Get length of string -> %d\n", strlen(arr_ptr));
But this, of course, only works if the array/string is \0-terminated.
As an aside:
int length = sizeof(a)/sizeof(char);//sizeof char is guaranteed 1, so sizeof(a) is enough
is actually assigning size_t
(the return type of sizeof
) to an int
, best write:
size_t length = sizeof(a)/sizeof(*a);//best use ptr's type -> good habit
Since size_t
is an unsigned type, if sizeof
returns bigger values, the value of length
might be something you didn't expect...
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