User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 374,185 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,470 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 334 | Replies: 6 | Solved
Reply
Join Date: May 2008
Posts: 3
Reputation: umeshjaviya is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
umeshjaviya umeshjaviya is offline Offline
Newbie Poster

pointer passing problem

  #1  
May 1st, 2008
I am a new guy in C. But I have read stuffs about Malloc, Realloc, how to pass pointers to the function...

If I am writing this code in single program, it works perfectly...but smart programmmer guide says write in compact form.

So with user defined function this is not giving me desired output. I dont know what mistake I am going in this simple program.

*pointer takes all the values correctly in function resize_long_pointer(...), but when it comes to main function it is not working..even it is crashing with large number for N_Cell_total....

Please Help me regarding this problem

Hear is my programe....

Code:
  1. #include <stdafx.h>
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6.  
  7. void main()
  8. {
  9. size_t size;
  10. long *CF5,i;
  11. long N_Cell_total;
  12.  
  13. void resize_long_pointer(long *,long *);
  14.  
  15. N_Cell_total = 1;
  16.  
  17. CF5 = (long *)malloc((N_Cell_total + 2) * sizeof(long));
  18.  
  19. for(i=1;i<=N_Cell_total;i++)
  20. {
  21. CF5[i] = i;
  22. printf("\ntest1 %ld",CF5[i]);
  23. }
  24.  
  25. N_Cell_total = 20000;
  26.  
  27. resize_long_pointer(CF5,&N_Cell_total);
  28.  
  29. for(i=1;i<=N_Cell_total;i++)
  30. {
  31. printf("\ntest2 i %ld CF5 %ld",i,CF5[i]);
  32. }
  33.  
  34. }
  35.  
  36. void resize_long_pointer(long *pointer,long *add_size)
  37. {
  38. long int_tmp,i;
  39. size_t size;
  40.  
  41. int_tmp = *add_size;
  42.  
  43. printf("\nAdditional size :: %ld",int_tmp);
  44.  
  45. if( (pointer = (long *) realloc( pointer, (int_tmp + 2) * sizeof(long) )) == NULL)
  46. printf("\nPointer resizing problem :: add_size : %ld",*add_size);
  47.  
  48. for(i=1;i<= int_tmp;i++)
  49. {
  50. *pointer = i;
  51. }
  52.  
  53. printf("\ntest2 pointer[%ld] %ld ",i-1,pointer[i-1]);
  54. }
output::

Code:

test1 1
Additional size :: 100
function test1 i 1 pointer 1
function test1 i 2 pointer 2
.......
function test1 i 3 pointer 100
test2 pointer[100] 100
test2 i 1 CF5 1
test2 i 2 CF5 -842150451
test2 i 3 CF5 -33686019
.......
test2 i 100 CF5 43807
Last edited by Ancient Dragon : May 1st, 2008 at 8:39 pm. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,184
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 34
Solved Threads: 822
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: pointer passing problem

  #2  
May 1st, 2008
line 27, you have to pass the pointer CF5 by reference, you are only passing it by value. I only show the corrections to a few lines. Function resize_long_pointer() will require several other corrections to accommodate the double star (pointer to a pointer)
// line 13:
void resize_long_pointer(long **,long *);
...
// line 27
resize_long_pointer(&CF5,&N_Cell_total); 

// line 36
void resize_long_pointer(long **pointer,long *add_size)

Another option is to change resize_long_pointer() to return a pointer

long * resize_long_pointer(long *,long *);
Last edited by Ancient Dragon : May 1st, 2008 at 8:48 pm.
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,394
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 15
Solved Threads: 260
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampire Moderator

Re: pointer passing problem

  #3  
May 1st, 2008
A couple of other notes:

- void main is evil. Don't use it.
- You shouldn't have your function prototype inside main(). Move line 13 to somewhere before main()
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: umeshjaviya is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
umeshjaviya umeshjaviya is offline Offline
Newbie Poster

Re: pointer passing problem

  #4  
May 2nd, 2008
Hello Friends,

Thank you very much for your kind help......
The correctly working code is given below......
What I understand that .... user define functions can communicate with main function through pointers only...so when I need to pass pointer and its value then I need to pass pointer of pointer (**pointer) but not pointer (*pointer) only.


  1. #include <stdafx.h>
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. void resize_long_pointer(long **,long *);
  7.  
  8. void main()
  9. {
  10. size_t size;
  11. long *CF5,i;
  12. long N_Cell_total;
  13.  
  14.  
  15.  
  16. N_Cell_total = 1;
  17.  
  18. CF5 = (long *)malloc((N_Cell_total + 2) * sizeof(long));
  19.  
  20. for(i=1;i<=N_Cell_total;i++)
  21. {
  22. CF5[i] = i;
  23. printf("\ntest1 %ld",CF5[i]);
  24. }
  25.  
  26. N_Cell_total = 200000;
  27.  
  28. resize_long_pointer(&CF5,&N_Cell_total);
  29.  
  30. for(i=1;i<=N_Cell_total;i++)
  31. {
  32. printf("\ntest2 i %ld CF5 %ld",i,CF5[i]);
  33. }
  34.  
  35. }
  36.  
  37. void resize_long_pointer(long **pointer2,long *add_size)
  38. {
  39.  
  40. long int_tmp,i,*pointer;
  41. size_t size;
  42.  
  43.  
  44. int_tmp = *add_size;
  45.  
  46. pointer = *pointer2;
  47.  
  48. printf("\nAdditional size :: %ld",int_tmp);
  49.  
  50. if( (pointer = (long *) realloc( pointer, (int_tmp + 2) * sizeof(long) )) == NULL)
  51. printf("\nPointer resizing problem :: add_size : %ld",*add_size);
  52.  
  53. for(i=1;i<= int_tmp;i++)
  54. {
  55. pointer[i] = i;
  56. //printf("\nfunction test1 i %ld pointer %ld",i,pointer[i]);
  57. }
  58.  
  59. printf("\ntest2 pointer[%ld] %ld ",i-1,pointer[i-1]);
  60.  
  61. *pointer2 = pointer;
  62. }
Last edited by Ancient Dragon : May 2nd, 2008 at 8:47 am. Reason: add code tags
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,184
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 34
Solved Threads: 822
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: pointer passing problem

  #5  
May 2nd, 2008
The only reason to pass a pointer to a pointer is so that the function can change the value of the original pointer that was declared in main().

>>for(i=1;i<= int_tmp;i++)
That line is incorrect. arrays are numbered 0, 1, ... Here is the correction
for(i=0; i < int_tmp; i++)

>>for(i=1;i<=N_Cell_total;i++)
From your original post in main() -- that line is also wrong for the same reason
for(i=0; i < N_Cell_total; i++)

And don't be afraid to make liberal use of white space, it make the code easier to read.
Last edited by Ancient Dragon : May 2nd, 2008 at 8:55 am.
'Politics' is made up of two words, 'poli,' which is Greek for 'many,' and 'tics,' which are blood-sucking insects.
- Gore Vidal
Being ignorant is not so much a shame as being unwilling to learn. - Benjamin Franklin
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: umeshjaviya is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
umeshjaviya umeshjaviya is offline Offline
Newbie Poster

Re: pointer passing problem

  #6  
May 2nd, 2008
Originally Posted by umeshjaviya View Post
Hello Friends,

Thank you very much for your kind help......
The correctly working code is given below......
What I understand that .... user define functions can communicate with main function through pointers only...so when I need to pass pointer and its value then I need to pass pointer of pointer (**pointer) but not pointer (*pointer) only.


  1. #include <stdafx.h>
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. void resize_long_pointer(long **,long *);
  7.  
  8. void main()
  9. {
  10. size_t size;
  11. long *CF5,i;
  12. long N_Cell_total;
  13.  
  14.  
  15.  
  16. N_Cell_total = 1;
  17.  
  18. CF5 = (long *)malloc((N_Cell_total + 2) * sizeof(long));
  19.  
  20. for(i=1;i<=N_Cell_total;i++)
  21. {
  22. CF5[i] = i;
  23. printf("\ntest1 %ld",CF5[i]);
  24. }
  25.  
  26. N_Cell_total = 200000;
  27.  
  28. resize_long_pointer(&CF5,&N_Cell_total);
  29.  
  30. for(i=1;i<=N_Cell_total;i++)
  31. {
  32. printf("\ntest2 i %ld CF5 %ld",i,CF5[i]);
  33. }
  34.  
  35. }
  36.  
  37. void resize_long_pointer(long **pointer2,long *add_size)
  38. {
  39.  
  40. long int_tmp,i,*pointer;
  41. size_t size;
  42.  
  43.  
  44. int_tmp = *add_size;
  45.  
  46. pointer = *pointer2;
  47.  
  48. printf("\nAdditional size :: %ld",int_tmp);
  49.  
  50. if( (pointer = (long *) realloc( pointer, (int_tmp + 2) * sizeof(long) )) == NULL)
  51. printf("\nPointer resizing problem :: add_size : %ld",*add_size);
  52.  
  53. for(i=1;i<= int_tmp;i++)
  54. {
  55. pointer[i] = i;
  56. //printf("\nfunction test1 i %ld pointer %ld",i,pointer[i]);
  57. }
  58.  
  59. printf("\ntest2 pointer[%ld] %ld ",i-1,pointer[i-1]);
  60.  
  61. *pointer2 = pointer;
  62. }
Thank you very much Ancient Dragon for your reply...

for(i=1;i<= int_tmp;i++)

I am skipping first element purposly..cause it makes my engineering calculations...and I also using CF5[0] = 0; somewhere in programe to interpolate between 0 and 1 values.

Thank you very much again..
Reply With Quote  
Join Date: May 2008
Location: Infinte Castle, below Beltline
Posts: 229
Reputation: Prabakar is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 16
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz in Training

Re: pointer passing problem

  #7  
May 2nd, 2008
Originally Posted by John A View Post
A couple of other notes:

- void main is evil. Don't use it.
- You shouldn't have your function prototype inside main(). Move line 13 to somewhere before main()
I nerver thought void main had so much drawbacks thanks a lot for you advice.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 4:50 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC