Whats wrong with the following function

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

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

An organised method of depicting the use of an area of computer memory used to signify the uses for different parts of the memory a) swap b) extended memory c) memory map d) all of the above

798


Is it better to bitshift a value than to multiply by 2?

738


write a c program to find the sum of five entered numbers using an array named number

1732


How do you determine a file’s attributes?

697


What is #define?

684






What is variable declaration and definition in c?

580


Implement bit Array in C.

780


Under what circumstances does a name clash occur?

791


Which of these functions is safer to use : fgets(), gets()? Why?

727


an expression contains relational operators, assignment operators, and arithmatic operstors. In the absence of parentheses, they will be evaluated in which of the following order a) assignment, relational, arithematic b) arithematic, relational, assignment c) relational, arithematic, assignment d) assignment, arithematic, relational

932


Why is sprintf unsafe?

709


Explain built-in function?

691


What are the standard predefined macros?

726


When do we get logical errors?

725


i have a written test for microland please give me test pattern

2305