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

Why do we use main function?

635


hai iam working in sap sd module for one year and working in lumax ind ltd in desp department but my problem is i have done m.b.a in hr/marketing and working sap sd there is any combination it. can you give right solution of my problem. and what can i do?

1659


What is difference between main and void main?

627


What is size of union in c?

578


What is the difference between struct and typedef struct in c?

657






Do character constants represent numerical values?

844


What is pointer and structure in c?

571


What is the difference between procedural and declarative language?

650


how to create duplicate link list using C???

2074


Explain how can you be sure that a program follows the ansi c standard?

862


Why c is called a middle level language?

634


Explain how can a program be made to print the name of a source file where an error occurs?

686


What are the features of c language?

619


How do we declare variables in c?

566


What are different types of operators?

594