The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
strcat
Discuss strcat in the C Programming forum on Dev Shed. strcat C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 1st, 2004, 12:01 PM
|
|
Junior Member
|
|
Join Date: Feb 2004
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
strcat
Hi
I have hit upon a problem in the program I am currently writing in C. I have a string
and I have searched through this string, incrementing the pointer with
until I get to the character I want to copy. Then I want to append this character to another string:
which I initialise to empty string:
Now I want to add the current 'text' character to the 'temp', then move the pointer along the text and append the next charcter, (until some test stops the loop).
Code:
while (*text != 'X') {
strcat(test,*text);
text++
}
I keep getting conversion errors because the parameters of strcat are wrong but I can't work out what is wrong. Can anyone help?
I just want to append the first character pointed to by 'test', to 'text'. It seems so simple but I can't get it to work!!!
|

February 1st, 2004, 12:43 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
strcat expects the second parameter to be a string (i.e., a char pointer). You cannot use it to append a single character as a char.
I would think either create a string out of the character (copy it to the first element of a two-char array whose second char is '\0' and apply that array to strcat). Or simply set the destination string's null-terminator position to the new character and the next position to '\0'.
|

February 2nd, 2004, 06:09 AM
|
|
Junior Member
|
|
Join Date: Feb 2004
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Can you show me how to create an empty string and with the character pointed to by *text and the null character. And then how to use it with strcat?
Thanks
|

February 2nd, 2004, 06:22 AM
|
 |
Contributing User
|
|
Join Date: Jan 2004
Location: near St. Louis Illinois
|
|
The initial allocation of your variable text has to be large enough to hold all the characters that it is to contain. In your example, it is only large enough for one character -- the null terminator. you need to do something like this:
Code:
char *temp = malloc(80); // hold up to 79 characters
// make it an empty string
temp[0] = 0;
// add something to it
strcpy(temp,"Some stuff ");
// contantinate a string to the end
strcat(temp,"Hello World");
or you could also do it this way
Code:
char temp[80]; // hold up to 79 characters
// make it an empty string
temp[0] = 0;
// add something to it
strcpy(temp,"Some stuff ");
// contantinate a string to the end
strcat(temp,"Hello World");
Last edited by Ancient Dragon : February 2nd, 2004 at 06:25 AM.
|

February 2nd, 2004, 06:55 AM
|
|
Junior Member
|
|
Join Date: Feb 2004
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
that worked great thanks, had me stumped for ages, not used to this manual memory allocation business!!
by the way, needed to cast the malloc before using it...
Code:
char * temp = ( char * ) malloc ( 80 );
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|