K
Khookie
Hi everyone
Out of curiosity, would this be a valid implementation of a string
copy function?
int copy(char* to, char* from) {
while (*to++ = *from++);
}
It's just that, I notice in string.h that it makes the 'from' argument
a constant argument.
char *strcpy( char *to, const char *from );
Thus I suppose there needs to be some other pointer initialised to
walk the from string.
Is there a reason for this? I thought since the pointers are passed
by value to the function, you wouldn't need to need to worry about the
them being changed. Well, I thought you could save on the
initialisation of another pointer, but given my ignorance of the
language, I'm sure there's a good reason behind the const.
Sorry if it sounds terribly pedantic, I'm just interested in learning
why things are done a certain way.
Chris
Out of curiosity, would this be a valid implementation of a string
copy function?
int copy(char* to, char* from) {
while (*to++ = *from++);
}
It's just that, I notice in string.h that it makes the 'from' argument
a constant argument.
char *strcpy( char *to, const char *from );
Thus I suppose there needs to be some other pointer initialised to
walk the from string.
Is there a reason for this? I thought since the pointers are passed
by value to the function, you wouldn't need to need to worry about the
them being changed. Well, I thought you could save on the
initialisation of another pointer, but given my ignorance of the
language, I'm sure there's a good reason behind the const.
Sorry if it sounds terribly pedantic, I'm just interested in learning
why things are done a certain way.
Chris