Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (SBGuy), 712 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
carefull with string from algo #480591
06/17/20 13:50
06/17/20 13:50
Joined: May 2015
Posts: 390
Czech Republic
G
Grat Offline OP
Senior Member
Grat  Offline OP
Senior Member
G

Joined: May 2015
Posts: 390
Czech Republic
Hi,

I want use Algo name in my struct. But I i get this:

Quote

ALGO= test
ALGO= price
ALGO= worm
ALGO= sup
name in 0 isallOther
name in 1 isallOther
name in 2 isallOther
name in 3 isallOther

Finish test!
Logout.. ok



source code:
Code
typedef struct{
    var  dLong;          // 
    var  dShort; 
    string cName;       
}PDATA;

PDATA pdata[4];

void resetPdata(){
    int i=0;
    while (algo(loop("test","price","worm","sup"))){
        pdata[i].cName=(string)Algo;        
        printf("\nALGO= %s",pdata[i].cName);
        i++;
    }
}

function run(){
    if (is(INITRUN))
        resetPdata();

    algo("allOther");
    int i=0;
    for (i=0; i< 4;i++)
        printf("\n name in %d is%s",i,pdata[i].cName);
    quit("\n Finish test!");
  
}


correct is this:
pdata[i].cName=strf("%s",Algo);

Last edited by Grat; 06/17/20 13:57.
Re: carefull with string from algo [Re: Grat] #480594
06/17/20 14:13
06/17/20 14:13
Joined: Feb 2017
Posts: 1,725
Chicago
AndrewAMD Offline
Serious User
AndrewAMD  Offline
Serious User

Joined: Feb 2017
Posts: 1,725
Chicago
This solution is no good and will eventually fail:
Quote
correct is this:
pdata[i].cName=strf("%s",Algo);


string is just a char pointer. You are using expired/temporary pointers, so strange behavior is to be expected!

Use a char array in your struct instead, and fill it with strcpy.

https://zorro-project.com/manual/en/str_.htm
https://zorro-project.com/manual/en/aarray.htm


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1