For a char []
, I can easily get its length by:
char a[] = \"aaaaa\";
int length = sizeof(a)/sizeof(char); // length=6
However,
Strlen command is working for me . You can try following code.
// char *s
unsigned int strLength=strlen(s);
This may sound Evil™ and I haven't tested it, but how about initializing all values in an array at allocation to '\0'
and then using strlen()
? This would give you your so-called real value since it would stop counting at the first '\0'
it encounters.
Well, now that I think about it though, please don't Ever™ do this. Unless, you want to land in a pile of dirty memory.
Also, for the allocated memory or the total memory you may use the following functions if your environment provides them:
You can make a back-tracker character, ex, you could append any special character say "%" to the end of your string and then check the occurrence of that character.
But this is a very risky way as that character can be in other places also in the char*
char* stringVar = new char[4] ;
stringVar[0] = 'H' ;
stringVar[1] = 'E' ;
stringVar[2] = '$' ; // back-tracker character.
int i = 0 ;
while(1)
{
if (stringVar[i] == '$')
break ;
i++ ;
}
// i is the length of the string.
// you need to make sure, that there is no other $ in the char*
Otherwise define a custom structure to keep track of length and allocate memory.
You can find the length of a char* string like this:
char* mystring = "Hello World";
int length = sprintf(mystring, "%s", mystring);
sprintf() prints mystring onto itself, and returns the number of characters printed.
You could try this:
int lengthChar(const char* chararray) {
int n = 0;
while(chararray[n] != '\0')
n ++;
return n;
}
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...