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 is extern variable in c with example?
Can an array be an Ivalue?
What is an array in c?
If null and 0 are equivalent as null pointer constants, which should I use?
what is the difference between class and unio?
What is static volatile in c?
Describe the header file and its usage in c programming?
Can you return null in c?
What is d'n in c?
Tell me what are bitwise shift operators?
Can a pointer point to null?
PROGRAM TO WRITE CONTENTS OF 1 FILE IN REVERSE TO ANOTHER FILE,PROGRAM TO COPY 1 FILE TO ANOTHER BY SPECIFYING FILE NAMES AS COMMAND LINE
What is the use of putchar function?
What is fflush() function?
What is 2 d array in c?