carefull with string from algo

Posted By: Grat

carefull with string from algo - 06/17/20 13:50

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);
Posted By: AndrewAMD

Re: carefull with string from algo - 06/17/20 14:13

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
© 2024 lite-C Forums