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

Can a function argument have default value?

0 Answers   Genpact,


I need testPalindrome and removeSpace #include <stdio.h> #define SIZE 256 /* function prototype */ /* test if the chars in the range of [left, right] of array is a palindrome */ int testPalindrome( char array[], int left, int right ); /* remove the space in the src array and copy it over to the "copy" array */ /* set the number of chars in the "copy" array to the location that cnt points t */ void removeSpace(char src[], char copy[], int *cnt); int main( void ) { char c; /* temporarily holds keyboard input */ char string[ SIZE ]; /* original string */ char copy[ SIZE ]; /* copy of string without spaces */ int count = 0; /* length of string */ int copyCount; /* length of copy */ printf( "Enter a sentence:\n" ); /* get sentence to test from user */ while ( ( c = getchar() ) != '\n' && count < SIZE ) { string[ count++ ] = c; } /* end while */ string[ count ] = '\0'; /* terminate string */ /* make a copy of string without spaces */ removeSpace(string, copy, &copyCount); /* print whether or not the sentence is a palindrome */ if ( testPalindrome( copy, 0, copyCount - 1 ) ) { printf( "\"%s\" is a palindrome\n", string ); } /* end if */ else { printf( "\"%s\" is not a palindrome\n", string ); } /* end else */ return 0; /* indicate successful termination */ } /* end main */ void removeSpace(char src[], char copy[], int *cnt) { } int testPalindrome( char array[], int left, int right ) { }

0 Answers  


what are the advanced features of functions a) function declaration and prototypes b) calling functions by value or by reference c) recursion d) all the above

0 Answers  


Which is better pointer or array?

0 Answers  


What is integer constants?

0 Answers  


What are identifiers in c?

0 Answers  


What does sizeof function do?

0 Answers  


What is the difference between malloc() and calloc()?

3 Answers  


What are bit fields? What is their use?

2 Answers   Adobe,


Is this program statement valid? INT = 10.50;

0 Answers  


Describe the modifier in c?

0 Answers  


Write a program or provide a pseudo code to flip the 2nd bit of the 32 bit number ! (Phone Screen)

1 Answers   NetApp, PTU, Wipro,


Categories