🔒 Locked
simple question
Advertisement
Original Post
Im taking a programming class where I had to implement my own version of C''s strcpy. I did it like this
char *cpy(char *a, char *b)
{
while(*b != ''\0'')
{
*a = *b;
a++;
b++;
}
*a = ''\0'';
return a;
}
but I did try it like this and couldnt figure out why this caused a fatal error at the end of execution
char *cpy(char *a, char *b)
{
while(*b != ''\0'')
{
*a = *b;
a++;
b++;
}
while(*a != ''\0'')
{
*a = ''\0'';
a++;
}
return a;
}
It doesnt really matter now that I got it to work, but Im just curious as to why the latter would cause an error.
Anyone have any ideas? =)
Thanks for your time, John
You make the assumption that the end of an array terminates with a ''\0'' character. This is rarely (and certainly not defined to be) the case. It is only the immediate end of a string present in an array that terminates with a ''\0''.
The problem with that assumption is that you will likely overrun past the end of the array, and probably get an access violation.
-Mezz
The problem with that assumption is that you will likely overrun past the end of the array, and probably get an access violation.
-Mezz
in your second function, in the second while loop, a is initially pointing to the first character after the copied string, right? And you were right the first time, all you need to do is set *a to null. I assume in your second example that you are trying to neatly set the remaining space to nulls. This isn't going to work though because the rest of the buffer is just junk, and probably won't contain a null (there isn't automatically one at the end of a buffer). So what's happening is your loop is just writing nulls indefinitly (and usually beond the buffer, hence the fault).
PS: shouldn't you be returning a pointer to the start of string a, not the end as you are at the moment?
Edit: hmm... I probably should have checked to see if anyone had replied already before I posted
[edited by - frankd on August 4, 2002 7:12:43 PM]
PS: shouldn't you be returning a pointer to the start of string a, not the end as you are at the moment?
Edit: hmm... I probably should have checked to see if anyone had replied already before I posted
[edited by - frankd on August 4, 2002 7:12:43 PM]
Both versions have several errors and will likely crash/cause errors.
First of all, in both versions you''re modifying the pointers that are passed to the function. a and b should still point to the first character of their respective strings when the function is finished, think about what your code is actually doing with them.
Your second implementation is crashing because you''re incrementing a after writing the terminating \0.
First of all, in both versions you''re modifying the pointers that are passed to the function. a and b should still point to the first character of their respective strings when the function is finished, think about what your code is actually doing with them.
Your second implementation is crashing because you''re incrementing a after writing the terminating \0.
quote:
Original post by Dobbs
...
First of all, in both versions you''re modifying the pointers that are passed to the function. a and b should still point to the first character of their respective strings when the function is finished, think about what your code is actually doing with them.
Changing the position of the pointers a and b doesn''t affect anything outside the function. The function has a copy of the addresses passed to it which are stored in the local variables a and b.
You're missing a semi-colon ; after your while loop's ending curly brace }
change your code to the following and is should work.
char *cpy(char *a, char *b) {
while(*b != '\0') {
*a = *b;
a++;
b++;
}; // <<< Added ;
*a = '\0';
return a;
}
"I thought Genius lived in bottles..." - Patrick Star
[edited by - Xanth on August 4, 2002 8:31:55 PM]
change your code to the following and is should work.
char *cpy(char *a, char *b) {
while(*b != '\0') {
*a = *b;
a++;
b++;
}; // <<< Added ;
*a = '\0';
return a;
}
"I thought Genius lived in bottles..." - Patrick Star
[edited by - Xanth on August 4, 2002 8:31:55 PM]
daerid: Safe to assume the string he is copying FROM is terminated with a ''\0''. Not the array he is copying TO (which is why he is probably overrunning the end).
Take for instance, the case where the character array ''b'' contains nothing in it''s string, i.e. it was initialised perhaps like so:
char my_array[6] = "";
Then the first loop in the second function would never execute. So what are you comparing the array ''a'' to now? Well, nothing really because you have no idea what it contains. The only way you can neatly assign ''\0'' to the rest of the array (as somebody else noticed, this is probably your intention) is to know the size of the array to begin with, because there is nothing to say where the end of an array is.
-Mezz
Take for instance, the case where the character array ''b'' contains nothing in it''s string, i.e. it was initialised perhaps like so:
char my_array[6] = "";
Then the first loop in the second function would never execute. So what are you comparing the array ''a'' to now? Well, nothing really because you have no idea what it contains. The only way you can neatly assign ''\0'' to the rest of the array (as somebody else noticed, this is probably your intention) is to know the size of the array to begin with, because there is nothing to say where the end of an array is.
-Mezz
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement