here is the code....

#include<stdio.h>

int main()
{
       int  *k;
       *k=10;
       printf("%d %d",k,*k);

       return 0;
}

it compile perfectly. but on executing it is giving segmentation fault.
where is the problem?

BOOM! That's a technical term!

Your pointer needs to be pointing at real data!

int  *k, v;
       k = &v;            // k now points to the address of {v}.
       *k=10;           // So we are now storing the value
                            // reference by k, meaning v is now equal to 10.
Comments
correct

Can u plz elaborate what dose a real data mean...

#include<stdio.h>
#include<stdlib.h>

int main()
{
       int  *k;
       k=(int*)malloc(sizeof(int));

       *k=10;
       printf("%d %d",k,*k);

       return 0;
}

After adding malloc ,this code compile and execute perfectly ...
can u explain it?

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 :

int *k;
int a=10;
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 :
j=&(k+5)
j=&(a==b)
&a=&b
&a=150 (j , a, b random variables)
if you don't understand why just ask..:)

Real Data? You are pointing nowhere! Whatever value was in your pointer at init time is where it will try to point! But there is no assigned memory there to store that value!

Why are you using malloc() for a simple integer? But if you do use malloc() don't forget your release!

You're now trying to print the address and the value in signed decimal? Printing the address isn't going to make any sense! But if you do, you may want to print it in hex.

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

.

Can u plz elaborate what dose a real data mean...

#include<stdio.h>
#include<stdlib.h>

int main()
{
       int  *k;
       k=(int*)malloc(sizeof(int));

       *k=10;
       printf("%d %d",k,*k);

       return 0;
}

After adding malloc ,this code compile and execute perfectly ...
can u explain it?

when we do free the memory space reserved by malloc..does it free only the memory space not being used at that time or the whole memory space allocated by malloc???

for example,

int *k;
k=(int *)malloc(6*sizeof(int));
*k=10;

free(k);
This question has already been answered. Start a new discussion instead.