Thread: return array from external function

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    59

    return array from external function

    This is my external function

    Code:
    double *splitme(double *array, size_t p, size_t q)
    {
    	int i, n, j;
    	double sum1, sum2; 
    	double *bfun_split;
    	size_t array_size, centroid;
    
    	//cast centroid as size_t
    	//round centroid sum
    	//q is centroid -1
    	//p is length - centroid - 1
    	//allocate new array double centroidedarray length same as before
    	//from 0 to p allocate everything from the right of centroid
    	//from length-q-1 till end allocate left of centroid 
    	//free old
    	//return new
    	
    	array_size = p;
    	
    	for (i=0; i<array_size; i++)
    	{
    		sum1 += (i * array[i]);
    		sum2 += array[i];
    	}
    	centroid = (round)(sum1/sum2);
    	printf("centroid is %d\n", centroid);
    	
    	q = centroid - 1;
    	p = array_size - centroid - 1;
    	printf("array size is %i\n", array_size);
    	printf("p is %i\n", p);
    	printf("q is %i\n", q);
    	bfun_split = malloc((array_size)*sizeof(double));
    	
    	i=0;
    	for (i=0; i<p; i++)
    	{
    		bfun_split[i] = array[i+centroid];//allocate the right of the centroid
    	}
    	
    	i=0;
    	//x=(array_size - q - 1);
    	for (i=(array_size - q - 1); i<array_size; i++)
    	{
    		bfun_split[i] = array[i-centroid]; //allocate the left of centroid
    	}
    	
    	i=0;
    	for (i=0; i<array_size; i++)
    	{
    		printf("split array %i is %lf\n", i, bfun_split[i]);
    	}
    	
    	return(*array);
    	free(array);
    }
    and this is my call

    Code:
    *split_array= *splitme(bfun_array, bfun_lines, 0);

    When compiling it wont let me put return(*array) only return(array) and I was under the impression that in order to return an array it had to be a pointer?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In order to return an array you need to use a different programming language.

    But here you appear to be happy to return a pointer, so that's what you need to do. "array" is a pointer, so that's what you should return. "*array" follows the pointer to the thing it is pointing to (in this case, that's a double), which is not (in this case) a pointer.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You probably want to return bfun_split because that's where all your calculations were put. In your calling function you'd use
    Code:
    split_array = splitme(bfun_array, bfun_lines, 0);
    assuming split_array is pointer.
    Also I don't think it's a good idea to free array inside the function.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Code:
    double *bfun_split;
    Code:
    double *array;
    Code:
    return (*bfun_split);
    or

    Code:
    return (*array);
    I thought this meant that they were all double pointers?

    even returning *bfun_split doesn't work (which, yes, is what I actually want to do...)

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by browser View Post
    Code:
    double *bfun_split;
    Code:
    double *array;
    So that means bfun_split is a pointer-to-double, yes. That is so incredibly different than *bfun_split, which is not a pointer-to-double, but is just a double.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    but returning

    return(bfun_split);

    just returns the pointer to the first array location.

    I want to return the entire array. How would I go about doing this?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by browser View Post
    but returning

    return(bfun_split);

    just returns the pointer to the first array location.

    I want to return the entire array. How would I go about doing this?
    In C, you can return exactly one thing. That's all you get.

    On the other hand, that one thing that you are returning is almost certainly the only thing you need to return, since arrays are contiguous in memory (i.e., no skipping about) so once you know where the starting point is in memory, you know where all the rest of them are (right behind it).

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    I did it here...

    Code:
    double ReadFile(FILE *filename, int num_lines, double *array)
    {
    	int	i;
    	
    	rewind(filename);
    	
    	i=0;
    	for(i=0; i<(num_lines); i++)
    	{
    		if((i & 1) == 0)
    		{
    			fscanf(filename, "%lf", &array[i]);
    		}
    		else
    		{
    			array[i]=0.0;
    		}	
    	}
    	
    	i=0;
    	for(i=0; i<(num_lines); i++)
    	{
    		printf("array %i is %lf\n", i, array[i]);
    	}
    	
    	
    	rewind(filename);
    	return(*array);
    	free(array);
    }
    with this as the call

    Code:
    *bfun_array= ReadFile(blurring, bfun_lines, bfun_array);

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by browser View Post
    I did it here...
    If you mean you're happy with that, then we're all happy with that. The thing to keep in mind is that you didn't return the array -- it got passed in as the third argument. Arrays that are passed in are edit-able (meaning the calling function can see the changes) so everybody's happy.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    Okay...

    so for this, would it be possible to write bfun_split into the passing *array and call it like that?

    edit: I tried

    Code:
    i=0;
    	for (i=0; i<array_size; i++)
    	{
    		bfun_split[i] = array[i];
    	}
    	
    	return(*array);
    and it isn't working
    Last edited by browser; 12-07-2010 at 11:00 AM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't feel like tracing through your code at the moment to see whether you can do this "in-place" or not. If so, then dump bfun_split completely. If not, then do your work with bfun_split in your function, and then *still in the function* do something like
    Code:
    array[i] = bfun_split[i];
    (in a loop, naturally) to put your split array back where it belongs.

    If you need both the original and the split, then you need two arrays in your calling function anyway, so just pass them both in.

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    59
    I did that and it is still not compiling.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by browser View Post
    I did that and it is still not compiling.
    Then you'll have to show us what you did.

  14. #14
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Rather than trying to make your code work, I'd suggest that you should get a good book on C.
    Question 7.5b
    Question 4.11
    Question 4.8
    Question 12.27
    Last edited by Bayint Naung; 12-07-2010 at 06:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM