Newbie Question: Hi! Intended to study how one array populates another (initialised) array during copying. So I ran the following bit of code.
#include<stdio.h>
char strA[]= "\nThis is array 'a'.\n";
char strB[] = "ABCDEFGABCDEFGABCDEFG";
int main()
{
/* Copy one string to another using pointers */
char *pA, *pB;
puts(strA);
puts(strB);
pA=strA;
pB=strB;
puts(pA);
puts(pB);
while(*pA!='\0') {
*pB++ = *pA++;
puts(pB);
}
*pB='\0';
puts(strB);
return 0;
}
What I expected was to see how strA[] copied itself into strB[] at each step i.e. somewhere in the middle strB[] would have strA[] elements copied and remaining strB[] elements. But I could not find strA[] elements copied into strB[], though strB[] elements kept decreasing. The following is the output:
This is array 'a'.
ABCDEFGABCDEFGABCDEFG
This is array 'a'.
ABCDEFGABCDEFGABCDEFG
BCDEFGABCDEFGABCDEFG
CDEFGABCDEFGABCDEFG
DEFGABCDEFGABCDEFG
EFGABCDEFGABCDEFG
FGABCDEFGABCDEFG
GABCDEFGABCDEFG
ABCDEFGABCDEFG
BCDEFGABCDEFG
CDEFGABCDEFG
DEFGABCDEFG
EFGABCDEFG
FGABCDEFG
GABCDEFG
ABCDEFG
BCDEFG
CDEFG
DEFG
EFG
FG
G
This is array 'a'.
Process returned 0 (0x0) execution time : 0.025 s
Press any key to continue.
Am I missing something basic here? Any explanations would be of great help.
pBadvances throughstrB, thusputs(pB)will print the string from the current position. If you want to print the whole, partially overwritten string, callputs(strB)inside the loop.