Does anyone have an example of copying a string without using strcpy?
Does anyone have an example of copying a string without using strcpy?
Just today Stack Overflow posted such, in Example 2.4.
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Doesn't anyone around here put forth any effort on their own? How the ........ do you think you'd do it? Seriously. Did you give this any thought at all?
"Hmm.. how would I copy a string myself?"
"Well, I'd start by writing the first letter. Then I'd move on to the next."
"I'd probably repeat that until I was done!"
"OMG! Could I do that in code!?"
"I bet I could do that while I subconciously think of something else to not put any effort into!"
"I mean, seriously, why bother, when everyone will do it for me?"
Quzah.
Hope is the first step on the road to disappointment.
Hello,
» Heh. This question gets asked alot. Speaking of effort, how's this:Originally Posted by quzah
Just for the fun of it. Never done it before, and my first time too. At least it compiles with -Wall, -pedantic, -ansi, and -std=c89 GCC flags.Code:unsigned int _O0(const char *_O0) { const char *_OO; _OO = _O0; while (*_OO) _OO++; return _OO - _O0; } void *_0O(void *_O0, const void *_OO, unsigned int _00) { while (_00--) ((char *)_O0)[_00] = ((char *)_OO)[_00]; return _O0; } int main() { char _00[5], _OO[5] = "_O0"; _0O(_00, _OO, _O0(_OO) + 1); return 0; }
- Stack Overflow
Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.
You're running across the reserved namespace here. Add a O to the beginning of all those underscores. Then you'll get something like this!
Quzah.
Hope is the first step on the road to disappointment.
Code:#include <stdio.h> int main() { int i = 0; char str1[30], str2[30]; printf("Insert str1\n"); scanf("%s", &str1); while(str1[i] != '\0') { str2[i] = str1[i]; i++; } printf("Str1 looks like ... %s\n", str1); printf("Str2 looks like ... %s\n", str2); return 0; }
Last edited by xxxrugby; 02-05-2005 at 06:11 PM.
Sorry for spelling errors, not English!
xxxrugby: "All Human Race Will Die From My Hand!"
xxxrugby: "We are all philosophers, when question is about politics!"
Well, gee, it's sure a good thing you did that, because none of us knew how. I mean, the only reason we didn't just post full working code is because we couldn't figure it out.
Oh, and your code is wrong. The second string won't have a null terminator, and when you try and use it, you'll merrily run off the end of your array.
Also, your printf statement is wrong.
So if you are actually going to do everything for someone, make sure you're actually doing it correctly.
Quzah.
Hope is the first step on the road to disappointment.
Ahh, and the system call at the end just wraps it up. Lets try to stay away from system specific implementations for example programs unless you specify where the system dependency lies.
Take a look at the FAQ
i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced
It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah
Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem
quazah I am a newbie. And I sow your code up, and I dont understand it. So I think what question is. That maybe the man is also like I and will not understand a bit.
I see where my printf statment is wrong. Thanks.And I am not gee. As you can see.Code:printf("Str.... %s", str1);
Thanks AndyHunter I usualy delite system commands, but I forgot now.
Last edited by xxxrugby; 02-05-2005 at 07:15 PM.
Sorry for spelling errors, not English!
xxxrugby: "All Human Race Will Die From My Hand!"
xxxrugby: "We are all philosophers, when question is about politics!"
Well you've edited your post as to make your printf statements correct now. However, you had them wrong. You don't use the & operator when using printf, unless you're trying to print the address of a varaible.
You had:
Your quoted code is also wrong, because you have:Code:printf( "foo is %s", &foo );
Which is wrong because:Code:printf("Str.... &d", str1);
1) You would be using %, and not the & there.
2) If that's supposed to be a specifier, you've got d, and if that's a string, it should be s.
Which would end up:
Code:printf("Str.... %s", str1);
Quzah.
Hope is the first step on the road to disappointment.
As a function:
Code:void mystrcpy(char *d, const char *s) { int x; for(x = 0; s[x] != '\0'; x++) d[x] = s[x]; d[x] = '\0`; }