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

Difference between pass by reference and pass by value?

0 Answers   TCS, TISL,


#include<stdio.h> main() { int i=5; printf("%d",i*i-- - --i*i*i++ + ++i); } tell the answer with correct reason .specially reason is important nt answer ans by turbo c is -39

1 Answers   GameLoft,


Write a Program to print this triangle: * ** * **** * ****** * ******** * ********** use two nested loops.

12 Answers   MIT, TCS,


main() { float a=3.2e40; printf("%d",a); }

9 Answers   Satyam,


what is associativity explain what is the precidence for * and & , * and ++ how the folloing declaration work 1) *&p; 2) *p++;

0 Answers   L&T,






What should malloc(0) do?

0 Answers  


What is the return type of sizeof?

0 Answers  


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

0 Answers  


what is computer engg

1 Answers  


Give basis knowledge of web designing ...

0 Answers   HCL,


Write a progarm to find the length of string using switch case?

0 Answers   TCS,


What do mean by network ?

0 Answers  


Categories