Whats wrong with the following function

char *string()
{
char *text[20];
strcpy(text,"Hello world");
return text;
}

Answers were Sorted based on User's Feedback



Whats wrong with the following function char *string() { char *text[20]; strcpy(text,&qu..

Answer / 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

Whats wrong with the following function char *string() { char *text[20]; strcpy(text,&qu..

Answer / vignesh1988i

as for as i know , there is only one error..... you have
declared text as array of pointers and not as character data
array..... so this text can only accept addresses.... :)

char *text[20] means you are going to store 20 addresses in
this array..... When you store addresses using arrays , the
that is called array of pointers....

if u declare : char text[20] , this will work correctly..



thank u

Is This Answer Correct ?    13 Yes 2 No

Whats wrong with the following function char *string() { char *text[20]; strcpy(text,&qu..

Answer / qint

1. returning address of a local variable.
2. wrong parameter passed to strcpy()

Is This Answer Correct ?    4 Yes 5 No

Post New Answer

More C Interview Questions

Which of these statements are false w.r.t File Functions? i)fputs() ii)fdopen() iii)fgetpos() iv)ferror() A)ii B)i,ii C)iii D)iv

6 Answers   Accenture,


how to go with this?

1 Answers   Wipro,


What is a list in c?

0 Answers  


What is sizeof return in c?

0 Answers  


Add Two Numbers Without Using the Addition Operator

0 Answers  






Take an MxN matrice from user and then sum upper diagonal in a variable and lower diagonal in a separate variables. Print the result

0 Answers  


main() { int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }

5 Answers   Vector, Vector Solutions,


int i=0,j; j=++i + ++i ++i; printf(" %d",j);

2 Answers   ME,


How do shell structures work?

0 Answers  


What is console in c language?

0 Answers  


Is double link list a linear data structure? If Yes, Why?If No, Why?

4 Answers  


How to add two numbers without using arithmetic operators?

18 Answers   College School Exams Tests, e track, Infosys, Pan Parag, Sapient, TCS,


Categories