Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: When can you assign one struct to another?

Thread Tools

Re: When can you assign one struct to another?

Victor Bazarov
Posts: n/a
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.

I do not respond to top-posted replies, please don't ask
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: When can you assign one struct to another? Alf P. Steinbach C++ 0 01-29-2014 12:30 AM
Warning in python file when i m using pychecker. Avnesh Shakya Python 2 04-26-2013 06:57 AM
when I read gzipped response from web-servers, GzipReader returnssometimes 'invalid compressed data -- crc error' Ruby 0 04-08-2013 03:51 PM
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
assign a struct variable into another struct variable C++ 18 07-22-2005 04:08 PM