assigning values to structure members

Discussion in 'C Programming' started by Pushkar Pradhan, Nov 16, 2003.

  1. I have a struct like this:
    struct point {
    int x;
    int y;
    };

    Then I need to assign a certain point "=" another pt.
    eg.
    struct point p1, pArr[10];

    /* initialize all pts. */
    Now assign p1 to a certain element of pArr, this is how I did and it
    works fine.
    p1 = pArr[someIndex];

    However, I just read my C book again and it says this stuff must be done
    like this actually:
    p1.x = parr[someIndex].x;
    p1.y = parr[someIndex].y;

    Can anybody tell if I should continue with the former code or should I
    make changes?
    Pushkar Pradhan
     
    Pushkar Pradhan, Nov 16, 2003
    #1
    1. Advertising

  2. On Sun, 16 Nov 2003 00:12:05 -0600, Pushkar Pradhan wrote:

    > I have a struct like this:
    > struct point {
    > int x;
    > int y;
    > };
    >
    > Then I need to assign a certain point "=" another pt.
    > eg.
    > struct point p1, pArr[10];
    >
    > /* initialize all pts. */
    > Now assign p1 to a certain element of pArr, this is how I did and it
    > works fine.
    > p1 = pArr[someIndex];
    >
    > However, I just read my C book again and it says this stuff must be done
    > like this actually:
    > p1.x = parr[someIndex].x;
    > p1.y = parr[someIndex].y;


    Please tell us the name of the C book so that we can all avoid it.
    What you have done is fine.
     
    Sheldon Simms, Nov 16, 2003
    #2
    1. Advertising

  3. Sheldon Simms wrote:

    > On Sun, 16 Nov 2003 00:12:05 -0600, Pushkar Pradhan wrote:
    >
    >> I have a struct like this:
    >> struct point {
    >> int x;
    >> int y;
    >> };
    >>
    >> Then I need to assign a certain point "=" another pt.
    >> eg.
    >> struct point p1, pArr[10];
    >>
    >> /* initialize all pts. */
    >> Now assign p1 to a certain element of pArr, this is how I did and it
    >> works fine.
    >> p1 = pArr[someIndex];
    >>
    >> However, I just read my C book again and it says this stuff must be done
    >> like this actually:
    >> p1.x = parr[someIndex].x;
    >> p1.y = parr[someIndex].y;

    >
    > Please tell us the name of the C book so that we can all avoid it.


    Careful. Maybe he has misread the book. (This has happened before in clc -
    someone has said "my book says <foo>" when in fact it says <not foo>).

    > What you have done is fine.


    Agreed.

    --
    Richard Heathfield :
    "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    K&R answers, C books, etc: http://users.powernet.co.uk/eton
     
    Richard Heathfield, Nov 16, 2003
    #3
  4. "Pushkar Pradhan" <> schrieb im Newsbeitrag
    news:...
    > I have a struct like this:
    > struct point {
    > int x;
    > int y;
    > };
    >
    > Then I need to assign a certain point "=" another pt.
    > eg.
    > struct point p1, pArr[10];
    >
    > /* initialize all pts. */
    > Now assign p1 to a certain element of pArr, this is how I did and it

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    > works fine.
    > p1 = pArr[someIndex];



    No :)
    pArr[someIndex] = p1;
    at least your text says so (if my english did not get into the way :).

    >
    > However, I just read my C book again and it says this stuff must be done
    > like this actually:
    > p1.x = parr[someIndex].x;
    > p1.y = parr[someIndex].y;
    >
    > Can anybody tell if I should continue with the former code or should I
    > make changes?


    The assignment is fine (in both directions)

    Robert
     
    Robert Stankowic, Nov 16, 2003
    #4
  5. Pushkar Pradhan

    CBFalconer Guest

    Richard Heathfield wrote:
    > Sheldon Simms wrote:
    > > On Sun, 16 Nov 2003 00:12:05 -0600, Pushkar Pradhan wrote:
    > >
    > >> I have a struct like this:
    > >> struct point {
    > >> int x;
    > >> int y;
    > >> };
    > >>
    > >> Then I need to assign a certain point "=" another pt.
    > >> eg.
    > >> struct point p1, pArr[10];
    > >>
    > >> /* initialize all pts. */
    > >> Now assign p1 to a certain element of pArr, this is how I did
    > >> and it works fine.
    > >> p1 = pArr[someIndex];
    > >>
    > >> However, I just read my C book again and it says this stuff
    > >> must be done like this actually:
    > >> p1.x = parr[someIndex].x;
    > >> p1.y = parr[someIndex].y;

    > >
    > > Please tell us the name of the C book so that we can all
    > > avoid it.

    >
    > Careful. Maybe he has misread the book. (This has happened
    > before in clc - someone has said "my book says <foo>" when in
    > fact it says <not foo>).
    >
    > > What you have done is fine.

    >
    > Agreed.


    He MAY have misread a section to do with detecting equality.
    Statements such as:

    if (p1 == pArr[ix]) {/* whatever */}

    are not defined, and must be broken up into tests on the
    individual components. Again, the name of the book would be
    worthwhile.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Nov 16, 2003
    #5
  6. The book is:
    The C Programming Language by KERNIGHAN & RICHIE.
    Ok I don't think they explicitly said that, I just concluded it from
    this code in the book:
    /* addpoint: add two points */
    struct point addpoint(struct point p1, struct point p2)
    {
    p1.x += p2.x;
    p1.y += p2.y;
    return p1;
    }
    My question is couldn't you just do:
    p1 += p2;



    Sheldon Simms wrote:
    > On Sun, 16 Nov 2003 00:12:05 -0600, Pushkar Pradhan wrote:
    >
    >
    >>I have a struct like this:
    >>struct point {
    >>int x;
    >>int y;
    >>};
    >>
    >>Then I need to assign a certain point "=" another pt.
    >>eg.
    >>struct point p1, pArr[10];
    >>
    >>/* initialize all pts. */
    >>Now assign p1 to a certain element of pArr, this is how I did and it
    >>works fine.
    >>p1 = pArr[someIndex];
    >>
    >>However, I just read my C book again and it says this stuff must be done
    >>like this actually:
    >>p1.x = parr[someIndex].x;
    >>p1.y = parr[someIndex].y;

    >
    >
    > Please tell us the name of the C book so that we can all avoid it.
    > What you have done is fine.
    >
    >
     
    Pushkar Pradhan, Nov 16, 2003
    #6
  7. Pushkar Pradhan <> scribbled the following:
    > The book is:
    > The C Programming Language by KERNIGHAN & RICHIE.
    > Ok I don't think they explicitly said that, I just concluded it from
    > this code in the book:
    > /* addpoint: add two points */
    > struct point addpoint(struct point p1, struct point p2)
    > {
    > p1.x += p2.x;
    > p1.y += p2.y;
    > return p1;
    > }
    > My question is couldn't you just do:
    > p1 += p2;


    This is because += is different than =. Simple assignment (with =) is
    always defined for structures. "Plus assignment" (with +=) is not. For
    example, if you had this structure:
    struct namedpoint {
    char *name;
    int x;
    int y;
    };
    how would the compiler treat "plus assignment" with those structures?

    I hope this answers your question. If it doesn't, I'll provide more
    detail, unless a real C guru does it first.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "C++ looks like line noise."
    - Fred L. Baube III
     
    Joona I Palaste, Nov 16, 2003
    #7
    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. JFCM
    Replies:
    4
    Views:
    5,741
  2. - Kees van der Bent -

    assigning bit-field structure

    - Kees van der Bent -, Apr 19, 2004, in forum: C Programming
    Replies:
    13
    Views:
    851
    - Kees van der Bent -
    Apr 22, 2004
  3. Arun
    Replies:
    1
    Views:
    402
  4. mr_guy99493
    Replies:
    0
    Views:
    336
    mr_guy99493
    Feb 4, 2011
  5. weston
    Replies:
    1
    Views:
    253
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page