Posted by: arch119 August 2, 2005
Information technology
Login in to Rate this Post:     0       ?        

>>testdirector provided that he wants to copy q into p ,I don't think that's a typo. But with the same supposition, his first code snippet should be while(*p=*q){ p++; q++; } >>sojodude I haven't tried it in Microsoft's compiler but it should not give you a *link* error. It should rather end up in a segmentation fault (runtime error) [ I don't think Microsoft's compiler is so smart as to detect a runtime error at the linking stage ]. Reason: p points to "pqrst\0" but the latter being a string constant is placed in that part of the memory which can only be read but can't be written. An attempt to write on it will surely cause a runtime error. Btw, when defining a string constant you do not explicitly need to type the trailing "\0". The compiler will place a \0 at the end of the characters in double quotes. And here is a code snippet that copies q into p, you should point p to a region in the memory which can be both read and written. An array or dynamic allocation with malloc will do. char *p=(char *)malloc(6); char *q="abcde"; char *r = p; while(*p=*q){ p++; q++; } printf("%s",r); //will print "abcde"
Read Full Discussion Thread for this article