C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

trouble understanding a problem when using strcat

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Mon May 07, 2007 9:11 am    Post subject: trouble understanding a problem when using strcat Reply with quote



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!
Back to top
Richard Heathfield
Guest





PostPosted: Mon May 07, 2007 9:11 am    Post subject: Re: trouble understanding a problem when using strcat Reply with quote



sail0r (AT) creepjoint (DOT) net said:

Quote:
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]

Quote:
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.
Back to top
Roland Pibinger
Guest





PostPosted: Mon May 07, 2007 9:11 am    Post subject: Re: trouble understanding a problem when using strcat Reply with quote



On 6 May 2007 22:54:40 -0700, sail0r (AT) creepjoint (DOT) net wrote:
Quote:
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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.