Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 16th, 2005, 3:39 PM   #1
ike0000
Newbie
 
Join Date: Apr 2005
Posts: 1
Rep Power: 0 ike0000 is on a distinguished road
C++ * programming question

Can anyone tell me how to go about copying a const char * to a char * without using anything from string or cstring (strlen, strcpy, etc.)?
For example, how can I copy const char * name to char * newname without doing something like:
newname = new char [strlen (name) + 1];
strcpy (newname, name); ?
ike0000 is offline   Reply With Quote
Old Apr 16th, 2005, 3:54 PM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
strcpy is a trivial function to write.
char *my_strcpy ( char *dst, const char *src )
{
  char *p = dst;

  while ( ( *p++ = *src++ ) != '\0' )
    ;

  return dst;
}
Eggbert is offline   Reply With Quote
Old Apr 17th, 2005, 10:39 PM   #3
pal
Programmer
 
pal's Avatar
 
Join Date: Mar 2005
Location: Washington
Posts: 90
Rep Power: 4 pal is on a distinguished road
const char is "const", which means constant variables can not be changed.
If there is a way, please let me know as well, k.
pal is offline   Reply With Quote
Old Apr 18th, 2005, 12:57 PM   #4
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>const char is "const", which means constant variables can not be changed.
Correct.

>If there is a way, please let me know as well, k.
A way to do what? There are no unanswered questions on this thread.
Eggbert is offline   Reply With Quote
Old Apr 18th, 2005, 1:31 PM   #5
BaroN NighT
Programmer
 
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4 BaroN NighT is on a distinguished road
Quote:
Originally Posted by Eggbert
>If there is a way, please let me know as well, k.
A way to do what? There are no unanswered questions on this thread.
A way that you can copy char to another char without using strcpy I guess. strcpy is an ease of use function. Why would you like to find a different way?
BaroN NighT is offline   Reply With Quote
Old Apr 18th, 2005, 7:07 PM   #6
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>A way that you can copy char to another char without using strcpy I guess.
Then the function I gave should be answer enough.

>Why would you like to find a different way?
Anyone can use library functions, but good programmers also know how they work under the hood. The only way to truly learn how they work is to implement them.
Eggbert is offline   Reply With Quote
Old Apr 20th, 2005, 2:30 AM   #7
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
eggbert rules!!!
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja 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 6:58 PM.

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