you can't use *k=10, because pointer points to place in memory...10 doesn't have place in memory its just a number.you need to have variable on which pointer is going to point (which has place in memory ). for example :
k=&a // &a is address of variable a
printf("%d", *k); //*k is pointing which address to print
printf("%d", k);//prints address of variable a
also incorrect :
&a=150 (j , a, b random variables)
if you don't understand why just ask..:)
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