After adding malloc ,this code compile and execute perfectly ... can u explain it?
'k' is a pointer. meaning it describes, or "points to," an address of memory.
when you first declare this pointer, there is no memory allocated for it and the address that it points to is undefined.
malloc() then causes a location of memory to be allocated (in your example for a single integer) and the pointer 'k' is assigned to the address of this allocated memory space.
the *value* contained at this memory space is still undefined, until the line of code
*k = 10 , which assigns the value 10 to the memory pointed to by 'k'
as
wildgoose suggested, to use malloc for a single integer is not very practical. malloc is for dynamically sizing arrays, not single variables. you should not get in the habit of using it like this. You should instead do it the way he showed you in
Post #2
in any event, it will make more sense to print the address as a hex value. use the format specifier '%p' to print a pointer as a hex address:
printf("address %p = %d\n", k, *k);
see
http://www.faqs.org/docs/learnc/c620.html
.