Re: When can you assign one struct to another?

Discussion in 'C++' started by Victor Bazarov, Jan 29, 2014.

  1. 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
    Victor Bazarov, Jan 29, 2014
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,372
    Chris Fogelklou
    Apr 20, 2004
  2. Mark McIntyre

    C beginner: struct inside another struct...

    Mark McIntyre, Apr 21, 2006, in forum: C Programming
    Replies:
    5
    Views:
    667
    Yourko
    Apr 23, 2006
  3. Replies:
    18
    Views:
    7,079
    Karl Heinz Buchegger
    Jul 22, 2005
  4. asgars
    Replies:
    1
    Views:
    2,324
    Patrice
    May 11, 2010
  5. Alf P. Steinbach
    Replies:
    0
    Views:
    96
    Alf P. Steinbach
    Jan 29, 2014
Loading...

Share This Page