how can I add the integer to the end of string and let compiler interpret it as a whole big char array? Pls help me and thank you in advanceCode:for(int i =0; i<boardCount; i++)
{
char boardName[] = "dxxxB";
strcat(boardName,i);
}
Printable View
how can I add the integer to the end of string and let compiler interpret it as a whole big char array? Pls help me and thank you in advanceCode:for(int i =0; i<boardCount; i++)
{
char boardName[] = "dxxxB";
strcat(boardName,i);
}
check sprintf(...);
Even if I look sprintf it seem like only formating strings and do not return them I have to return DXXXB1, DXXXB2 and pass them to a function as reference, could you help me?
Also getting errors cannot convert int to constant char, is there anything else such as append string? I am very unfamiliar with C thank u in advance
is this something that you need?
Code:#include<stdio.h>
#include<stdlib.h>
char *func()
{
char *array=calloc(100,sizeof(char));
int i;
i=1;
sprintf(array,"DXXXB%d",i);
return array;
}
int main()
{
char *p;
p=func();
puts(p);
free(p);
return 0;
}
You can't pass by reference in C, however you can pass a pointer.
you'll have to resize boardName (it's allocated on the stack), since it's not large enough to hold anymore than "dxxxB"+1 characters, either make it big enough, or use dynamic memory allocation.
such as:
Code:#include <stdio.h>
void foo(char * s);
int main(void)
{
char boardName[32];
size_t i;
for(i = 0; i < boardCount; i++)
{
sprintf(boardName, "dxxxB%d", i);
foo(boardName);
}
return 0;
}
void foo(char * s)
{
/* do whatever with s */
return;
}
Thanx you. Both of them work and I learnt more than I expect.