Pointer Problem

Thread Solved

Join Date: Apr 2009
Posts: 29
Reputation: Pavan_ is an unknown quantity at this point 
Solved Threads: 3
Pavan_ Pavan_ is offline Offline
Light Poster

Pointer Problem

 
0
  #1
Jul 6th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Pointer Problem

 
1
  #2
Jul 6th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 29
Reputation: Pavan_ is an unknown quantity at this point 
Solved Threads: 3
Pavan_ Pavan_ is offline Offline
Light Poster

Re: Pointer Problem

 
0
  #3
Jul 6th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 7
Reputation: trshaaa is an unknown quantity at this point 
Solved Threads: 2
trshaaa trshaaa is offline Offline
Newbie Poster

Re: Pointer Problem

 
0
  #4
Jul 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Pointer Problem

 
0
  #5
Jul 6th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,713
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 127
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Pointer Problem

 
0
  #6
Jul 6th, 2009
Originally Posted by Pavan_ View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 8
Reputation: cool_zephyr is an unknown quantity at this point 
Solved Threads: 0
cool_zephyr cool_zephyr is offline Offline
Newbie Poster

Re: Pointer Problem

 
0
  #7
Jul 7th, 2009
Originally Posted by Pavan_ View Post
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);
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2,022
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 219
tux4life's Avatar
tux4life tux4life is offline Offline
Postaholic

Re: Pointer Problem

 
0
  #8
Jul 7th, 2009
Originally Posted by cool_zephyr View Post
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/
"You can't build a reputation on what you are going to do."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 400 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC