![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 48
Rep Power: 0
![]() |
char *
I have a a string
char *chMsg = "02222AAA00"; i wana separate this "02222AAA00" into part by part.. Like this "0", "2222", "AAA", "00". so i declared 4 variables char *chOne = new char[1]; //"0" char *chTwo = new char[4]; //"2222" char *chThree= new char[3]; //"AAA" char *chFour = new char[2]; //"00" than, i use strncpy strncpy(chOne, chMsg, 1); but how to get the "2222" from "02222AAA00"??? |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Oct 2005
Posts: 48
Rep Power: 0
![]() |
i try this
string strMsg = chMsg;
string strOne = strMsg.substr(0,1);
string strTwo = strMsg.substr(1,4);
string strThree = strMsg.substr(5,3);
string strFour =strMsg.substr(8,2);
printf( "chMsg: %s \n", strMsg);error in printf, the use of %s. i found that %s is just use for char *. How bout string data type? ![]() |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 3
![]() |
You can use the c_str() method in the string type to return the string as a char*:
printf( "chMsg: %s \n", strMsg.c_str()); |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Since this is in the C++ forum, just use:
std::cout << strMsg << std::endl;
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 | |||
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5
![]() |
Quote:
C-strings include a trailing zero byte, so you need to do this; char *chOne = new char[2]; //"0" char *chTwo = new char[5]; //"2222" char *chThree= new char[4]; //"AAA" char *chFour = new char[3]; //"00" Quote:
fprintf(stdout, "%s\n", chOne); To eliminate the undefined behaviour, do this immediately after the above strncpy() call.... chOne[1] = '\0'; Quote:
strncpy(chTwo, chMsg + 1, 4); chTwo[4] = '\0'; |
|||
|
|
|
|
|
#6 |
|
Programmer
Join Date: Oct 2005
Posts: 48
Rep Power: 0
![]() |
Kaja Fumei, nnxion and, grumpy,
Thanks guys. Very appreciate all yours help. :-) Grumpy, your explained very detail and clear, make me very easy to understand the concepts.. Thanks all. :-) |
|
|
|
|
|
#7 |
|
Expert Programmer
|
I'm not so good at C++, but I thought the char type was for storing single characters, not strings, such as char *chMsg = "02222AAA00". How does that work?
|
|
|
|
|
|
#8 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 746
Rep Power: 3
![]() |
a char is just a single character, while a char* (which could also be written as char []) is an array of characters, hence the "02222AAA00".
|
|
|
|
|
|
#9 |
|
Expert Programmer
|
I thought a * meant that a variable was a pointer... (to a single char)?
|
|
|
|
|
|
#10 |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 746
Rep Power: 3
![]() |
A pointer points to a char in memory, but an array is a sequence of consecutive chars in mem. So you could have a pointer to the first one, and then iterate through memory (by incrementing the pointer) to get the rest of the array.
Basically, arrays are pointers that only point the the first item yet still index the whole array. (array[i] == *(array+i)) You'll see the similarities a lot more when you use dynamic memory allocation |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|