Results 1 to 3 of 3
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.
- 01-15-2013 #1Just 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:
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.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;
Also triedIn 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]'
and gotCode:strcpy (data.bndry, bndry);
Pre-post edit: OK, I figured it out:request for member 'bndry' in something not a structure or unionCan 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).Code:strcpy (data->bndry, bndry);
- 01-16-2013 #2Just 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
- 01-16-2013 #3Just Joined!
- Join Date
- Jul 2010
- Posts
- 25


1Likes
Reply With Quote

