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 is extern variable in c with example?

744


Can an array be an Ivalue?

860


What is an array in c?

771


If null and 0 are equivalent as null pointer constants, which should I use?

800


what is the difference between class and unio?

2087


What is static volatile in c?

734


Describe the header file and its usage in c programming?

806


Can you return null in c?

841


What is d'n in c?

805


Tell me what are bitwise shift operators?

865


Can a pointer point to null?

780


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

1677


What is the use of putchar function?

827


What is fflush() function?

872


What is 2 d array in c?

739