Whats wrong with the following function

char *string()
{
char *text[20];
strcpy(text,"Hello world");
return text;
}

Answer Posted / avinash

In this question ,two wrong thins ----
1.this is an array of char pointer so use
strcy(text[no.],"Hello World");

2.
we are copying a string without allocating memory to pointer . This is bug code .

correct solution :----

char *string()
{
char *text[20];
text[0]=malloc(20*sizeof (char));
strcpy(text,"Hello world");
return text;
}

Is This Answer Correct ?    15 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are global variables and explain how do you declare them?

580


how to construct a simulator keeping the logical boolean gates in c

1730


please explain clearly about execution of c program in detail,in which stage are the printf sacnf getting into exeecutable code

1708


What is array within structure?

587


Are there namespaces in c?

570






What is difference between && and & in c?

600


Explain the use of function toupper() with and example code?

655


What is the full form of getch?

585


pgm to find any error in linklist(in single linklist check whether any node points any of previous nodes instead of next node)

2201


What is a protocol in c?

559


Where are the auto variables stored?

626


which type of aspect you want from the student.

1703


Which control loop is recommended if you have to execute set of statements for fixed number of times?

811


Is a house a mass structure?

643


How do you list files in a directory?

563