f(char *p)
{
p=(char *)malloc(sizeof(6));
strcpy(p,"HELLO");
}
main()
{
char *p="BYE";
f(p)
printf("%s",p);
}
what is the output?

Answer Posted / smirnov_amm

It will show "HELL"
the f function reserve only 4 bytes with the sizeof(6). it will return the sizeof (int) =4 bytes.
So you only allocate 4 bytes.
When performing the strcpy, you will corupt memory because you'll copy more than allocate.

Cheers

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain what are multibyte characters?

600


What is the difference between array and pointer?

542


WHAT IS THE DEFINATION OF IN TECHNOLOGY AND OFF TECHNOLOGY ?

1833


Can the size of an array be declared at runtime?

581


Which is an example of a structural homology?

746






Is it possible to use curly brackets ({}) to enclose single line code in c program?

761


What is 'bus error'?

608


What is the function of volatile in c language?

639


What are structural members?

539


Simplify the program segment if X = B then C ← true else C ← false

2549


Explain what is the stack?

608


Explain can static variables be declared in a header file?

643


There is a practice in coding to keep some code blocks in comment symbols than delete it when debugging. How this affect when debugging?

786


Function which gives a pointer to a binary trees const an integer value at each code, return function of all the nodes in binary tree.?

595


Is c++ based on c?

628