Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Feb 23rd, 2006, 9:55 PM   #1
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
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);
to get the "0" from "02222AAA00".

but how to get the "2222" from "02222AAA00"???
myName is offline   Reply With Quote
Old Feb 23rd, 2006, 10:32 PM   #2
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
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?
myName is offline   Reply With Quote
Old Feb 23rd, 2006, 10:53 PM   #3
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 3 Kaja Fumei is on a distinguished road
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());
Kaja Fumei is offline   Reply With Quote
Old Feb 24th, 2006, 2:28 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Feb 24th, 2006, 4:35 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by myName
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"
The lengths of each of the new'd arrays are one too short (particularly if you want to treat chOne etc as strings without invoking undefined behaviour).

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:
Originally Posted by myName
than,
i use strncpy
strncpy(chOne, chMsg, 1);
to get the "0" from "02222AAA00".
This is technically OK, unless you attempt to use chOne as a string. For example;
fprintf(stdout, "%s\n", chOne);
will generate undefined behaviour (in practice, a common result will be garbage output, but anything is actually allowed to happen).

To eliminate the undefined behaviour, do this immediately after the above strncpy() call....
chOne[1] = '\0';
Quote:
Originally Posted by myName
but how to get the "2222" from "02222AAA00"???
I'm assuming you can assume, as in this example, that the "2222" substring starts at the second byte in the string (i.e. at index 1) and you know it is of length 4. In that case, the approach is;
   strncpy(chTwo, chMsg + 1, 4);
   chTwo[4] = '\0';
grumpy is offline   Reply With Quote
Old Feb 26th, 2006, 7:37 PM   #6
myName
Programmer
 
Join Date: Oct 2005
Posts: 48
Rep Power: 0 myName is on a distinguished road
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. :-)
myName is offline   Reply With Quote
Old Feb 26th, 2006, 9:10 PM   #7
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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?
titaniumdecoy is offline   Reply With Quote
Old Feb 26th, 2006, 9:55 PM   #8
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 746
Rep Power: 3 Jimbo is on a distinguished road
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".
Jimbo is offline   Reply With Quote
Old Feb 26th, 2006, 10:56 PM   #9
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 837
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I thought a * meant that a variable was a pointer... (to a single char)?
titaniumdecoy is offline   Reply With Quote
Old Feb 26th, 2006, 11:19 PM   #10
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 746
Rep Power: 3 Jimbo is on a distinguished road
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
Jimbo 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 5:19 AM.

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