On Sun, Jun 06, 2004 at 07:31:11PM +0200, cstrato wrote: > I apologize for a simple non-root question, but sorrowly I have > nobody else to ask: > Sometimes I have statements such as the following: > if (strcmp(name, "option") == 0) { > char *option; > option = strcpy(option, opt); > } > > Although it is correct, I get the following compiler warning: > "warning: `char*option' might be used uninitialized in this function" This is not a minor warning; it is an error. The code above works by luck. If your application is big and run for enough time, it will eventually crash (you resolve these errors using valgrind or electric fence). >From the man page of strcpy(3): The strcpy() function copies the string pointed to by src (including the terminating `\0' character) to the array pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy. The destination string here is the 'option' pointer and you have not allocated any space for it. > How can I prevent such compiler warnings? > Would it be save to write: > char *option = ""; > (If this is not save, why?) No, this maybe elliminate the warning, but it is still an error. Use malloc() to allocate enough space (don't forget to reserve one char for the null termination). Regards, -- University of Athens I bet the human brain Physics Department is a kludge --Marvin Minsky
This archive was generated by hypermail 2b29 : Sun Jan 02 2005 - 05:50:08 MET