Find the answer to your Linux question:
Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By levi_silva
C has a nice, simple syntax, and then they throw strings, er... character arrays, into the mix, and change the syntax with pointers for structurs (or something). Please help me ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    25

    How to assign string to struct's string?


    C has a nice, simple syntax, and then they throw strings, er... character arrays, into the mix, and change the syntax with pointers for structurs (or something). Please help me fix this error:

    Code:
    typedef struct {
      char bndry[10];
    } *UserData;
    
    int main (){...
     UserData data;
     char bndry[10] = "";
     strcpy (bndry, argv[3]);
    
    data->bndry = bndry;
    data->bndry = &bndry;
    data->bndry = *bndry;
    All of those last three lines fail. What's left to try? data.bndry is the same type as bndry, so I woulda thought the first line should work.

    In function 'main':
    :186: error: incompatible types when assigning to type 'char[10]' from type 'char *'
    187: error: incompatible types when assigning to type 'char[10]' from type 'char'
    188: error: incompatible types when assigning to type 'char[10]' from type 'char (*)[10]'
    Also tried
    Code:
    strcpy (data.bndry, bndry);
    and got
    request for member 'bndry' in something not a structure or union
    Pre-post edit: OK, I figured it out:
    Code:
    strcpy (data->bndry, bndry);
    Can someone explain the reasoning here? And, even better, why it's so difficult for me to get this (I assume you're a mind reader, or at least had experience learning it yourself).

  2. #2
    Just Joined!
    Join Date
    May 2011
    Location
    Brazil
    Posts
    21
    Hey,

    Lets think together

    When declaring UserData data; the variable data will store an address and not a value.

    The data variable is declared locally in the main funtion. Normally the local variables are not initialized unless you do it explicitly. In this case, data variable is storing an unknow address.

    When you use data->bndry = bndry; without allocate a valid address to data you are saying the following: "Go to the unknow address and there copy the char*. This is impossible, because the address is not valid.

    In your second try you did strcpy (data.bndry, bndry); In this statement you are trying to use data as a variable that stores a struct but remember that you created the data var as a pointer. Pointer stores address =)

    With that in mind, search for malloc function and the implementation of strcpy to really understand how the correct attribution is done

    After that come here with more doubts !!!!

    Best Regards
    StupidUser likes this.

  3. #3
    Just Joined!
    Join Date
    Jul 2010
    Posts
    25
    Quote Originally Posted by levi_silva View Post
    Hey,

    Lets think together

    When declaring UserData data; the variable data will store an address and not a value.

    The data variable is declared locally in the main funtion. Normally the local variables are not initialized unless you do it explicitly. In this case, data variable is storing an unknow address.

    When you use data->bndry = bndry; without allocate a valid address to data you are saying the following: "Go to the unknow address and there copy the char*. This is impossible, because the address is not valid.

    In your second try you did strcpy (data.bndry, bndry); In this statement you are trying to use data as a variable that stores a struct but remember that you created the data var as a pointer. Pointer stores address =)

    With that in mind, search for malloc function and the implementation of strcpy to really understand how the correct attribution is done

    After that come here with more doubts !!!!

    Best Regards
    Thanks for great reply. I added data = (UserData) malloc(sizeof *data); before the assignments, and used strcpy (data->bndry, bndry);. Works!

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •