my problem : tab1[i] is always the same value
i need to have tab1[0]="0" and tab[1] = "1" etc, but i have tab1[0]=tab[1]=...=tab[fin] : i think it's due to the adress !!
I don't want to use 'string' because i have a function which works with char !!!
in fact i have ever used .c_str() but the problem is the same : when i want to convert string to *char i have the last character (here it's "50" ) in every cases of my *char : tab1[0]="50", tab1[2]="50", etc... but i need to put 0,1, ... 50 in tab1
please, don't worry about it !! i just need to fill tab1
i want
tab1[0]="0"
tab1[1]="1"
...
...
tab1[50]="50"
but unfortunately i have
tab1[0]="50"
tab1[1]="50"
....etc
when i ask it to show me that using that code :
for(i=0;i<=50;i++)
{
sprintf(T,"%d",i);
tab1[i]=T;
}
The issue is that you copy the pointer instead of the string, so every element of the array points to the same string. This is why you should use std::string to avoid such issues.
If you insist on not using std::string, your only option is to allocate memory for each element of your array and use strcpy to copy from T into the element. This is messy and requires you to manually clean up the memory afterward.
you're right, it works correctly with string.
But it means i have to restart Myfunction (char a[], char b[]), except if it was possible to translate the data 'string' to char !??!