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

What is the argument of a function in c?

0 Answers  


how to write hello word without using semicolon at the end?

6 Answers   Accenture,


Why is c known as a mother language?

0 Answers  


what r callback function?

1 Answers  


Write a C program that computes the value ex by using the formula ex =1+x/1!+x2/2!+x3+3!+………….

1 Answers  






44.what is the difference between strcpy() and memcpy() function? 45.what is output of the following statetment? 46.Printf(“%x”, -1<<4); ? 47.will the program compile? int i; scanf(“%d”,i); printf(“%d”,i); 48.write a string copy function routine? 49.swap two integer variables without using a third temporary variable? 50.how do you redirect stdout value from a program to a file? 51.write a program that finds the factorial of a number using recursion?

6 Answers   Amdocs,


Is there sort function in c?

0 Answers  


What is static identifier?

0 Answers   TCS,


What is an example of enumeration?

1 Answers  


How will you delete a node in DLL?

0 Answers   GrapeCity,


What is meant by int fun const(int a, int b) { .... ... }

1 Answers  


15.what is the disadvantage of using macros? 16.what is the self-referential structure? 17.can a union be self-referenced? 18.What is a pointer? 19.What is the Lvalue and Rvalue? 20.what is the difference between these initializations? 21.Char a[]=”string”; 22.Char *p=”literal”; 23.Does *p++ increment p, or what it points to?

2 Answers   CTS,


Categories