Thread: problem with pointer passing to function

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    7

    Question problem with pointer passing to function

    I am a new guy in C. But I have read stuffs about Malloc, Realloc, how to pass pointers to the function and FAQ guidline as well...

    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:
    #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]);
    }
    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

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    void main()
    read FAQ main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])

    Code:
    void resize_long_pointer(long *,long *);
    general practice is to place the function prototypes outside other functions... so move this line before main

    Code:
    CF5 = (long *)malloc((N_Cell_total + 2) * sizeof(long));
    You do not need to cast malloc: Casting malloc

    But you DO need check the return value of malloc

    Code:
    for(i=1;i<=N_Cell_total;i++)
    arrays in C are 0 indexed... it means valid indexes are 0,1,2,...,N_Cell_total + 1 (because you allocate N_Cell_total + 2 elements)

    Code:
    resize_long_pointer(long *pointer
    your pointer is passed by value, so when realloc changes its value - calling function does not know about it...
    one possible way - make your function return the new pointer...

    Code:
    pointer = (long *) realloc( pointer
    bad practice - if realloc failed - you are loosing your original pointer
    should be something like
    Code:
    temp = realloc(pointer,...);
    if(temp) 
    {
       pointer = temp;
       newsize = ...
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    7
    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.

    Code:
    #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 &#37;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;
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As vart points out, you haven't fixed anything yet.
    Void main is bad (http://cpwiki.sf.net/Void_main).
    Don't omit variable names in function prototypes.
    Do not cast the return value of malloc in C.
    Arrays begin from 0, not 1.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM