Thread: Assigning value of a member of one structure to another

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    22

    Assigning value of a member of one structure to another

    Hello,

    I need to assign a value of the member aaa->myhouse[co1].id to aaa->myflat[co1].id.

    myhouse and myflat are both arrays of structures within the structure aaa. They contain different members, but id is a member that they both contain.

    Both structures myflat and myhouse had already been initialised before this.

    I tried

    aaa->myflat[co1].id=aaa->myhouse[co1].id;

    but when compiling, I get an error saying: " error: incompatible types when assigning to type to 'char[5]' from type 'char*'.

    From everything I've read about structures, I thought that using "->" would give the value of "id" and not the address of "id".

    What do I need to do to get the value of "id" and not the pointer, ie char[5] type and not char* type?

    Also, I don't understand why on the left side the expression is read as the value of id, and on the right side it is not. Is there a different way of accessing structure members whether you want to copy the values from them or assign the values to them?

    These are all the variables I am using:
    struct city *aaa
    struct house *myhouse
    struct flat *myflat
    char id[5]
    int co1

    Thanks!
    Last edited by Layla_E; 12-06-2017 at 05:33 PM. Reason: typo

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    you're using char for ID? ok you have to strcpy them
    Code:
    #include <stdio.h>
    #include <string.h>
     
    
    
    struct city
    {
    char id[5];
    int co1;
    };
    struct house
    {
    char id[5];
    int co1;
    };
    struct flat
    {
    char id[5];
    int co1;
    };
    
    int main (void)
    {
        //aaa->myflat[co1].id=aaa->myhouse[co1].id;
    
    struct city aaa;
    struct house  myhouse;
    struct flat myflat;
        
        
     strcpy(aaa.id, "123");
     strcpy(myhouse.id, aaa.id);
     strcpy(myflat.id, myhouse.id);
     
     printf("aaa id %s, myhouse.id %s, myflat.id %s\n", aaa.id,myhouse.id,myflat.id);
    
        
    return 0;    
    }
    C tutor
    Structures in C
    Last edited by userxbw; 12-06-2017 at 06:03 PM.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    id is an array. Forgot about the struct for a moment and work out if this works:

    Code:
    int main(void)
    {
        char id1[5] = "1234";
        char id2[5];
    
        id2 = id1;
    
        return 0;
    }
    If it doesn't work, then why? How would you properly copy the contents of the first array into the second?

  4. #4
    Registered User
    Join Date
    Oct 2017
    Posts
    22
    Thank you both, for pointing out to me that char[5] is an array and telling me to use strcpy to assign values!

    I don't get any compiling errors now.


    Quote Originally Posted by userxbw View Post
    you're using char for ID? ok you have to strcpy them
    Code:
    #include <stdio.h>
    #include <string.h>
     
    
    
    struct city
    {
    char id[5];
    int co1;
    };
    struct house
    {
    char id[5];
    int co1;
    };
    struct flat
    {
    char id[5];
    int co1;
    };
    
    int main (void)
    {
        //aaa->myflat[co1].id=aaa->myhouse[co1].id;
    
    struct city aaa;
    struct house  myhouse;
    struct flat myflat;
        
        
     strcpy(aaa.id, "123");
     strcpy(myhouse.id, aaa.id);
     strcpy(myflat.id, myhouse.id);
     
     printf("aaa id %s, myhouse.id %s, myflat.id %s\n", aaa.id,myhouse.id,myflat.id);
    
        
    return 0;    
    }
    C tutor
    Structures in C

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    NP dude

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 03-18-2014, 10:43 AM
  2. Replies: 3
    Last Post: 10-04-2012, 02:33 PM
  3. Replies: 2
    Last Post: 08-15-2012, 06:43 PM
  4. bus error when assigning value to member of struct
    By popapez in forum C Programming
    Replies: 4
    Last Post: 09-23-2009, 08:37 PM
  5. Assigning memory address of member struct to pointer.
    By Tronic in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2004, 05:53 PM

Tags for this Thread