197k views
5 votes
Consider the following C program.#include int main(int argc, char *argv[]){ char *temp; strcpy(temp,argv[0]); return 0;}Why is the above code incorrect (i.e., likely to crash)?Write your explanation here:

1 Answer

3 votes

Answer:

Pointer temp is never set.

Step-by-step explanation:

To begin with the #include statement doesn't include anything, however that won't be a problem.

However temp is a pointer that is declared and then read from without having written anything to it.

Not only is the memory address it points to never written to, the address is never set and then attempted to read from. While this might compile, probably with warnings, the machine won't be able to execute it.

User Radream
by
5.3k points