#include <stdio.h>
void copystr(char *, char * );
int main(void)
{
// pushes the letters 'H', 'i', 's', ' ', ... onto the stack and stores the address of the 'H' in the char* "aa"
char aa[]={"His name was John"}; // declare char array and initialize
// this puts another char* on the stack called "bb" - "[sizeof(aa)]"
// should be a compile-time error - if not it does nothing (that i know - maybe they've changed the C language since i used it).
char bb[ sizeof(aa) ]; // declare char array of equal size
// this should be changed to char* paa; char* pbb; (or char *paa, *pbb;)
// this line would then push 2 char*s called "paa", and "pbb" on the stack (unitialized)
char * paa,pbb ; // declare char pointer
// this puts the value of "aa" (the address of the 'H' on the stack) into "paa"
paa=aa; // initialize char pointer to &aa[], or aa common short hand.
// this puts the value of "bb" (unitialized) into "pbb" (oops)
pbb=bb; // initialize char pointer to &bb[], or bb common short hand.
// this prints the 'H', 'i', 's'... on the stack - stops at null (\0) - not sure if compiler will add one by default - if not
// this will continue printing the pointer values and any other trash on stack until a null is found
printf("Contents of array aa[]: %s\n", paa ); // print string starting at &aa[0]
// this prints the bytes at the (unitialized, i.e., random) value stored in "pbb" until a \0 is found
printf("Contents of array bb[]: %s\n", pbb ); // print string starting at &bb[0]
printf("Calling function copystr.\n");
// we'll work on this next...
copystr( paa, pbb) ; // copy contents of aa[] to bb[]
printf("Contents of array bb[]: %s\n", pbb);
return 0;
}
void copystr(char *str1, char *str2 )
{
int ii;
for( ii=0; *(str1+ii); ii++); // ii initialize to zero
// while thing-at position in array is not NULL
// do block
// increment position in array by 1
{
*(str2+ii)=*(str1+ii);
}
printf("Done copying array\n");
}