Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 13th, 2006, 11:51 AM   #1
keweedsmo
Newbie
 
Join Date: Feb 2006
Location: Albany, NY (UALBANY)
Posts: 20
Rep Power: 0 keweedsmo is on a distinguished road
Send a message via AIM to keweedsmo
is it possible to combine 2 char* with an operator (like in other languages)

how do I combine 2 char* into another?
char* s1;
char* s2;
char* s3;
char* s4
s1 = "Hi";
s2 = "Hello";
s3 = " ";
s4 = s1 + s3 + s2;

Output:
"Hi Hello"

Errors:
editorCore.cxx:143: invalid operands `char *' and `const char[2]' to binary `operator +
editorCore.cxx:147: invalid operands `char *' and `const char[2]' to binary `operator +
editorCore.cxx:150: invalid operands `char *' and `char *' to binary `operator +


I tried & and . operators also. they didn't work either.
keweedsmo is offline   Reply With Quote
Old Mar 13th, 2006, 12:14 PM   #2
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
char* s1,s2,s3;
s1="Hi ";
s2="Hello";
strcat(s3,s1);
strcat(s3,s2);
this copies s1 and s2 into s3. You could simple create a function to make this easier though.
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Mar 13th, 2006, 12:28 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
char* are pointer to an array of char. In C and C++, arrays are not objects in the sense that they are in some other languages. They are collections of objects (ints, char, whatever). Since they are collections of essentially unknown size, one can't just assign one of them to another. One must do it piecemeal. If one wants to define an object that contains an array of some sort, then one may populate the object with methods that will copy, assign, whatever. The construct, char *cPtr = "Abcd", is not really a language operation; it is a feature of the language compiler that eases your life while possibly confusing you unduly.

char*, while equivalent to an array of char in some respects, is not a complete equivalent. You will find differences explained in the "Pointer Basics" material referred to in my signature.

C++ has a string class that is much more intuitive to use; I recommend that you research it and, possibly, use it, rather than the C-style strings represented by a char array with a terminating '\0'.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 13th, 2006, 1:40 PM   #4
keweedsmo
Newbie
 
Join Date: Feb 2006
Location: Albany, NY (UALBANY)
Posts: 20
Rep Power: 0 keweedsmo is on a distinguished road
Send a message via AIM to keweedsmo
int spw = 0;
char * stt;
char str[72];

...

strcpy(str, cur_Ptr->data);

...

stt = strtok (str," ");
cur_Ptr->data = stt;
stt = strtok (NULL, " ");
while (stt != NULL)
{
	cout << "STT: " << stt << endl;
	cout << "5" << endl;
	for(int j =0; j <= spw; j++)
	{
                         //This Line causes stt to become null.
		strcat(cur_Ptr->data, " ");

Why is the strcat making stt null?
keweedsmo is offline   Reply With Quote
Old Mar 13th, 2006, 3:07 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Check your documentation for strtok. Your example code is incomplete, too. You're pushing things at us that we can't see, but you can. My crystal is in the shop for ball joints.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 13th, 2006, 4:34 PM   #6
keweedsmo
Newbie
 
Join Date: Feb 2006
Location: Albany, NY (UALBANY)
Posts: 20
Rep Power: 0 keweedsmo is on a distinguished road
Send a message via AIM to keweedsmo
well those are the only lines that really relate to the code at hand, other than cur_Ptr. thats just a pointer to a node on a linked list. I know I can't strtok seperate strings at the same time within while loops, but I didn't think that would affect strcat...

i can print stt before the strcat, but after the strcat i get a seg fault.
keweedsmo is offline   Reply With Quote
Old Mar 13th, 2006, 6:22 PM   #7
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 824
Rep Power: 4 The Dark is on a distinguished road
strtok returns a pointer into your original string. The strcat function that you called adds a space and a zero-byte to the end of the string that is pointed to by its parameter. This will overwrite the value that stt is pointing at.

For example. If your original string is
A B C D

the first call to strtok will return a pointer to A and replace the space after A with a zero byte. This pointer is then stored in cur_Ptr->data.
The second call to strtok will return a pointer to B and replace the space after B with a zero byte. This pointer is then stored in stt.

when you call strcat(cur_Ptr->data, " "), the zero byte that was after A gets replaced with the space from the parameter and the B gets replaced with the zero byte to null terminate the string.

This means that the value which used to be B is now a zero byte.

Clear as mud!
The Dark is offline   Reply With Quote
Old Mar 15th, 2006, 2:28 AM   #8
CodeWeasel
Newbie
 
Join Date: Jul 2004
Location: Blacksburg, VA
Posts: 13
Rep Power: 0 CodeWeasel is on a distinguished road
Just a heads up, a lot of the old C string manipulation functions like strcat and strtok are depricated now, with newer, "safer" versions out.
CodeWeasel is offline   Reply With Quote
Old Mar 15th, 2006, 2:53 AM   #9
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3 Soulstorm is on a distinguished road
Quote:
Originally Posted by CodeWeasel
Just a heads up, a lot of the old C string manipulation functions like strcat and strtok are depricated now, with newer, "safer" versions out.
This may sound like a newbie's question, but I would like a reference to them, if you may...?
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
Old Mar 15th, 2006, 9:36 PM   #10
CodeWeasel
Newbie
 
Join Date: Jul 2004
Location: Blacksburg, VA
Posts: 13
Rep Power: 0 CodeWeasel is on a distinguished road
Quote:
Originally Posted by Soulstorm
This may sound like a newbie's question, but I would like a reference to them, if you may...?
It's not that big a deal, old code still works like it used to, there are just new "secure" versions out there. Usually they have the same name, with a _s suffix after them. The old versions were found to be susceptible to buffer overruns. More info: http://msdn2.microsoft.com/en-US/library/8ef0s5kh.aspx
CodeWeasel is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:14 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC