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

Can we change the value of #define in c?

0 Answers  


what are the difference between ANSI C and Let Us c and Turbo C

4 Answers   LG Soft,


can we declare a variable in different scopes with different data types? answer in detail

3 Answers   TCS,


Explain why c is faster than c++?

0 Answers  


how to solve "unable to open stdio.h and conio.h header files in windows 7 by using Dos-box software

0 Answers  


can any one tel me wt is the question pattern for NIC exam

0 Answers   NIC,


Differentiate between calloc and malloc.

0 Answers   Wipro,


whats the use of header file in c?

2 Answers  


Write code for initializing one dimentional and two dimentional array in a C Program?

5 Answers   Deshaw, Edutech, GMD,


how many argument we can pas in in a function

25 Answers   CTS,


explain what is fifo?

0 Answers  


writ a program to compare using strcmp VIVA and viva with its output.

0 Answers  


Categories