none
Returning array of numbers from function to the main function

    Question

  • Hi,

    I sent the array of numbers from main to function called sum in attached code using ptr1 and ptr2. Then I added the arrays in the function and managed to print the added array from the function sum. But I want to send the added array added_array back to main. So I use a pointer ptr3 but I am not getting the added array back in main. Any ideas ?

    Many thanks for help!

    /*This program lets the user enter two arrays of ten elements. It then displays the sum of values of two arrays into the third arrays*/
    #include <stdio.h>
    int x,y,z;
    int array1[10],array2[10],*ptr1,*ptr2, call;
    int sum( int *ptr1, int *ptr2);
    int main()
    {
    	ptr1=array1;
    	ptr2=array2;
    	printf("Enter the 10 elements:\n");
    	for (x=0;x<10;x++)
    	{	
     		scanf("%d",&array1[x]);
    		getchar();
    	}
    
    	printf("Enter the second 10 elements:\n");
    	for (y=0;y<10;y++)
    	{	
    		scanf("%d",&array2[y]);
    		getchar();
    	}
    	printf("the first_array array1 entered is\n");
    	for (x=0;x<9;x++)
    		{printf("%d\t",array1[x]);}
    	printf("\nthe second_array array2 you entered is\n");
    	for (y=0;y<10;y++)
    		{printf("%d\t",array2[y]);}
    	call=sum(ptr1,ptr2);
    	for (z=0;z<10;z++)
    	{scanf("returned array is %d",&call);}
    	fflush(stdin);
    	getchar();
    	return 0;
    }
    	int sum( int *ptr1, int *ptr2)
    
    {
    	
    	int added_array[10];
    
    	int ctr=0;
    	int *ptr3;
    	for (ctr=0;ctr<10;ctr++)
    	{ 
    		ptr3=&added_array[ctr];
    		added_array[ctr]=array1[ctr]+array2[ctr];
    		printf("%d\t",added_array[ctr]);}
    		return *ptr3;
    		getchar();
    	
    	
    	}
    
    Saturday, July 10, 2010 7:48 PM

Answers

  • AustinCann wrote:

    I sent the array of numbers from main to function called sum in  attached code using ptr1 and ptr2. Then I added the arrays in the
    function and managed to print the added array from the function sum.  But I want to send the added array added_array back to main.
    So I use a pointer ptr3 but I am not getting the added array back in  main. Any ideas ? 

    Pass three pointers - the first two pointing to arrays to be added up,  and the third pointing to the array in which to store the result.

    void sum(int* arr1, int* arr2, int* result) {
      for (int i = 0; i < 10; ++i) {
        result[i] = arr1[i] + arr2[i];
      }
    }


    Igor Tandetnik

    Saturday, July 10, 2010 8:00 PM
  • Compiler returns the error I am afraid ( c4716: 'sum' must return a value
    Look at Igor's code. It is
     
    void sum(int* arr1, int* arr2, int* result);
     
    Note the "void". When you get a compiler error, you can get more detail by typing the error code into the index of the VC Help. The documentation for c4716 is pretty clear (as is the error message itself, really).
     

    David Wilkinson | Visual C++ MVP
    Sunday, July 11, 2010 10:19 AM
  • Thats great! This error is removed. (As void doesn't need the return)
     
    Last thing I am struggling with calling this function in the main.
     
    I have call=(arr1,arr2,arr3); but compiler doesn't like this. ( a value to type void can't be assigned to entity of type int)
    Please do not say "compiler does not like this". Always show the error message and the expression that creates it, together with the declarations of any variables or functions in the expression.
     
    But I am not surprised the compiler does not like
     
    call=(arr1,arr2,arr3);
     
    What is this supposed to mean? You are not calling any function here.
     
    You have (I think) a function sum() that takes three pointers and returns void. To use it, you just call it
     
    sum(arr1, arr2, arr3);
     
    You cannot assign the result to anything, because the function returns void.
     
    I think it would help you to get hold of a good elementary book on C or C++ and study it. In the present context, you would need to look at the material on declaring, defining and calling functions.
     
    I should add that, unless you have a very special reason to be learning C, you would be better to learn C++ from the beginning. Almost all valid C programs are valid C++ programs, but there are some minor differences, and there is no particular value to learning the C way of doing things.
     

    David Wilkinson | Visual C++ MVP
    Sunday, July 11, 2010 11:39 AM
  • I am learning C coz I am going to be doing embedded designing soon. And I am advised to learn C for embedded designing. Is C++ used for this purpose?

    That depends on what embedded platform you will be using. If it's windows mobile or linux based, then C++ will work well. If it's a microcontroller, then C or even assembly may be required.

     

    -PaulH

    Monday, July 12, 2010 5:50 PM

All replies

  • AustinCann wrote:

    I sent the array of numbers from main to function called sum in  attached code using ptr1 and ptr2. Then I added the arrays in the
    function and managed to print the added array from the function sum.  But I want to send the added array added_array back to main.
    So I use a pointer ptr3 but I am not getting the added array back in  main. Any ideas ? 

    Pass three pointers - the first two pointing to arrays to be added up,  and the third pointing to the array in which to store the result.

    void sum(int* arr1, int* arr2, int* result) {
      for (int i = 0; i < 10; ++i) {
        result[i] = arr1[i] + arr2[i];
      }
    }


    Igor Tandetnik

    Saturday, July 10, 2010 8:00 PM
  • I want to do the addition not in main function. I have done similar to this in function sum. Now I want to return the result[i] to the main.

    Many thanks for help!

     

    Saturday, July 10, 2010 9:32 PM
  • On Sat, 10 Jul 2010 20:48:45 +0100, <AustinCann> wrote:

    >
    > Post: 19;
    >
    >
    > Hi, I sent the array of numbers from main to function called sum in
    > attached code using ptr1 and ptr2. Then I added the arrays in the
    > function and managed to print the added array from the function sum. But
    > I want to send the added array added_array back to main. So I use a
    > pointer ptr3 but I am not getting the added array back in main. Any
    > ideas ? Many thanks for help! /*This program lets the user enter two
    > arrays of ten elements. It then displays the sum of values of two arrays
    > into the third arrays*/
    > #include <stdio.h>
    > int x,y,z;
    > int array1[10],array2[10],*ptr1,*ptr2, call;
    > int sum( int *ptr1, int *ptr2);

    You want to pass in two arrays, and expect to get back an array - but you
    actually say you want a single int back -this should be:

    int* sum( int *ptr1, int *ptr2);

    However this doesn't tell sum how many elements have been passed in - a
    better approach would be
    int* sum( int *ptr1, int *ptr2, int count); // count in the number of
    elements in each array

    or
    int[10] sum(int arg1[10], int arg2[10]);


    > int main()
    > {
    > ptr1=array1;
    > ptr2=array2;
    > printf("Enter the 10 elements:\n");
    > for (x=0;x<10;x++)
    > {
    > scanf("%d",&array1[x]);
    > getchar();
    > }
    >
    > printf("Enter the second 10 elements:\n");
    > for (y=0;y<10;y++)
    > {
    > scanf("%d",&array2[y]);
    > getchar();
    > }
    > printf("the first_array array1 entered is\n");
    > for (x=0;x<9;x++)
    > {printf("%d\t",array1[x]);}
    > printf("\nthe second_array array2 you entered is\n");
    > for (y=0;y<10;y++)
    > {printf("%d\t",array2[y]);}
    > call=sum(ptr1,ptr2);
    > for (z=0;z<10;z++)
    > {scanf("returned array is %d",&call);}

    If sum returns the array of integers you want, then printing out the
    values in the list should look siilar to printing out the input arrays.

    You don't need to use different variables for the for loops as they are
    not nested, and generally it is better to use i as the loop variable - and
    only use x, y, or, z, when referring to coordinates.

    > fflush(stdin);
    > getchar();
    > return 0;
    > }
    > int sum( int *ptr1, int *ptr2)
    >
    > {
    >
    > int added_array[10];
    >
    > int ctr=0;
    > int *ptr3;
    > for (ctr=0;ctr<10;ctr++)
    > { ptr3=&added_array[ctr];
    > added_array[ctr]=array1[ctr]+array2[ctr];
    > printf("%d\t",added_array[ctr]);}
    > return *ptr3;

    return marks the end of execution of the current function, and specifies
    the return value - thus the getchar() below will never be executed nor
    will the second pass of the for loop.

    Another problem is that you are calculating the sum in added_array but
    added_array is only defined within sum, if you return a pointer to it from
    sum then the results are indeterminate.

    > getchar();
    >
    >
    > }


    --
    Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


    Saturday, July 10, 2010 10:02 PM
  • I want to do the addition not in main function. I have done similar to this in function sum. Now I want to return the result[i] to the main.
    But this is exactly what Igor showed you how to do...
     
    You have to allocate the result array in main(), but the calculation is done in the sum() function.
     

    David Wilkinson | Visual C++ MVP
    Saturday, July 10, 2010 10:24 PM
  • void sum(int* arr1, int* arr2, int* result) {
      for (int i = 0; i < 10; ++i) {
        result[i] = arr1[i] + arr2[i];

    In this case, The return to main is then written as

    return *result;

    ??

    Many thanks for help!

     

    Sunday, July 11, 2010 1:02 AM
  • You don't need to return anything. Remove the return *result line.

    Sunday, July 11, 2010 1:12 AM
  • Compiler returns the error I am afraid ( c4716: 'sum' must return a value

    Many thanks for help

    Sunday, July 11, 2010 9:27 AM
  • Hello,

    since this is a C++ Forum and you seem to be a beginner, I wonder if you use this C stuff intentionally. If you want to learn C++ it's IMHO not neccessary or helpfull to learn C first. Here is what your program would look like in C++:

    //Read two ranges of numbers from the input, add their elements and print result
    #include <vector>
    #include <iostream>
    #include <iterator>
    #include <algorithm>
    #include <functional>
    #include <exception>
    
    using namespace std;
    
    int main()
    {
    	typedef istream_iterator<int> in;
    	typedef ostream_iterator<int> out;
    
    	const size_t num = 10;	//the number of integers to add
    
    	try{
    		vector<int> summand_1;
    		vector<int> summand_2;
    
    		cin.exceptions(ios::failbit | ios::badbit);	//throw exception on error
    		//read to times num values from the standard input
    		cout << "Please enter " << num << " integers:\n";
    		copy_n( in(cin), num, back_inserter(summand_1) );	//read first vector of numbers
    		cout << "Please enter " << num << " integers:\n";
    		copy_n( in(cin), num, back_inserter(summand_2) );	//read second vector of numbers
    
    		//calculate the result
    		vector<int> result;
    		transform(
    			summand_1.begin(), summand_1.end(),	//the first summands
    			summand_2.begin(),			//the second summands
    			back_inserter(result),		//store the sum at the end of result vector
    			plus<int>() );			//the operation to perfom on the elements
    
    		//print the result to standard output
    		copy(result.begin(), result.end(), out(cout, " "));
    
    	} catch ( const exception& e) {
    		cerr << "exception: " << e.what() << endl;
    	} catch(...) {
    		cerr << "An unknown error occurred!" << endl;
    	}
    }
    

     And to answer your original question, the simplest way to return an array in C++ is by doing so:
    std::vector<int> myFunc(); //signature of a function returning a vector to int

    don't worry about the extra copy to much. It's gone in C++0x and even in the old standard many compilers are able to remove it. In any case, the point I'm trying to make is that C and C++ are very diffrent and you gain little by learning C first.

    best regards,
    Markus

    Sunday, July 11, 2010 10:12 AM
  • Compiler returns the error I am afraid ( c4716: 'sum' must return a value
    Look at Igor's code. It is
     
    void sum(int* arr1, int* arr2, int* result);
     
    Note the "void". When you get a compiler error, you can get more detail by typing the error code into the index of the VC Help. The documentation for c4716 is pretty clear (as is the error message itself, really).
     

    David Wilkinson | Visual C++ MVP
    Sunday, July 11, 2010 10:19 AM
  • Thats great! This error is removed. (As void doesn't need the return)

    Last thing I am struggling with calling this function in the main.

    I have call=(arr1,arr2,arr3); but compiler doesn't like this. ( a value to type void can't be assigned to entity of type int)

    Many thanks for help

    Sunday, July 11, 2010 11:18 AM
  • Thats great! This error is removed. (As void doesn't need the return)
     
    Last thing I am struggling with calling this function in the main.
     
    I have call=(arr1,arr2,arr3); but compiler doesn't like this. ( a value to type void can't be assigned to entity of type int)
    Please do not say "compiler does not like this". Always show the error message and the expression that creates it, together with the declarations of any variables or functions in the expression.
     
    But I am not surprised the compiler does not like
     
    call=(arr1,arr2,arr3);
     
    What is this supposed to mean? You are not calling any function here.
     
    You have (I think) a function sum() that takes three pointers and returns void. To use it, you just call it
     
    sum(arr1, arr2, arr3);
     
    You cannot assign the result to anything, because the function returns void.
     
    I think it would help you to get hold of a good elementary book on C or C++ and study it. In the present context, you would need to look at the material on declaring, defining and calling functions.
     
    I should add that, unless you have a very special reason to be learning C, you would be better to learn C++ from the beginning. Almost all valid C programs are valid C++ programs, but there are some minor differences, and there is no particular value to learning the C way of doing things.
     

    David Wilkinson | Visual C++ MVP
    Sunday, July 11, 2010 11:39 AM
  • I am learning C coz I am going to be doing embedded designing soon. And I am advised to learn C for embedded designing. Is C++ used for this purpose?
    Sunday, July 11, 2010 1:31 PM
  • I am learning C coz I am going to be doing embedded designing soon. And I am advised to learn C for embedded designing. Is C++ used for this purpose?
    In that case learning C may be the way to go (I don't really know anything about embedded programming).
     
    If you stick with C you should be sure to point that out when asking questions, because otherwise people will assume you are using C++.
     

    David Wilkinson | Visual C++ MVP
    Sunday, July 11, 2010 3:03 PM
  • sagji wrote:

    int[10] sum(int arg1[10], int arg2[10]);

    This wouldn't compile. A function return value can't be of array type.


    Igor Tandetnik

    Sunday, July 11, 2010 6:27 PM
  • I am learning C coz I am going to be doing embedded designing soon. And I am advised to learn C for embedded designing. Is C++ used for this purpose?

    That depends on what embedded platform you will be using. If it's windows mobile or linux based, then C++ will work well. If it's a microcontroller, then C or even assembly may be required.

     

    -PaulH

    Monday, July 12, 2010 5:50 PM
  • Yes, C++ is used a lot for embedded devices. Basically the question boils down to, if there is a C++ compiler available for your platform. This might not be the case for a programmable heat sensor used in a toaster, but for Handys and smart devices you are very likely to be able to develop in C++. If your embeded Application needs to provide a GUI you might be interested in the fact that Silverlight embedded allows native Code Behind for XAML(I wish I could do that on my desktop). Nokia offers QT which works on Desktop and (modern) Nokia Handys alike.

    Best regards,
    Markus

    Monday, July 12, 2010 6:38 PM