char *ptr = malloc(sizeof(char));
printf("\nValue of ptr: %d\n",ptr);
printf("\nValue of address of ptr: %x\n",&ptr);
printf("\nValue of pointed by ptr: %c\n",*ptr);
printf("\nValue of address pointed by ptr: %x\n",&(*ptr));
*ptr='u';
printf("\nValue of pointed by ptr: %c\n",*ptr);
printf("\nValue of address pointed by ptr: %x\n",&(*ptr));
In the above piece of code, the following output is received:
Value of ptr: 1046113600
Value of address of ptr: a2fffa78
Value of pointed by ptr: P
Value of address pointed by ptr: 3e5a6d40
Value of pointed by ptr: u
Value of address pointed by ptr: 3e5a6d40
Does this imply that the default value of the malloc with a single char size is P?
Tried to allocate memory of single char size using malloc and initialized a pointer pointing to the allocated memory. Tried to print the value of the allocated memory.
mallocallocates memory without initializing it. So the allocated memory can contain any set of bit values. Neither default value is used.Opposite to malloc you can use another allocating function
callocthat zero initializes the allocated memory as for examplePay attention to that such calls of printf to output values of pointers like
are invalid and have undefined behavior. In particularly for example the conversion specifier
dis designed to output integers of the typeintandsizeof( int )can be equal to4whilesizeof( char * )can be equal to8.From the C Standard (7.21.6.1 The fprintf function)
You should use either the conversion specifier
pwith pointers as for exampleor conversion specifiers defined in header
<inttypes.h>