•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 423,575 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,428 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: Programming Forums
Views: 414 | Replies: 6 | Solved
![]() |
•
•
Join Date: May 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
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:
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
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:
c Syntax (Toggle Plain Text)
#include <stdafx.h> #include <stdio.h> #include <malloc.h> #include <math.h> #include <stdlib.h> void main() { size_t size; long *CF5,i; long N_Cell_total; void resize_long_pointer(long *,long *); N_Cell_total = 1; CF5 = (long *)malloc((N_Cell_total + 2) * sizeof(long)); for(i=1;i<=N_Cell_total;i++) { CF5[i] = i; printf("\ntest1 %ld",CF5[i]); } N_Cell_total = 20000; resize_long_pointer(CF5,&N_Cell_total); for(i=1;i<=N_Cell_total;i++) { printf("\ntest2 i %ld CF5 %ld",i,CF5[i]); } } void resize_long_pointer(long *pointer,long *add_size) { long int_tmp,i; size_t size; int_tmp = *add_size; printf("\nAdditional size :: %ld",int_tmp); if( (pointer = (long *) realloc( pointer, (int_tmp + 2) * sizeof(long) )) == NULL) printf("\nPointer resizing problem :: add_size : %ld",*add_size); for(i=1;i<= int_tmp;i++) { *pointer = i; } printf("\ntest2 pointer[%ld] %ld ",i-1,pointer[i-1]); }
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
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,124
Reputation:
Rep Power: 38
Solved Threads: 929
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)
Another option is to change resize_long_pointer() to return 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.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
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()
- 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
•
•
Join Date: May 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
c Syntax (Toggle Plain Text)
#include <stdafx.h> #include <stdio.h> #include <malloc.h> #include <math.h> #include <stdlib.h> void resize_long_pointer(long **,long *); void main() { size_t size; long *CF5,i; long N_Cell_total; N_Cell_total = 1; CF5 = (long *)malloc((N_Cell_total + 2) * sizeof(long)); for(i=1;i<=N_Cell_total;i++) { CF5[i] = i; printf("\ntest1 %ld",CF5[i]); } N_Cell_total = 200000; resize_long_pointer(&CF5,&N_Cell_total); for(i=1;i<=N_Cell_total;i++) { printf("\ntest2 i %ld CF5 %ld",i,CF5[i]); } } void resize_long_pointer(long **pointer2,long *add_size) { long int_tmp,i,*pointer; size_t size; int_tmp = *add_size; pointer = *pointer2; printf("\nAdditional size :: %ld",int_tmp); if( (pointer = (long *) realloc( pointer, (int_tmp + 2) * sizeof(long) )) == NULL) printf("\nPointer resizing problem :: add_size : %ld",*add_size); for(i=1;i<= int_tmp;i++) { pointer[i] = i; //printf("\nfunction test1 i %ld pointer %ld",i,pointer[i]); } printf("\ntest2 pointer[%ld] %ld ",i-1,pointer[i-1]); *pointer2 = pointer; }
Last edited by Ancient Dragon : May 2nd, 2008 at 8:47 am. Reason: add code tags
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,124
Reputation:
Rep Power: 38
Solved Threads: 929
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=1;i<=N_Cell_total;i++)
From your original post in main() -- that line is also wrong for the same reason
And don't be afraid to make liberal use of white space, it make the code easier to read.
>>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.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
•
•
Join Date: May 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
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.
c Syntax (Toggle Plain Text)
#include <stdafx.h> #include <stdio.h> #include <malloc.h> #include <math.h> #include <stdlib.h> void resize_long_pointer(long **,long *); void main() { size_t size; long *CF5,i; long N_Cell_total; N_Cell_total = 1; CF5 = (long *)malloc((N_Cell_total + 2) * sizeof(long)); for(i=1;i<=N_Cell_total;i++) { CF5[i] = i; printf("\ntest1 %ld",CF5[i]); } N_Cell_total = 200000; resize_long_pointer(&CF5,&N_Cell_total); for(i=1;i<=N_Cell_total;i++) { printf("\ntest2 i %ld CF5 %ld",i,CF5[i]); } } void resize_long_pointer(long **pointer2,long *add_size) { long int_tmp,i,*pointer; size_t size; int_tmp = *add_size; pointer = *pointer2; printf("\nAdditional size :: %ld",int_tmp); if( (pointer = (long *) realloc( pointer, (int_tmp + 2) * sizeof(long) )) == NULL) printf("\nPointer resizing problem :: add_size : %ld",*add_size); for(i=1;i<= int_tmp;i++) { pointer[i] = i; //printf("\nfunction test1 i %ld pointer %ld",i,pointer[i]); } printf("\ntest2 pointer[%ld] %ld ",i-1,pointer[i-1]); *pointer2 = pointer; }
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..
•
•
Join Date: May 2008
Location: Infinite Castle, below Beltline
Posts: 287
Reputation:
Rep Power: 1
Solved Threads: 24
•
•
•
•
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()
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Incompatible pointer type problem (C)
- C++ Object Pointers Problem (C++)
- Singly-Linked Lists problem (C++)
- Passing char * to function and populating inside (C++)
- Passing a Function, function pointer (C++)
- passing vector to a pointer? (C++)
- total newb - "passing arg 2 of `strcpy' makes pointer from integer without a cast" (C++)
- Passing Arrays to function in Visual C++ (C++)
Other Threads in the C Forum
- Previous Thread: Help in reading .bmp image in C
- Next Thread: passing arg 1 of 'fputs' makes pointer from integer without a cast



Linear Mode