473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

getting the size of a character array

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
Jul 23 '05 #1
6 25427
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 :)

Jul 23 '05 #2
just a loop such as:

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

or something similar

On Mon, 25 Apr 2005 14:59:46 -0700, block111 wrote:
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 :)


Jul 23 '05 #3
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

Jul 23 '05 #4
Dave Hague wrote:
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,
--
CrayzeeWulf
Jul 23 '05 #5

"Dave Hague" <ha***@cse.ohio-state.edu> wrote in message
news:pa****************************@cse.ohio-state.edu...
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.
Jul 23 '05 #6
"Dave Hague" <ha***@cse.ohio-state.edu> wrote in message
news:pa****************************@cse.ohio-state.edu...
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?
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Charles L | last post by:
I don't know if this is a stupid quesiton or not. I would like to know how to convert an array of characters generated from a previous operation to a string ie how do I append a null character at...
2
by: deanfamily | last post by:
I have a character array that I am using to get the user's input. I only want to get out of the array grouping symbols (,),{or}. Is there a way for me to do this? I am currently trying to use an...
4
by: deanfamily | last post by:
Is there a way to get a character array to accept spaces? The use of my program enters the input like this: (6 + 5). Is there a way to get the program to insert the space (" ") into the array?
12
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
6
by: Kannan | last post by:
Hi, I have question about character array initialization. In section 6.7.8 paragraph number 21, it's given that "If there are fewer initializers in a brace-enclosed list than there are...
3
by: roopa.v1 | last post by:
Hi, How to assign long to character array and later extract it
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
6
by: Spikey | last post by:
Hi, I have a problem that's (probably) very easy to solve, only I'am more a Java programmer and I couldn't find anything like it on the internet. My problem is that I have to add a variable (int,...
7
crystal2005
by: crystal2005 | last post by:
Hi everyone, I gonna ask how to set unlimited size of array of characters. According to the tutorial that i have found after amount of time of googling, array size should be defined when we...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.