Gamestudio Links
Zorro Links
Newest Posts
Executing Trades on Next Bar Open
by vicknick. 06/13/24 08:51
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 1,209 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19059 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
typedef struct #238007
11/24/08 01:36
11/24/08 01:36
Joined: Sep 2005
Posts: 75
CA, Orange County
Gerrit Offline OP
Junior Member
Gerrit  Offline OP
Junior Member

Joined: Sep 2005
Posts: 75
CA, Orange County
I would like to use the following structure:

typedef struct
{
int score;
char score_char[10];
char playername[30];

} our_player;

our_player our_players[10]; // array of 10 our_players

that seems to work when I use this in the following (at least I don't get an error):
(in a for loop)
our_players[i].playername = " ";

but that doesn't seem to work when using
file_str_read(filehandle,our_players[i].playername);
or even when I simply try to copy as in:
str_cpy(our_players[i].playername,current_player);

Any suggestions or help would be very welcome
Thanks
Gerrit

Re: typedef struct [Re: Gerrit] #238009
11/24/08 02:42
11/24/08 02:42
Joined: Jul 2008
Posts: 553
Singapore
delinkx Offline
User
delinkx  Offline
User

Joined: Jul 2008
Posts: 553
Singapore
wat error u get ? post the error.

Also if its not a typo mistake here, then its a syntax error: while defining the struct u used OUR_PLAYER. and in ur code below you are using OUR_PLAYERS.

else, the code is proper for str_copy if current_player is a string.


A7 commercial Team / VC++ 2008 Pro
homepage | twitter | facebook
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 Offline
Expert
Uhrwerk  Offline
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: Uhrwerk] #238114
11/25/08 00:20
11/25/08 00:20
Joined: Sep 2005
Posts: 75
CA, Orange County
Gerrit Offline OP
Junior Member
Gerrit  Offline OP
Junior Member

Joined: Sep 2005
Posts: 75
CA, Orange County
Uhrwerk,
Can I use a STRING* in a typedef struct definition and if so how (I tried everything I can think of -> no success).

If the above is ompossible how do I convert (copy) a char[10] to a string and vice versa. The manual states that char[10] and string are pretty much the same but that is not what I experience.

Your help is greatly appriciated
Best regards
Gerrit

Re: typedef struct [Re: Gerrit] #238121
11/25/08 01:22
11/25/08 01:22
Joined: Sep 2005
Posts: 75
CA, Orange County
Gerrit Offline OP
Junior Member
Gerrit  Offline OP
Junior Member

Joined: Sep 2005
Posts: 75
CA, Orange County
Some progress:
I did try the following definition:

typedef struct
{
int score;
STRING* score_char;
STRING* playername;

} our_player;

our_player our_players[10]; // define an array of the struct player

now this works:
str_cpy(current_player, our_players[i].playername); (in a for loop)
but this doesn't:
str_cpy(our_players[i].playername,current_player); (gives error invalid argument (string)

So if I could now find a way to copy the STRING current_player in the string of the structure my problem would be solved.

Any suggestion are greatly appriciated.
Gerrit

Re: typedef struct [Re: Gerrit] #238126
11/25/08 02:53
11/25/08 02:53
Joined: Jul 2008
Posts: 553
Singapore
delinkx Offline
User
delinkx  Offline
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:

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";
	}
	
	
}



A7 commercial Team / VC++ 2008 Pro
homepage | twitter | facebook
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 Offline OP
Junior Member
Gerrit  Offline 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

Best regards
Gerrit

Re: typedef struct [Re: Gerrit] #238134
11/25/08 05:03
11/25/08 05:03
Joined: Sep 2005
Posts: 75
CA, Orange County
Gerrit Offline OP
Junior Member
Gerrit  Offline OP
Junior Member

Joined: Sep 2005
Posts: 75
CA, Orange County
Delinkx,
I spoke too soon.
the statement our_players[i].playername = "text" // works fine
BUT
the statement our_players[i].playername = current_player // does not work even though current_player is defined as a string...
Some weird problem...

Re: typedef struct [Re: Gerrit] #238137
11/25/08 05:27
11/25/08 05:27
Joined: Jul 2008
Posts: 553
Singapore
delinkx Offline
User
delinkx  Offline
User

Joined: Jul 2008
Posts: 553
Singapore
yes. smile 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 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.


A7 commercial Team / VC++ 2008 Pro
homepage | twitter | facebook
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 Offline
Expert
Uhrwerk  Offline
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...
Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1