1 registered members (Akow),
1,365
guests, and 7
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Strings in structures
#247538
01/21/09 14:36
01/21/09 14:36
|
Joined: Apr 2006
Posts: 737 Ottawa, Canada
Ottawa
OP
User
|
OP
User
Joined: Apr 2006
Posts: 737
Ottawa, Canada
|
Hi! I'm learning to code structures in Lite-C and I have a problem putting information in my strings variables
#include<acknex.h>
#include<default.c>
typedef struct PEINT {
var toucher ; // touched
var bon_rep ; // good answer
var mau_rep ; // bad answer
var jeu ; // game
char slettre[2];
STRING* s_mi;
} PEINT;
// ----------------- definitions
PEINT* STmi = {toucher = 4; bon_rep = 3; mau_rep = 5; jeu=21; slettre = "bx"; s_mi= "#100";}
//------------------ panel
PANEL* p_resultats = {
pos_x = 0;
pos_y = 0;
layer 900;
digits (10,100," jeu no: %.0f ",*,1,STmi.jeu);
digits (350, 100, "toucher = %.0f",*,1,STmi.toucher);
digits (550,100,"bonne rp: %.0f ",*,1,STmi.bon_rep);
digits (650,100,"mau choix: %.0f ",*,1,STmi.mau_rep);
digits (300,130,"value of STmi.lettre: %s ",*,1,STmi.slettre);
digits (300,160,"value of STmi.s_mi: %s ",*,1,STmi.s_mi);
flags = OVERLAY ;
}
function give_new_val ()
{
while (STmi == NULL) { wait (1); }
p_resultats.flags |= VISIBLE;
// change the values to
STmi.toucher = 14;
STmi.bon_rep = 13;
STmi.mau_rep = 15;
STmi.jeu = 222;
//-----------------------------
STmi.slettre = "by";
STmi.s_mi = "cv";
// str_cpy(STmi.slettre, "mmmm"); //not working..error 1515
// str_cpy(STmi.slettre,STmi.s_mi); //not working..error 1515
//------------------------------
}
function main()
{
video_set(800,600,32,1);
screen_color.blue = 255;
wait(2);
level_load("");
wait (3) ;
give_new_val ();
}
//
Hope this helps! Ottawa Ver 7.86.2 Pro and Lite-C
|
|
|
Re: Strings in structures
[Re: Xarthor]
#247657
01/21/09 23:32
01/21/09 23:32
|
Joined: Apr 2006
Posts: 737 Ottawa, Canada
Ottawa
OP
User
|
OP
User
Joined: Apr 2006
Posts: 737
Ottawa, Canada
|
Hi Xarthor! No the STmi.s_mi = "cv"; does not work. There is no error reported by using this format but the outcome is NULL. str_cpy works well when I work outside a structure. In this case it will not work because of the type. And I don't know how to code it so that it will accept the correct type STRING,
Hope this helps! Ottawa Ver 7.86.2 Pro and Lite-C
|
|
|
Re: Strings in structures
[Re: Ottawa]
#247674
01/22/09 02:14
01/22/09 02:14
|
Joined: Aug 2008
Posts: 218 U.S.
GamerX
Member
|
Member
Joined: Aug 2008
Posts: 218
U.S.
|
As far as i know you cannot change a structure STRING directly with str_cpy or even ="blablabla" the only way i have gotten to change a structures string was by making the structures string equal to a string. Like STRING* new_string = "blablabla"; STmi.s_mi = new_string;
You can probably do that then alter new_string. I ran into this problem about 2 months ago and that's how i fixed it. Have it in a project i am working on now and it works well. could be another way but that's the only way i have gotten it to work without an error.
"You may never know what results come of your action, but if you do nothing there will be no result." -Mahatma Gandhi
|
|
|
Re: Strings in structures
[Re: GamerX]
#247675
01/22/09 03:27
01/22/09 03:27
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
You cant set strings in a struct using = but you can use str_cpy, as long as you use str_create first, but only from inside a function.
#include<acknex.h>
#include<default.c>
typedef struct PEINT {
var toucher ; // touched
var bon_rep ; // good answer
var mau_rep ; // bad answer
var jeu ; // game
char slettre[2];
STRING* s_mi;
} PEINT;
// ----------------- definitions
PEINT* STmi = {toucher = 4; bon_rep = 3; mau_rep = 5; jeu=21; slettre = "bx"; s_mi = NULL;} //dont bother with 's_mi' yet
//------------------ panel
PANEL* p_resultats = {
pos_x = 0;
pos_y = 0;
layer 900;
digits (10,100," jeu no: %.0f ",*,1,STmi.jeu);
digits (350, 100, "toucher = %.0f",*,1,STmi.toucher);
digits (550,100,"bonne rp: %.0f ",*,1,STmi.bon_rep);
digits (650,100,"mau choix: %.0f ",*,1,STmi.mau_rep);
digits (300,130,"value of STmi.lettre: %s ",*,1,STmi.slettre);
digits (300,160,"value of STmi.s_mi: %s ",*,1,STmi.s_mi);
flags = OVERLAY ;
}
function give_new_val ()
{
while (STmi == NULL) { wait (1); }
p_resultats.flags |= VISIBLE;
// change the values to
STmi.toucher = 14;
STmi.bon_rep = 13;
STmi.mau_rep = 15;
STmi.jeu = 222;
STmi.slettre = "by";
str_cpy(STmi.s_mi, "mmmm"); //This should work - but also look at main
}
function main()
{
video_set(800,600,32,1);
screen_color.blue = 255;
level_load("");
wait(2);
STmi.s_mi = str_create("something"); //This will make "give_new_val" work
wait(-5); //wait a few seconds before running give_new_val
give_new_val ();
}
//
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Strings in structures
[Re: EvilSOB]
#247678
01/22/09 04:05
01/22/09 04:05
|
Joined: Apr 2006
Posts: 737 Ottawa, Canada
Ottawa
OP
User
|
OP
User
Joined: Apr 2006
Posts: 737
Ottawa, Canada
|
Hi!
I will work on your information and suggestions tomorow.
Hope this helps! Ottawa Ver 7.86.2 Pro and Lite-C
|
|
|
Re: Strings in structures
[Re: Ottawa]
#247776
01/22/09 14:35
01/22/09 14:35
|
Joined: Apr 2006
Posts: 737 Ottawa, Canada
Ottawa
OP
User
|
OP
User
Joined: Apr 2006
Posts: 737
Ottawa, Canada
|
Hi! to EvilSOB The line : STmi.s_mi = str_create("something"); in the main function creates a window error .... acknex.exe has encountered a problem and needs to close. We are sorry for the inconvenience. Send or don't send report If this line is placed outside any function (at the top) this message (crash) does not happen. The line : str_cpy(STmi.s_mi, "mmmm"); //This should work - but also look at main does not work. to GamerX STRING* new_string = "blablabla"; // declared at the top of code STmi.s_mi = new_string; // placed in the give_new_val () using both lines in the code gives me a crash and the same message as above. The problems to this situation are : first : declare in a structure a .lettre or a .s_mi that will receive the information that I want then to pick up later. (similar to .pan or .tilt) second : this information will have to be posted in a panel.
Hope this helps! Ottawa Ver 7.86.2 Pro and Lite-C
|
|
|
Re: Strings in structures
[Re: Ottawa]
#247945
01/23/09 15:00
01/23/09 15:00
|
Joined: Apr 2006
Posts: 737 Ottawa, Canada
Ottawa
OP
User
|
OP
User
Joined: Apr 2006
Posts: 737
Ottawa, Canada
|
Hi!
anyone? Using char or string in a structure doesn't seem easy!
Hope this helps! Ottawa Ver 7.86.2 Pro and Lite-C
|
|
|
Re: Strings in structures
[Re: Ottawa]
#248046
01/24/09 03:46
01/24/09 03:46
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
Expert
|
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Ottawa, sorry, my bad sloppy programming, I didnt read yours thoroughly. This code is an example of how to use strings in structs. It also has a workaround in it because pointers in structs cannot be used by panels, thats where the windows crash is coming from. #include<acknex.h>
#include<default.c>
typedef struct PEINT {
var toucher ; // touched
var bon_rep ; // good answer
var mau_rep ; // bad answer
var jeu ; // game
char slettre[2];
STRING* s_mi;
} PEINT;
// ----------------- definitions
PEINT* STmi = {toucher = 4; bon_rep = 3; mau_rep = 5; jeu=21; slettre = "bx"; s_mi = NULL;} //make sure s_mi=NULL
//------------------ panel
STRING* p_resultats_s_mi = "";
//
PANEL* p_resultats = {
pos_x = 0;
pos_y = 0;
layer 900;
digits (10,100," jeu no: %.0f ",*,1,STmi.jeu);
digits (350, 100, "toucher = %.0f",*,1,STmi.toucher);
digits (550,100,"bonne rp: %.0f ",*,1,STmi.bon_rep);
digits (650,100,"mau choix: %.0f ",*,1,STmi.mau_rep);
digits (300,130,"value of STmi.lettre: %s ",*,1,STmi.slettre);
// digits (300,160,"value of STmi.s_mi: %s ",*,1,STmi.s_mi);
// cant use s_mi in panels because panels dont like pointers...
digits (300,160,"value of STmi.s_mi: %s ",*,1,p_resultats_s_mi); // this is a different way
flags = OVERLAY ;
}
function give_new_val ()
{
while (STmi == NULL) { wait (1); }
p_resultats.flags |= VISIBLE;
// change the values to
STmi.toucher = 14;
STmi.bon_rep = 13;
STmi.mau_rep = 15;
STmi.jeu = 222;
STmi.slettre = "by";
if(STmi->s_mi==NULL) STmi->s_mi=str_create(""); //initialise String in struct
str_cpy(STmi->s_mi, "mmmm"); //This does work - but previous line MUST happen first
// //always use -> instead of . for s_mi because it is is a pointer
str_cpy(p_resultats_s_mi, STmi->s_mi); //for the panel to use
}
function main()
{
video_set(800,600,32,0);
screen_color.blue = 255;
level_load(NULL);
give_new_val ();
}
//
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Strings in structures
[Re: EvilSOB]
#248274
01/25/09 18:11
01/25/09 18:11
|
Joined: Apr 2006
Posts: 737 Ottawa, Canada
Ottawa
OP
User
|
OP
User
Joined: Apr 2006
Posts: 737
Ottawa, Canada
|
Hi EvilSOB! Thanks for the work and the information. I'll be able to move along now with strings in a struct. A note on your pointer to -> I found it's rank in a C manual. It's the highest. I will use it for pointer now instead of the . It also makes the information much clearer. Rank operator 1 -> . () [] 2 (cast) sizeof ! ++ ~ ...
A note to Conitec : We need more information on strings in struct in the manual.. We also need more information on char name[10];in your struct example. Again thanks EvilSob Ottawa
|
|
|
|