470,625 Members | 1,963 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,625 developers. It's quick & easy.

trouble understanding a problem when using strcat

Perhaps this is obvious but I am not sure what is going on...
Here is the relevant code:
char *command;
char *argument;
char url[]="file:///usr/u/myname/Project/cats/";
char target_path[]="/tmp/abc";

command=strtok(buf,":\n\r");
argument=strtok(NULL,"\n\r");

/*now, I want to strcat argument onto both url and target_path */
strcat(url,argument);
strcat(target_path,argument);

But that doesn't work as I expect it to. Why?
What I am seeing is that the first strcat work but the second one
has a result string that is all discombobulated somehow?

Any advice would be greatly appreciated!

May 7 '07 #1
3 1600
sa****@creepjoint.net said:
Perhaps this is obvious but I am not sure what is going on...
Here is the relevant code:
char *command;
char *argument;
char url[]="file:///usr/u/myname/Project/cats/";
How much storage is reserved for url[]? Count the bytes in the
initialiser to find out. (Add 1 for the terminator.) Call this X.

How much of this storage is used? Call this Y. What do you notice about
X and Y? [1]
char target_path[]="/tmp/abc";

command=strtok(buf,":\n\r");
argument=strtok(NULL,"\n\r");

/*now, I want to strcat argument onto both url and target_path */
strcat(url,argument);
Let us assume that argument now points to a string longer than zero
bytes. Z, say, not including the terminator.

How much storage will the url[] array require if it is to store both the
information it already contains and the Z bytes of information you are
now trying to add to it? [2]

How much storage does it actually have? [3]

How short are you of the necessary bytes? [4]

Consider using dynamic memory allocation instead.
Answers:

[1] They are equal
[2] Y + Z
[3] Y
[4] Z

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 7 '07 #2
On 6 May 2007 22:54:40 -0700, sa****@creepjoint.net wrote:
>But that doesn't work as I expect it to. Why?
Unlike other languages C has no string type. Really. You need to use
'\0' terminated char arrays and provide the necessary automatic or
dynamic memory space for them.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
May 7 '07 #3
sa****@creepjoint.net wrote:
>
Perhaps this is obvious but I am not sure what is going on...
Here is the relevant code:
char *command;
char *argument;
char url[]="file:///usr/u/myname/Project/cats/";
char target_path[]="/tmp/abc";

command=strtok(buf,":\n\r");
argument=strtok(NULL,"\n\r");

/*now, I want to strcat argument onto both url and target_path */
strcat(url,argument);
strcat(target_path,argument);

But that doesn't work as I expect it to. Why?
What I am seeing is that the first strcat work but the second one
has a result string that is all discombobulated somehow?

Any advice would be greatly appreciated!
/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

int main(void)
{
char *command = NULL;
char argument[] = "\nWhat is \"buf\" supposed to be?\n\n";
char url[sizeof "file:///usr/u/myname/Project/cats/"
+ sizeof argument]
= "file:///usr/u/myname/Project/cats/";
char target_path[sizeof url + sizeof "/tmp/abc"] = "/tmp/abc";
/*
** command = strtok(buf, ":\n\r");
** argument= strtok(NULL, "\n\r");
*/
/*now, I want to strcat argument onto both url and target_path */
strcat(url, argument);
strcat(target_path, argument);
puts(url);
puts(target_path);
return 0;
}

/* END new.c */

--
pete
May 7 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by CheGueVerra | last post: by
18 posts views Thread by Ian Stanley | last post: by
6 posts views Thread by Gary Morris | last post: by
16 posts views Thread by mikelinyoho | last post: by
9 posts views Thread by Greg Linwood | last post: by
6 posts views Thread by Daniel Walzenbach | last post: by
8 posts views Thread by ctara_shafa | last post: by
20 posts views Thread by ramubdvt | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.