How to make a string buffer...

S

s88

Hello All:
I'm trying to make a string buffer for my APIs. For example:
void test(char * str){
sprintf(str,"inner test\n");

}

int main(void){
int i = -5;
char *str=(char*)malloc(sizeof(char)*100);
sprintf(str,"\number is %d\n",i);
test(str);
printf("STR IS %s\n",str);
return 0;


I want the char array str contains "\nnumber is -5\ninner test\n", but
the result is "\ninner test\n". I know the str means that the beginning
pointer of the cahr array. So, as I invoke the test function, the
sprintf of teh test function write down the characters from the
beginning of the str.
How to slove this problem?
Thank you!

Dave.
 
S

Sandeep

s88 said:
Hello All:
I'm trying to make a string buffer for my APIs. For example:
void test(char * str){
sprintf(str,"inner test\n");

}

int main(void){
int i = -5;
char *str=(char*)malloc(sizeof(char)*100);
sprintf(str,"\number is %d\n",i);
test(str);
printf("STR IS %s\n",str);
return 0;


I want the char array str contains "\nnumber is -5\ninner test\n", but
the result is "\ninner test\n". I know the str means that the beginning
pointer of the cahr array. So, as I invoke the test function, the
sprintf of teh test function write down the characters from the
beginning of the str.
How to slove this problem?

Not sure what you mean by "String Buffer", but if this is a specific
problem, you can just use strcat in "temp" function.

void test(char * str)
{
strcat(str," inner test\n");
}
 
R

Robert Gamble

s88 said:
Hello All:
I'm trying to make a string buffer for my APIs. For example:
void test(char * str){
sprintf(str,"inner test\n");

}

int main(void){
int i = -5;
char *str=(char*)malloc(sizeof(char)*100);
sprintf(str,"\number is %d\n",i);
test(str);
printf("STR IS %s\n",str);
return 0;


I want the char array str contains "\nnumber is -5\ninner test\n", but
the result is "\ninner test\n". I know the str means that the beginning
pointer of the cahr array. So, as I invoke the test function, the
sprintf of teh test function write down the characters from the
beginning of the str.
How to slove this problem?

Don't cast the return value of malloc and check out the strcat
function.

Robert Gamble
 
C

CBFalconer

Robert said:
Don't cast the return value of malloc and check out the strcat
function.

Better, check out the (non-standard) strlcpy and strlcat
functions. You can find an implementation in pure portable C, with
references to the original proposals, etc., at:

<http://cbfalconer.home.att.net/download/strlcpy.zip>

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top