0 registered members (),
1,209
guests, and 0
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: typedef struct
[Re: delinkx]
#238012
11/24/08 03:10
11/24/08 03:10
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
Bot file_str_read and str_cpy work with STRING. I doubt they work with character arrays. The use of file_str_read can be done by first reading the string to STRING and then copying your strings char members to your struct. Concerning str_cpy you can use strcpy instead. Remember that bot parameters in strcpy have to be char*.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
Re: typedef struct
[Re: Gerrit]
#238126
11/25/08 02:53
11/25/08 02:53
|
Joined: Jul 2008
Posts: 553 Singapore
delinkx
User
|
User
Joined: Jul 2008
Posts: 553
Singapore
|
for the second part.. use assignment operator directly: our_players[i].playername = current_player; here is a simple sample code:
var i = 0;
typedef struct
{
int score;
STRING* score_char;
STRING* playername;
} our_player;
our_player our_players[10];
STRING* current_player = "Me";
function main()
{
for(i=0; i<10; i++)
{
our_players[i].playername = "xxx";
}
for(i=0; i<10; i++)
{
str_cpy(current_player, our_players[i].playername);
}
for(i=0; i<10; i++)
{
our_players[i].playername = "oooooooo";
}
}
|
|
|
Re: typedef struct
[Re: delinkx]
#238132
11/25/08 04:07
11/25/08 04:07
|
Joined: Sep 2005
Posts: 75 CA, Orange County
Gerrit
OP
Junior Member
|
OP
Junior Member
Joined: Sep 2005
Posts: 75
CA, Orange County
|
Delinkx, Thank you very much for the tip. I can't believe that this works. I wonder what good the str_cpy does if a simple assigment does the trick.... Again thank you very much I was pulling my hair out ![smile smile](/ubb7/images/graemlins/default_dark/smile.gif) Best regards Gerrit
|
|
|
Re: typedef struct
[Re: Gerrit]
#238137
11/25/08 05:27
11/25/08 05:27
|
Joined: Jul 2008
Posts: 553 Singapore
delinkx
User
|
User
Joined: Jul 2008
Posts: 553
Singapore
|
yes. ![smile smile](/ubb7/images/graemlins/default_dark/smile.gif) tats true. if u copying from struct's string to a normal string, use Assignment operator. if u copying from string to struct's string use str_cpy. see in my code above how i wrote. ![smile smile](/ubb7/images/graemlins/default_dark/smile.gif) i also faced this problem before. i donno wats the logic behind it. but it works this way. maybe the memory allocation is done differently for struct and STRING alone.
|
|
|
Re: typedef struct
[Re: delinkx]
#238145
11/25/08 07:55
11/25/08 07:55
|
Joined: Jan 2002
Posts: 4,225 Germany / Essen
Uhrwerk
Expert
|
Expert
Joined: Jan 2002
Posts: 4,225
Germany / Essen
|
There is a difference between a simple assignment and str_cpy.
When you use str_cpy you have to ensure that both parameters passed to the function point to a valid string. str_cpy just copies the content of strings. So if you want to copy a string to a string of a struct you have to first initialize it with str_create.
yourstruct.string = str_create("#12"); str_cpy(yourstruct.string,"Hello World");
If you just use the assingment operator something different is done. You then assign the pointer the memory area of the string. This is a huge difference. Once you free the string (for what ever reasons) all pointers pointing to that string will immediately become invalid.
You have to choose what version fits your needs better.
Always learn from history, to be sure you make the same mistakes again...
|
|
|
|