No, you shouldn't have to. But your code probably won't run as expected.
I have reformatted your code so it makes a little more sense:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <stdio.h>
int main()
{
char *pStr1 = "hello";
char vStr2[10];
char *pS = pStr1;
char *pT, = vStr2;
while(*t != NULL || *s != NULL)
{
++pT;
++pS;
*pT = *pS;
}
printf("%s\n", vStr2);
getc();
return 0;
}
|
Now I'm going to explain my changes and why I made them.
Line 5: I changed your array of undefined size to a pointer. No need to worry, they are essentially the same and so many operations still work on a pointer that do an array.
Line 10: Saying
while(*t=*s)
will always be true and will always set
*t
to
*s
. I also did bound checking (crude and rusty) in this line making it so you didn't corrupt memory.
IN THE LOOP: I have made it so each loop iteration will increment your pointers, moving them along the array in memory, and setting the destination to the source
*pT = *pS;
NOTE: This code may seem confusing at first, but that is because I am using a coding convention that makes sense to me. What is important here are the principles at work.