Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

When can you assign one struct to another?

30 views
Skip to first unread message

Bint

unread,
Jan 28, 2014, 7:01:38 PM1/28/14
to
Hello, I have a struct, which includes another struct, like this:

typedef float CGFloat;

struct CGPoint {
CGFloat x;
CGFloat y;
};

struct LinePoint {
CGPoint pos;
float width;
};


Now, if I have a pointer to a bunch of LinePoints, can I directly assign one
of them with another LinePoint?

LinePoint P;
LinePoint *array;

*(array+i) = P;

Will it copy all of the fields, or not? Sometimes it seems like it does, and
other times no. I can't figure out what is going on.

Thanks
B'

Alf P. Steinbach

unread,
Jan 28, 2014, 7:30:37 PM1/28/14
to
On 29.01.2014 01:01, Bint wrote:
> Hello, I have a struct, which includes another struct, like this:
>
> typedef float CGFloat;
>
> struct CGPoint {
> CGFloat x;
> CGFloat y;
> };
>
> struct LinePoint {
> CGPoint pos;
> float width;
> };
>
>
> Now, if I have a pointer to a bunch of LinePoints, can I directly assign one
> of them with another LinePoint?

Yes.


> LinePoint P;
> LinePoint *array;
>
> *(array+i) = P;
>
> Will it copy all of the fields, or not?

Yes, it will copy all of the field.

In passing,

(1) the preferred array indexing notation is

array[i] = P;

(2) the floating point type of choice when there are no strong reasons
for anything else, is `double` (e.g. `3.14` is of type `double`), and

(3) it's a good idea to use std::vector instead of trying to implement
dynamic size arrays yourself, i.e.

LinePoint P;
std::vector<LinePoint> array;

// Add items to array, then
array[i] = P;


> Sometimes it seems like it does, and
> other times no. I can't figure out what is going on.

You may have double deallocation, uninitialized pointer, indexing beyond
the array end, messed up heap or stack, ...

However, it may help to narrow down the causes by using std::vector::at,

array.at( i ) = P;

because it throws an exception if `i` is not in range for the current
array size.


Cheers & hth.,

- Alf

Victor Bazarov

unread,
Jan 28, 2014, 7:31:12 PM1/28/14
to
On 1/28/2014 7:01 PM, Bint wrote:
> Hello, I have a struct, which includes another struct, like this:
>
> typedef float CGFloat;
>
> struct CGPoint {
> CGFloat x;
> CGFloat y;
> };
>
> struct LinePoint {
> CGPoint pos;
> float width;
> };
>
>
> Now, if I have a pointer to a bunch of LinePoints, can I directly assign one
> of them with another LinePoint?
>
> LinePoint P;
> LinePoint *array;
>
> *(array+i) = P;

Is there anything between those lines or are those it? I'm asking
because declaring 'array' a pointer to LinePoint and not assigning any
value to it (and not initializing it with anything valid) does not
certainly mean that you *have a pointer to _a bunch_ of LinePoints.

In fact it does not at all point to any LinePoint whatever, generally
speaking. It has a random value.

> Will it copy all of the fields, or not?

Yes, *if* 'array' points to the first valid object of type LinePoint and
there are at least 'i' of those in a row. If 'i' is such that you run
beyond the boundary of the array you have, the copying will not work
very well.

Post the code that shows how you make 'array' point to "a bunch of"
objects, and how you calculate 'i' before using the expression.

> Sometimes it seems like it does, and
> other times no. I can't figure out what is going on.

Distill your code to the bare minimum that seems to misbehave (or does
misbehave), then post it here entirely. For more information please see
section 5 of the FAQ Lite.

V
--
I do not respond to top-posted replies, please don't ask
0 new messages