|
From: Ramana K. <ra...@my...> - 2002-07-31 12:36:05
|
thank you carlos
i had in fact thought of using a special array member at the end, and thank
you for illustrating implentation of this so beautifully.
i'm afraid this may have to be the way i do it in the end, unless i simply
don't pass the array and declare it locally.
but since the array is in fact a two-dimensional array full of instances of
my own String class, adding a "terminating character" will involve creating
too much arbitrary code for my liking. I am still dreaming of the
possibility of sizeof-ing the array with the built in operator, if anyone
knows how to do this correctly. In fact, i am highly curious about the
workings of the sizeof operator. I realize that since passing an array to a
function is actually passing a pointer to the first object ( is this x[0] or
x[0][0] in a multidimensional array?), the sizeof operator gets the size of
that pointer... or does it? what are you passing the operator when the array
is local?
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...]On Behalf Of Carlos García
del Monte
Sent: Wednesday, 31 July 2002 10:09 PM
To: dev-cpp-users-list
Subject: Re: [Dev-C++] sizeof array
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 done
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 equal
to the location of that member plus one, or it has a size of that subindex
of elements you mind plus one member you don't mind, and whenever you can
need reallocate memory or adding members you must have space for that last,
and 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 = 0; testmember != itemextra; counter++){
itemextra = array[counter];
size = counter + 1;
}
[size++]
You can say this size++ or not depending on whether you want size mean
size 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 with 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ó:
thanks ioannis, i have briefly read your reply and it is what i had
feared.
is there any way to implement what i had before so that the sizeof
operator
reports the size as if it were a local array? for example, can i pass
the
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++='a';
}
int main()
{
char somearray[500];
for(unsigned long i=0; i<sizeof(somearray)-2; i++)
somearray[i]='s';
somearray[sizeof(somearray)-1]='\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=31
_______________________________________________
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
|