Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > trouble understanding a problem when using strcat

Reply
Thread Tools

trouble understanding a problem when using strcat

 
 
sail0r@creepjoint.net
Guest
Posts: n/a
 
      05-07-2007
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!

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      05-07-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Roland Pibinger
Guest
Posts: n/a
 
      05-07-2007
On 6 May 2007 22:54:40 -0700, (E-Mail Removed) 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
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      05-07-2007
(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String Concatenation Using Pointers No strcat() raidvvan@yahoo.com C Programming 12 12-08-2007 04:51 PM
strcpy and strcat problem JC C Programming 23 09-29-2003 04:27 PM
strcat problem again Ian Stanley C Programming 18 09-22-2003 06:06 AM
strcat problem Ian Stanley C Programming 5 09-13-2003 02:03 PM
Using strcat Nicholas C Programming 10 09-12-2003 12:05 AM



Advertisments