pointer to function

This is a discussion on pointer to function within the C Programming forums, part of the General Programming Boards category; Hi everyone, What is the difference between a pointer to a function that returns a pointer, to a function that ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    pointer to function

    Hi everyone,

    What is the difference between a pointer to a function that returns a pointer, to a function that takes a pointer as argument?

    1) Passing a pointer to a function and returning it after operations
    Code:
    void iarray2d(int **mat, size_t nrows, size_t ncols, int index)
    {
    	//nrows = number of rows
    	//ncols = ncolumns
    	//index = 1 if you want allocation in the fortran way starting from 1
    	//index = 0 if you want the standard C allocation way starting from 0
    	
    	long i;
    
    	//Malloc pointers to rows
    	mat= malloc((nrows+index)*sizeof(int*));
    	if (!mat){
    		printf("Matrix MAT could not be allocted");
    		exit(1);
    	}
    	
    	mat[0] = NULL;
    
    	for(i=index; i<nrows+index; i++){
    		mat[i] = malloc((ncols+index)*sizeof(int*));
    		if (!mat[i]){
    			printf("Matrix MAT could not be allocted\n The exec. will Exit now\n");
    			exit(1);
    		}
    	}
    	
    return;
    }

    2) returning a pointer from a pointer to function
    Code:
    int **iarray2d(size_t nrows, size_t ncols, int index)
    {
          	long i;
    	int **mat;
    
    	//Malloc pointers to rows
    	mat= malloc((nrows+index+1)*sizeof(int*));
    	if (!mat){
    		printf("Matrix MAT could not be allocted");
    		exit(1);
    	}
    	
    	mat[0] = NULL;
    	for(i=index; i<nrows+index; i++){
    		mat[i] = malloc((ncols+index+1)*sizeof(int*));
    		if (!mat[i]){
    			printf("Matrix MAT could not be allocted\n The exec. will Exit now\n");
    			exit(1);
    		}
    	}
    	
    return mat;
    }
    Thank you in advance
    All the best

    cfd

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Depends on how you intend to use it. Both are valid.

    By the way, in a void function, you don't need a return.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Quote Originally Posted by matsp View Post
    Depends on how you intend to use it. Both are valid.

    By the way, in a void function, you don't need a return.

    --
    Mats
    Hi Mat, thanks for replying. I posted a reply also to the thread "dynamic allocation from 1 instead of zero" because I get a BUS error with the first option, where the function is not a pointer to function, but the argument is passed as a pointer.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    in first function you need

    Code:
    void iarray2d(int **mat, size_t nrows, size_t ncols, int index)
    change to

    Code:
    void iarray2d(int ***matp, size_t nrows, size_t ncols, int index)
    and when you call it in the program write the call as iarray2d(&m, 2, 2) and have declaration int **m;
    why are you use index ? you are have ncols

    second function is right, because it returns pointer to allocated memory (first function is not and allocated memory is lost)

    my function for ints attached
    Attached Files Attached Files
    Last edited by c.user; 04-29-2009 at 12:10 AM.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi c.user,

    thank you very much for your suggestion and for attaching your function. It'll be very helpful.

    To answer your question on INDEX, it is because I not always want to allocate from zero, but also want to be able to allocate from 1; that's why I have the index variable as argument

    all the best

  6. #6
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    ah... I'm understand, I would write second function which use normal function and call it with index added to nrows and ncols

    Code:
    int **CreateIndexMatrix(unsigned n, unsigned m, unsigned idx)
    {
        return CreateMatrix(n+idx, m+idx);
    }
    it's a shell for base function

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hello there,
    thank you for the hint

    Best regards
    cfd

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. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 08:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21