getting the size of a character array

B

block111

assuming that you know with what character your array ends all you need
is to check character by character until you find the terminating
character and then calculate the size of the array...
in case your caracter array is a simple c-string and it terminates with
'\0' the easiest way is to use strlen(your_char_array);
i thought about just making a quick loop but

Interesting, what's the quick loop you are talking about? I think they
all are quite quick :)
 
B

block111

if your array of things is not a simple cstring and it doesn't finish
with '\0' then
thing* begin = &array[0];
while(*begin != end_thing) ++begin;
cout << "array has " << (begin-&array[0]) << " things.";

where end_thing is the value of thing by which you identify the next
after the last element in you array of things
 
D

Dave Hague

whats the fastest way to get the size of a character array? i thought
about just making a quick loop but 1) i dont know if thats the best way to
do it and 2) im not sure what my loop condition would be

thanks
 
D

Dave Hague

just a loop such as:

for(int i = 0; not the end of the array; i++)
{
char x = array;
}

or something similar
 
C

CrayzeeWulf

Dave said:
whats the fastest way to get the size of a character array?

If it is really declared as a fixed sized array and not dynamically
allocated:

#include <iostream>

int
main()
{
char my_array[] = "blah blah" ;
const int MY_ARRAY_SIZE = sizeof(my_array)/sizeof(my_array[0]) ;
std::cout << "My array size: " << MY_ARRAY_SIZE << std::endl ;
return 0 ;
}

Notice that this will print:

My array size: 10

because the size of the array is 10 while that of the string "blah blah" is
9.

Thanks,
 
W

Winbatch

Dave Hague said:
whats the fastest way to get the size of a character array? i thought
about just making a quick loop but 1) i dont know if thats the best way to
do it and 2) im not sure what my loop condition would be

thanks

If you're not storing any binary data, then it should already be null
terminated and you should be able to do a simple strlen( ) on it.
 
A

Andrew Koenig

whats the fastest way to get the size of a character array?

Remember what the size was when you created it.

How about telling us a little more about the problem you're trying to solve?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,202
Latest member
MikoOslo

Latest Threads

Top