|
From: Carlos d. M. <cg...@wo...> - 2002-07-31 12:13:17
|
Do you mean having an array array of size n and call a function
function(array); and that function calculate the size in it?
I don't know about any "built-in" function for do so, but it could be don=
e
adding a special member which should be added as the last member, so you =
can
search it from the begin of the array, so you can consider the size as eq=
ual to
the location of that member plus one, or it has a size of that subindex o=
f
elements you mind plus one member you don't mind, and whenever you can ne=
ed
reallocate memory or adding members you must have space for that last, an=
d when
adding, start adding substituting that member. And then work considering =
the
matter of having one member which you don't use.
For example, if it is an array of chars, that member is evident, you only=
need
to search for '\0', but in chars* it is used, and has sense but in other =
cases
the equivalent you use not. You can have an array of positive values, so =
you
can use -1 as that member.
for(counter =3D 0; testmember !=3D itemextra; counter++){
itemextra =3D array[counter];
size =3D counter + 1;
}
[size++]
You can say this size++ or not depending on whether you want size mean si=
ze of
the whole array including the last member or not. Or you can reallocate =
to
adjust the size to the array you need without that member and then work w=
ith it
as if you didn't use that member, and at the end you can realloc and add =
it, or
whatever you need or want to do.
Ramana Kumar escribi=F3:
> thanks ioannis, i have briefly read your reply and it is what i had fea=
red.
> is there any way to implement what i had before so that the sizeof oper=
ator
> reports the size as if it were a local array? for example, can i pass t=
he
> array to the function by reference or dereference the pointer that is
> passed?
>
> -----Original Message-----
> From: Ioannis Vranos [mailto:no...@ho...]
> Sent: Wednesday, 31 July 2002 9:15 PM
> To: 'Ramana Kumar'
> Subject: RE: [Dev-C++] sizeof array
>
> sizeof() reports the total size of the array when it is used in the
> scope of its definition.
>
> When you use an array as an argument of a function in reality it is not
> an array but a pointer.
>
> consider the following example:
>
> void makea(char array[100])
> {
> while(*array)
> *array++=3D'a';
> }
>
> int main()
> {
> char somearray[500];
>
> for(unsigned long i=3D0; i<sizeof(somearray)-2; i++)
> somearray[i]=3D's';
>
> somearray[sizeof(somearray)-1]=3D'\0';
>
> makea(somearray);
> }
>
> void makea(char array[100]) is equivallent to void makea(char *array)
> and void makea(char array[])
>
> So sizeof(array) when applied inside the makea() is equivallent to
> sizeof(char *).
>
> array there is char *array.
>
> Ioannis
>
> * Ioannis Vranos
> * Programming pages: http://www.noicys.cjb.net
> * Alternative URL: http://run.to/noicys
>
> -------------------------------------------------------
> This sf.net email is sponsored by: Dice - The leading online job board
> for high-tech professionals. Search and apply for tech jobs today!
> http://seeker.dice.com/seeker.epl?rel_code=3D31
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|