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
How do you redirect a standard stream?
What is use of #include in c?
main() { printf("hello"); fork(); }
Can we access array using pointer in c language?
What are the 4 types of programming language?
Difference between macros and inline functions? Can a function be forced as inline?
What is the g value paradox?
What is .obj file in c?
Function calling procedures? and their differences? Why should one go for Call by Reference?
What is the difference between array_name and &array_name?
which is an algorithm for sorting in a growing Lexicographic order
Explain what is the stack?
Do you know what are bitwise shift operators in c programming?
What is use of pointer?
What is string function in c?