trouble understanding a problem when using strcat

S

sail0r

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!
 
R

Richard Heathfield

(e-mail address 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
 
R

Roland Pibinger

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.
 
P

pete

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 */
 

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
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top