Thread: Accessing a struct within a struct

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    Accessing a struct within a struct

    I have the following two structures...

    Code:
    typedef struct Coord {
      int   Degrees;
      float Minutes;
      char  Cardinal_Sign[4];
    } Coord;
    
    
    typedef struct GPS_Data_Struct {
      time_t Time;
      Coord Latitude;
      Coord Longitude;
      int Fix_Quality;
      int Sattelites;
      long Altitude;
    } GPS_Data_Struct;
    
    GPS_Data_Struct GPS_Data;
    I then have the following function...

    Code:
    int Get_GPS_Data(GPS_Data_Struct *GPS_Data) {
      GPS_Data->Satellites = 1;
      GPS_Data->Latitude.Cardinal_Sign = "   ";
      // yadda yadda
    
      return 0;
    }
    I get an "L-value required" error on the Cardinal_Sign line. Even when I used strcpy.

    What should I have there instead?

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You can't assign to an array. Either make it a pointer or strcpy it.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Perhaps because GPS_Data is an object instead of a pointer to GPS_Data_Struct.
    Code:
    GPS_Data.Latitude.Cardinal_Sign[] = "   ";
    Edit: above initialization works only in the declaration part, otherwise use
    Code:
    strcpy(GPS_Data.Latitude.Cardinal_Sign, "   ");
    Last edited by itCbitC; 04-20-2009 at 10:08 AM.

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    No. He declared Cardinal_Sign as an array, and you effectively can't assign to an array.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Brafil View Post
    No. He declared Cardinal_Sign as an array, and you effectively can't assign to an array.
    Except when initialization is done with the declaration.

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Initialization != Assigning ;-) You can also initialize const variables, but not assign to them.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Yep!

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Brafil View Post
    No. He declared Cardinal_Sign as an array, and you effectively can't assign to an array.
    Sure but in the OP it says the same thing happened with strcpy...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Oh. I didn't see that part. 8-)

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Ok. Finally solved the problem. A missing "}" earlier screwed the whole sequence up. Blah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Accessing a struct element in a different class...
    By Sparrowhawk in forum C++ Programming
    Replies: 0
    Last Post: 03-09-2009, 04:24 PM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM