473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

xstrcat function

I've been studing the way to have a more useful strcat function and I
realized that I need more than one argument to handle at a time, and
also a way to resize the pointer automatically so I can forget the
memory handling problem.

So I constructed a new xstrcat function but, although works pretty
well in most environments, I discover that it works erratically when I
try to add the result var on the list of strings to add (i mean if I
do a xstrcat(&tmp, tmp, NULL)), so I send this to the community to see
if someone with more experience than me can help.
Cheers,
Leo

char *
xstrcat (char **dest, char *str, ...)
{
va_list va;
size_t length = 0;
char *ptr, *tmp;

if (!str)
return (char *) NULL;

length += strlen (str);

va_start (va, str);
while ((tmp = va_arg (va, char *)))
length += strlen (tmp);
va_end (va);

if (!*dest)
*dest = malloc (length + 1);
else
*dest = realloc (*dest, (length + 1));

if (!dest)
return (char *) NULL;

ptr = *dest;

for (tmp = str; *tmp; tmp++)
*ptr++ = *tmp;

va_start (va, str);
while ((tmp = va_arg (va, char *)))
{
while (*tmp)
*ptr++ = *tmp++;
}
va_end (va);
*ptr = '\0';
return *dest;
}
Nov 13 '05 #1
1 3276
lb****@afip.gov.ar (Leonardo Javier Belén) writes:
So I constructed a new xstrcat function but, although works pretty
well in most environments, I discover that it works erratically when I
try to add the result var on the list of strings to add (i mean if I
do a xstrcat(&tmp, tmp, NULL)), so I send this to the community to see
if someone with more experience than me can help. char *
xstrcat (char **dest, char *str, ...)
{
va_list va;
size_t length = 0;
char *ptr, *tmp;

if (!str)
return (char *) NULL;
The cast is gratuitous.
length += strlen (str);

va_start (va, str);
while ((tmp = va_arg (va, char *)))
length += strlen (tmp);
va_end (va);

if (!*dest)
*dest = malloc (length + 1);
else
*dest = realloc (*dest, (length + 1));
1. realloc() acts like malloc() when the first argument is a null
pointer, so there's no need to switch to malloc() for that
case.

2. If realloc() fails, you probably just leaked memory, because
you overwrote the original pointer.
if (!dest)
This does not test what you think it tests. If allocation
failed, *dest will be a null pointer, not dest.
return (char *) NULL;
Gratuitous cast.
ptr = *dest;

for (tmp = str; *tmp; tmp++)
*ptr++ = *tmp;
va_start (va, str);
while ((tmp = va_arg (va, char *)))
{
while (*tmp)
*ptr++ = *tmp++;
}
va_end (va);
*ptr = '\0';
The rest of this looks okay, though I'd prefer strcpy() followed
by strchr() over explicit loops.
return *dest;
}


--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.