![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
|
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. |
|
|
|
|
|
#2 | |
|
Professional Programmer
|
char* s1,s2,s3; s1="Hi "; s2="Hello"; strcat(s3,s1); strcat(s3,s2);
__________________
▄▄▄▄ Quote:
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! ▄▄▄▄ |
|
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#4 |
|
Newbie
|
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? |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#6 |
|
Newbie
|
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. |
|
|
|
|
|
#7 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 824
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Jul 2004
Location: Blacksburg, VA
Posts: 13
Rep Power: 0
![]() |
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.
|
|
|
|
|
|
#9 | |
|
Hobbyist Programmer
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 239
Rep Power: 3
![]() |
Quote:
__________________
Project::Soulstorm (personal homepage) |
|
|
|
|
|
|
#10 | |
|
Newbie
Join Date: Jul 2004
Location: Blacksburg, VA
Posts: 13
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|