What's wrong with "char *p; *p = malloc(10);"?

Answers were Sorted based on User's Feedback



What's wrong with "char *p; *p = malloc(10);"?..

Answer / shruti

the syntax of malloc is wrong.

in your example it should be written as:

char *p;

p = (char *)malloc(sizeof(char));

Is This Answer Correct ?    21 Yes 1 No

What's wrong with "char *p; *p = malloc(10);"?..

Answer / guest

The pointer you declared is p, not *p.

Is This Answer Correct ?    10 Yes 0 No

What's wrong with "char *p; *p = malloc(10);"?..

Answer / parasher

char *p;

p = (char *)malloc(sizeof(char));

Is This Answer Correct ?    8 Yes 2 No

What's wrong with "char *p; *p = malloc(10);"?..

Answer / raj

here syntox of malloc is not fallowed

Is This Answer Correct ?    3 Yes 1 No

What's wrong with "char *p; *p = malloc(10);"?..

Answer / clay

Here,

After char *p;
since pointer p is not initialized it is pointing at some
unknown location.

In the next step, the address of the memory allocated by
malloc() is stored at some garbage location pointed by p.

Here p is never initialized or never assigned any value.

Is This Answer Correct ?    0 Yes 3 No

Post New Answer

More C Interview Questions

Write the Program to reverse a string using pointers.

0 Answers   InterGraph,


What is the difference between %d and %i?

0 Answers  


How does C++ help with the tradeoff of safety vs. usability?

1 Answers  


Is main() is used in the program,,see below example? void main() { int i; for(i=0;i<10;i++) main(); } Then what is the output of the program?

6 Answers  


ratio,age,persentage

0 Answers  






what is op? for(c=0;c=1000;c++) printf("%c",c);

21 Answers   Trigent,


write an interactive program to generate the divisors of a given integer.

7 Answers   TCS,


Why cann't whole array can be passed to function as value.

1 Answers  


What is the difference between a free-standing and a hosted environment?

0 Answers   Aspire,


What is struct node in c?

0 Answers  


In header files whether functions are declared or defined?

1 Answers   TCS,


How can I do graphics in c?

0 Answers  


Categories