C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 1st, 2004, 12:01 PM
nelsonxl nelsonxl is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 3 nelsonxl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
Code:
char *text;

and I have searched through this string, incrementing the pointer with
Code:
text++;

until I get to the character I want to copy. Then I want to append this character to another string:
Code:
char *temp;

which I initialise to empty string:
Code:
temp = "";

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

Reply With Quote
  #2  
Old February 1st, 2004, 12:43 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,138 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 22 h 31 m 19 sec
Reputation Power: 1974
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'.

Reply With Quote
  #3  
Old February 2nd, 2004, 06:09 AM
nelsonxl nelsonxl is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 3 nelsonxl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old February 2nd, 2004, 06:22 AM
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jan 2004
Location: near St. Louis Illinois
Posts: 3,288 Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level)Ancient Dragon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 20 h 37 m 22 sec
Reputation Power: 23
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.

Reply With Quote
  #5  
Old February 2nd, 2004, 06:55 AM
nelsonxl nelsonxl is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 3 nelsonxl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 );

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > strcat

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap