![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 1
Rep Power: 0
![]() |
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); ? |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
strcpy is a trivial function to write.
char *my_strcpy ( char *dst, const char *src )
{
char *p = dst;
while ( ( *p++ = *src++ ) != '\0' )
;
return dst;
} |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Mar 2005
Location: Washington
Posts: 90
Rep Power: 4
![]() |
const char is "const", which means constant variables can not be changed.
If there is a way, please let me know as well, k. |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
>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. |
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Mar 2005
Location: USA
Posts: 60
Rep Power: 4
![]() |
Quote:
|
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 4
![]() |
>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. |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
eggbert rules!!!
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|