![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Newbie
Join Date: Sep 2005
Posts: 28
Rep Power: 0
![]() |
You should use malloc to reserve space for the char array as DaWei said. Do something like
char message[1024]; char* begin; begin = (char*) malloc (sizeof(char) * (strlen(message)+1)); strncpy(begin,message,(strlen(message)+1)); The above code copies the string inside the begin char array. You could use 2 arrays, one to store the command and one to store the argument. It's all up to you...
__________________
The geeks shall inherit the earth. |
|
|
|
|
|
#12 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
I don't see why you don't use the keyword new. Or why you're not using C++ strings.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#13 |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
You don't need either malloc or new:
#include <iostream>
using namespace std;
int main ()
{
char message[1024];
char* begin;
cin.getline(message, 1024);
begin = message + 4;
cout << begin;
cin.sync();
cin.get();
return 0;
}EDIT: Just be aware that 'begin' is pointing to the actual location of message[3]. So any changes made to 'message + 4' or above will be seen from 'begin' also. Last edited by Cache; Jan 29th, 2006 at 11:32 AM. |
|
|
|
|
|
#14 |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
I really wish the edit time on this forum would be extended slightly. Anyway, I just wanted to say: on reflection, it does actually look a lot cleaner to use new, and negates the need for a second variable:
#include <iostream>
using namespace std;
int main ()
{
char *message = new char[1024];
cin.getline(message, 1024);
message += 4;
cout << message;
cin.sync();
cin.get();
delete [] message;
return 0;
} |
|
|
|
|
|
#15 |
|
Programming Guru
![]() ![]() ![]() |
Just a possible alternative... how about using strings?
string s = "say hello"; s = s.substr(5,s.length());
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#16 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Suggested in posts #7 and #12, IR, and apparently rejected
.
__________________
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 |
|
|
|
|
|
#17 |
|
Programming Guru
![]() ![]() ![]() |
Missed it... I guess I read too many threads this morning... lines are running together.
I should probably go get my morning supply of caffeine.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#18 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#19 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
If you want the first 4 characters deleted, you could also do this:
strcmp(message, message + 4); I'm not sure this will work on every C library, since implementations can always do things weird . |
|
|
|
|
|
#20 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
strcmp is a string comparison function. Perhaps you were thinking of strcpy. Both are standard library functions. If an implementation doesn't handle them, the implementation is broken.
As far as there being a time penalty for C++ strings versus char arrays, no biggie. Everyone here wastes more time in effed up code than that would ever hope to crowd in inefficiency. If the OP doesn't care to use C++ strings, that's his decision. That doesn't make it a wise one.
__________________
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|