943,929 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 509
  • C RSS
Jul 6th, 2009
0

Pointer Problem

Expand Post »
here is the code....

  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5. int *k;
  6. *k=10;
  7. printf("%d %d",k,*k);
  8.  
  9. return 0;
  10. }

it compile perfectly. but on executing it is giving segmentation fault.
where is the problem?
Similar Threads
Reputation Points: 26
Solved Threads: 3
Light Poster
Pavan_ is offline Offline
29 posts
since Apr 2009
Jul 6th, 2009
1

Re: Pointer Problem

BOOM! That's a technical term!

Your pointer needs to be pointing at real data!
  1. int *k, v;
  2. k = &v; // k now points to the address of {v}.
  3. *k=10; // So we are now storing the value
  4. // reference by k, meaning v is now equal to 10.
Last edited by wildgoose; Jul 6th, 2009 at 11:34 am. Reason: typo
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 6th, 2009
0

Re: Pointer Problem

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

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main()
  5. {
  6. int *k;
  7. k=(int*)malloc(sizeof(int));
  8.  
  9. *k=10;
  10. printf("%d %d",k,*k);
  11.  
  12. return 0;
  13. }

After adding malloc ,this code compile and execute perfectly ...
can u explain it?
Reputation Points: 26
Solved Threads: 3
Light Poster
Pavan_ is offline Offline
29 posts
since Apr 2009
Jul 6th, 2009
0

Re: Pointer Problem

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 :
  1. int *k;
  2. int a=10;
  3. k=&a // &a is address of variable a
  4. printf("%d", *k); //*k is pointing which address to print
  5. 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..
Last edited by trshaaa; Jul 6th, 2009 at 11:59 am.
Reputation Points: 18
Solved Threads: 2
Newbie Poster
trshaaa is offline Offline
7 posts
since Apr 2009
Jul 6th, 2009
0

Re: Pointer Problem

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.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 6th, 2009
0

Re: Pointer Problem

Click to Expand / Collapse  Quote originally posted by Pavan_ ...
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



.
Last edited by jephthah; Jul 6th, 2009 at 12:56 pm.
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jul 7th, 2009
0

Re: Pointer Problem

Click to Expand / Collapse  Quote originally posted by Pavan_ ...
Can u plz elaborate what dose a real data mean...

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main()
  5. {
  6. int *k;
  7. k=(int*)malloc(sizeof(int));
  8.  
  9. *k=10;
  10. printf("%d %d",k,*k);
  11.  
  12. return 0;
  13. }

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,
  1. int *k;
  2. k=(int *)malloc(6*sizeof(int));
  3. *k=10;
  4.  
  5. free(k);
Reputation Points: 8
Solved Threads: 7
Junior Poster in Training
cool_zephyr is offline Offline
66 posts
since Apr 2009
Jul 7th, 2009
0

Re: Pointer Problem

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???
http://www.cplusplus.com/reference/c.../cstdlib/free/
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: IIC Interrupt Handler
Next Thread in C Forum Timeline: Re: mp3 compression





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC