Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jan 29th, 2006, 10:52 AM   #11
Symptom
Newbie
 
Symptom's Avatar
 
Join Date: Sep 2005
Posts: 28
Rep Power: 0 Symptom is on a distinguished road
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.
Symptom is offline   Reply With Quote
Old Jan 29th, 2006, 11:11 AM   #12
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
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
nnxion is offline   Reply With Quote
Old Jan 29th, 2006, 11:17 AM   #13
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
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.
Cache is offline   Reply With Quote
Old Jan 29th, 2006, 12:17 PM   #14
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
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;
}
Cache is offline   Reply With Quote
Old Jan 30th, 2006, 8:50 AM   #15
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Jan 30th, 2006, 8:56 AM   #16
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jan 30th, 2006, 9:26 AM   #17
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Jan 30th, 2006, 3:27 PM   #18
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
Quote:
Originally Posted by jayme
This also has to be done in a char, not a string. Thx
That's why I persisted with C-style strings during this thread. Also note: the string will always start of as a C-style string - since the string is to be filled from a recv operation (I have no doubt both of you already know this). Although 1024 bytes being copied to a std::string after the initial recv isn't such a penalty, I guess.
Cache is offline   Reply With Quote
Old Jan 30th, 2006, 3:32 PM   #19
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
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 .
Polyphemus_ is offline   Reply With Quote
Old Jan 30th, 2006, 3:52 PM   #20
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei 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 4:09 AM.

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