Thread: Returning arrays from a function

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    7

    Question Returning arrays from a function

    I am trying to return a pointer address to my main function but it isn't working. I am using Windows 98, Visual Studio 6.0 and making my program for a win 32 console application. I have put the function that I am working on below. The purpose of this program is to complete an assignment for school. What this function is to do is to take a complex array of predefined numbers and square them, storing the result in the original array. I am pretty sure that I have messed this up badly.

    I have a couple of questions about this as well.

    I am using pointers because I am under the impression that if you just pass the array the contents are protected, and would not be changed when the function returns. Is this true?

    The instruction and the book that in the course that I am taking has not been the best. Does anyone know where I can find a good explanation of using pointers, functions and structs?


    int square_function(int * original[][COLUMNS], int rows)
    {
    int i, j;
    for (i=0 ; i < rows ; i++)
    for (j=0 ; j < COLUMNS ; j++)
    *original[i][j] = (*original[i][j] * 2);

    return (*original);
    }


    Thank you for any help or direction.

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I think you have the right idea. You can't really return an array. You return a pointer to the first element of the array and then...poof...read 'til the cows come home

    Do you need more help/code? I'm willing to help you out.

    --Garfield
    1978 Silver Anniversary Corvette

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > int square_function(

    You'll need to change this to return a pointer.

    int *returnArray( int array[] ) { return array; }

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    7
    I understand what Garfield has posted. It makes sense and I think that I have a similar example. I didn't recognize the problem that I have, and what all of the possible solutions are, given the explanantions in class.

    Seeing Quzah's message leads me to this question.


    For this line of code to prototype the function:

    int square_function(int * original[][COLUMNS]);

    Would this be the replacement to return the pointer?

    int *square_function( int * original[] ) { square_function; };


    To call this function in the code is this the correct format:

    square_function( original ) {square_function};


    Thanks again. The answers that I am getting here are far superior to what I get in class.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    There is no need to return anything because arrays are passed by reference so any changes you make inside the array will still be there when you return to the calling function. Secondly when you square something that is not the same as multiplying by 2.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yes, you are right. When you specify something like this:
    Code:
    char array[5];
    
    sscanf(line, "%s", array);
    You don't need to do "&array" because that is already the address of array. If you were to do "&array", then that is two levels of indirection.

    --Garfield
    1978 Silver Anniversary Corvette

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    void square_function(int original[][COLUMNS]);

    >Would this be the replacement to return the pointer?

    Code:
    void square_function( int original[][COLUMNS] )
    { 
       int i, j; 
       for (i=0 ; i < ROWS ; i++) 
          for (j=0 ; j < COLUMNS ; j++) 
             original[i][j] *= 2; 
    }
    >To call this function in the code is this the correct format:
    Code:
    const int ROWS = 5;
    const int COLUMNS = 5;
    int main(void)
    {
       int r,c;
       for (r=0; r<ROWS; r++)
          for (c=0; c<COLUMNS; c++)
             original[r][c] = 2;
       square_function(original);
       for (r=0; r<ROWS; r++)
          for (c=0; c<COLUMNS; c++)
             printf("original[%d][%d]=%d\n",r,c,original[r][c]);
       return 0;
    }
    Last edited by swoopy; 12-14-2001 at 08:39 PM.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    And if you want to square them, it seems like instead of this:
    original[i][j] *= 2;

    You would want this:
    original[i][j] *= original[i][j];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM