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
What are global variables and explain how do you declare them?
how to construct a simulator keeping the logical boolean gates in c
please explain clearly about execution of c program in detail,in which stage are the printf sacnf getting into exeecutable code
What is array within structure?
Are there namespaces in c?
What is difference between && and & in c?
Explain the use of function toupper() with and example code?
What is the full form of getch?
pgm to find any error in linklist(in single linklist check whether any node points any of previous nodes instead of next node)
What is a protocol in c?
Where are the auto variables stored?
which type of aspect you want from the student.
Which control loop is recommended if you have to execute set of statements for fixed number of times?
Is a house a mass structure?
How do you list files in a directory?