/
stringcopy.c
61 lines (54 loc) · 1.27 KB
/
stringcopy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
// These iterations show the slow learning process of my poor brain.
// The goal was to implement and learn a string copy 'while (*s++ = *t++)'
*/
#include <stdio.h>
#include <stdlib.h>
/*
//won't work b/c of error: lvalue required as increment operand
int main()
{
char * word = "Sorrell";
char newWord[sizeof(word)];
while (*newWord++ = *word++ ) {};
printf("word:%s and newWord:%s\n", word, newWord);
return 0;
}
*/
/*
//works, but not that elegant
int main()
{
char * word = "Sorrell";
char newWord[sizeof(word)];
int i = 0;
while (newWord[i] = *word++ ) {i++;};
word = &word[-sizeof(word)];
printf("word:%s and newWord:%s\n", word, newWord);
}
*/
/*
// works ... and how! but maybe even LESS elegant
int main()
{
char * word = "Sorrell";
char * newWord = (char*) malloc (sizeof(word));
char *p = &newWord[0];
char *q = &word[0];
while (*p++ = *q++) {};
printf("%s\n", word);
printf("%s\n", newWord);
}
*/
/*
// finally, the desired outcome
int main()
{
char * word = "Sorrell";
char * newWord = (char*) malloc (sizeof(word));
while (*newWord++ = *word++) {};
word = &word[-sizeof(word)]; //reset the pointer back to the beginning mem location
newWord = &newWord[-sizeof(newWord)]; // same as above
printf("word:%s and newWord:%s\n", word, newWord);
}
*/