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

Efficient data structure for store/search list of 1000 records a)array b)double linked list c)circular queue d)hash table

3 Answers   Value Labs,


How are 16- and 32-bit numbers stored?

0 Answers  


What are lookup tables in c?

0 Answers  


What does 1f stand for?

0 Answers  


Can variables be declared anywhere in c?

0 Answers  


What is non linear data structure in c?

0 Answers  


Why doesn't the code "int a = 1000, b = 1000; long int c = a * b;" work?

7 Answers  


Write a program to generate prime factors of a given integer?

2 Answers  


Can a function be forced to be inline? Also, give a comparison between inline function and the C macro?

0 Answers   Genpact,


Mention four important string handling functions in c languages .

0 Answers  


WAP TO ACCEPT STRING AND COUNT A COMES N TIMES B COMES N TIMES C COMES N TIMES D COMES N TIMES AND SO ON......... AT LAST UNTIL Z COMES N TIMES...............

3 Answers  


Can we assign integer value to char in c?

0 Answers  


Categories