to complete a Tab ?

hello,

i would like to complete a char* tab
you can see the code i want :

int deb=0,fin=48i;
char *tab1[50];
char T[50];

for(i=deb;i<=fin;i++)
{
sprintf(T,"%d",i);
tab1[i]=T;
}

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 !!!

So, please what should i do ?


Last edited on
juls wrote:
I don't want to use 'string' because i have a function which works with char !!!
std::string has a member function .c_str() which gives you what you want.
1
2
3
4
5
std::vector<std::string> nums;
for(std::size_t i = 0; i < 50; ++i)
{
    nums.push_back(std::to_string(i));
}
Don't be afraid to use the C++ Standard Library as it is meant to be used.
Last edited on

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
Last edited on
It works fine for me: https://ideone.com/BSxW77
Make sure you #include <vector>
juls wrote:
when i want to convert string to *char i have the last character (here it's "50" ) in every cases of my *char!
I do not understand what you mean.
Last edited on
i want to read : tab[0]=0 and tab[1]=1 etc
but the code you shew does :
tab[0]=tab[1]=.......= tab[50]=50

if a follow you, i have a string, but i use a function which does :
Myfunction(char a, char b)

so i have to convert string to char, and the problem continues : tab[0]=tab[1] =....=50 : it's not what i'm waiting for !
juls wrote:
but the code you shew does :
tab[0]=tab[1]=.......= tab[50]=50
No, the code I showed clearly does not do anything like that at all. You can even see the output below the code: https://ideone.com/BSxW77
juls wrote:
if a follow you, i have a string, but i use a function which does :
Myfunction(char a, char b)
A char can only contain one digit, so you can't store "10" or higher in a char.
A char can only contain one digit, so you can't store "10" or higher in a char.

yes but it's not a problem if a put for example :
tab1[0]="1565255";
tab1[1]="55447";
Myfunction (tab1[0],tab1[1]) .. it works in Myfunction !!!

so my problem is just how to fill tab1[i] for i from 0 to 50 even if a use a string ???

and that is curiously NOT OK :
for(i=deb;i<=fin;i++)
{
sprintf(T,"%d",i);
tab1[i]=T;
}
Last edited on
These three functions are extremely different:

Myfunction(char a, char b)

Myfunction(char *a, char *b)

Myfunction(char const *a, char const *b)

Which one are you using? It should be the third one.
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;
}
Last edited on
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.
ok it's surely true, but i don't see how to fill tab1 after that ?
Last edited on
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.
Last edited on
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 !??!
Thank you LB for your answer !

I have a new request :
now, i would like to do stcmp(string,string)
it works only as well as strcmp(char*,char*)

so what can i do please ?
Last edited on
Use the == operator when using std::string, no need to call any extra function.
ok thank you, in fact it was so easy !!
Topic archived. No new replies allowed.