I write my own version of strcpy(). I learn it from http://pweb.netcom.com/~tjensen/ptr/ch3x.htm .
So.. here is the source code:
#include <stdio.h>
char *my_strcpy(char *dst, char *src);
char *my_strcpy(char *dst, char *src)
{
char *ptr = dst;
while (*src)
{
*ptr++ = *src++;
}
*ptr = '\0';
return dst;
}
int main(int argc, const char *argv[])
{
char strA[] = "Awesome string! Yes it is G-String!";
char strB[20];
my_strcpy(strB, strA);
puts(strB);
return 0;
}
In function main() i experiment to change:
char strA[] = "Awesome string! Yes it is G-String!";
char strB[20];
become
char *strA = "Awesome string! Yes it is G-String!";
char *strB;
And, yay! it works! Then the question is, How char *strB
and char *ptr = dst
(inside my_strcpy() ) work?
In my understanding, they not have more than one space in memory. They only have one space in memory since char *strB
not initialized as like char *strA
. Whether it can be considered as a automatic / dynamic space creation in memory? How the data stored?
Please enlightenment
Thank you :)