how to find the end of (const * float)?
Author Message
how to find the end of (const * float)?

I was given a const float *, but I dont know how long is it. I would
regroup the array as (x1, y1), (x2, y2), (..)...

void regroupv(const float *); //I need complete the function
//inside, but can not change
//the prototype.
const float *v; //I dont know how do they define
//pointer v, assume this is right
int main(void)
{
regroupv(v)

Quote:
}

The point is how I know the length of the pointer, i.e. how many
element inside? If v is defined as an array, we can use sizeof(v)/4.

void regroupv(const float *v)
{
float *dup_v;
memcpy(dup_v, v, sizeof(v)) //sizeof(v) seems wrong
while ((*v) != NULL)
{........
}

Quote:
}

Could anybody give me some suggestions? Thank you very much.

Regards,

Linrm

Sent via Deja.com http://www.*-*-*.com/

Sat, 24 Aug 2002 03:00:00 GMT
how to find the end of (const * float)?

Quote:

> I was given a const float *, but I dont know how long is it. I would
> regroup the array as (x1, y1), (x2, y2), (..)...

> void regroupv(const float *); //I need complete the function
>                               //inside, but can not change
>                               //the prototype.
> const float *v; //I dont know how do they define
>                 //pointer v, assume this is right
> int main(void)
> {
>  regroupv(v)
> }

> The point is how I know the length of the pointer, i.e. how many
> element inside? If v is defined as an array, we can use sizeof(v)/4.

Not really. You could use sizeof(v)/sizeof(float) (btw, one pair of
those parens is superfluous, but I keep forgetting which, so I always
put both in :-)),  because sizeof(float) is not always 4.
However, since you're passing this array to a function, and since such
passed arrays decay into pointers, you can't do that. Yes, this is
unfortunate. It is also inevitable: you try passing a, say, 20000-byte
entity on some computers. Agh.

Quote:
> void regroupv(const float *v)
> {
>   float *dup_v;
>   memcpy(dup_v, v, sizeof(v)) //sizeof(v) seems wrong
>   while ((*v) != NULL)
>   {........
>   }

> }

> Could anybody give me some suggestions? Thank you very much.

You _have_ to pass the size of the array somehow, or have a marker value
at the end of it that tells you that it is the end (similar to the 0 at
the end of strings). There is no way of finding the size of a passed
array - which is now a pointer, not an array any more - inside a
function, without some help from the outside.
However, since you seem (in the above example) to be able to rely on the
last element of *v being 0 (_Warning!_ *v _cannot_ be NULL. NULL is a
pointer. *v is a float. compare *v to 0, or better, to 0.0. Be aware of
pitfalls in comparing floats directly. In this case, I'd be rather angry
if a directly assigned 0.0 did not compare equal to a directly compared
0.0 (0.0 being special and all), but you never know.) you can count the
number of elements in v that are not 0, malloc() an area of memory that
size plus one (for the 0.0), and then memcpy your floats.

Richard

Sat, 24 Aug 2002 03:00:00 GMT

 Page 1 of 1 [ 2 post ]

Relevant Pages