[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-devel
Subject: Re: General C++ Question about arrays
From: "David S. Chappell" <David.Chappell () mail ! cc ! trincoll ! edu>
Date: 1999-08-25 14:14:38
[Download RAW message or body]
At 03:23 PM 8/25/99 +0200, you wrote:
>On Wed, 25 Aug 1999, Markus Goetz wrote:
>
>> Hi,
>> i hope i can ask a general c++ question here and you won't kill me :-)
>>
>> Ok, i have an constant array declared with
>> "const static char* myarray[] = { "one", "two", "kde rulez", "billy
sucks" };"
>>
>> Now i want to go to the array from element 0 to the last element. I am
having
>> another int constant that has the number of elements.
>>
>> But i don't want to do this, i want to go from 0 to the last element and
not to
>> 0 to myconstant.
>>
>> In pascal i would do this with "for a := 0 to high(myarray) do .." but
in C++ ?
>
>AFAIK there is no way to do this, as C(++) doesn't no about array sizes...
>
>Peter
Actually the compiler knows about array sizes. It is just that there is no
run time bounds checking. In contrast, pointers don't have a size in the
sense that arrays do. I think this is the code you want:
for(i=0; i < (sizeof(myarray) / sizeof(const char *)); i++)
{
}
If you tried this with a pointer, it would only do one element.
================================================================
David Chappell David.Chappell@Mail.Trincoll.Edu
Computing Center PostMaster@Mail.Trincoll.Edu
Trinity College (860) 297-2114
Hartford, Connecticut 06106
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic