change and return arrays from a function

This is a discussion on change and return arrays from a function within the C Programming forums, part of the General Programming Boards category; I want to input empty arrays into a function and change the values and return those arrays. I remember something ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    16

    change and return arrays from a function

    I want to input empty arrays into a function and change the values and return those arrays. I remember something about if I only use *array1 as input, that array won't be re-written. Should it be something like:
    Code:
    void function(int **array1, int **array2){
      int i;
      for (i=0 ; i<10; i++){
        array1[i]=i;
        array2[i]=10-i;
      }
    }
    
    int main(){
      int *array1, *array2;
      //allocates memory for array1 and array2 from another function then 
      function(&array1, &array2);
    }
    will that write the values into array1 and array2?
    thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by cuizy View Post
    I want to input empty arrays into a function and change the values and return those arrays. I remember something about if I only use *array1 as input, that array won't be re-written. Should it be something like:
    Code:
    void function(int **array1, int **array2){
      int i;
      for (i=0 ; i<10; i++){
        array1[i]=i;
        array2[i]=10-i;
      }
    }
    
    int main(){
      int *array1, *array2;
      //allocates memory for array1 and array2 from another function then 
      function(&array1, &array2);
    }
    Making a pointer to a variable other than an array, is necessary in C, because C uses a copy mechanism for all function paramaters. So if you made a change to any such (non-global) variables that were passed to a function, it would not change the original variable, but instead, change just the copy.

    That's not the case with arrays, however. Array names are very much like pointers, and the original pointer when copied, still has the same valid and original array's address to it's base. So any changes made, within the called function, would result in a change to the original array.

    Your function call needs only the name of the array:
    function(array1, array2);

    since the name of the array is almost like a constant pointer to it's first element. No &.

    Your function needs:
    void function(int array1[], int array2[]) //(only the first dimension can be left blank like this

    it could also be:
    void function(int *array1, int *array2)

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Note: if you pass an array in, like this,
    Code:
    int zerofirst(int *array) {
        array[0] = 0;
    }
    then it might do as you expect: the first element in the array is initialized to zero, and this change is reflected in the calling function. You see, you don't make a copy of the array when you pass it to the function; that would be quite expensive. Instead, you just get a pointer to the first element of the array, which is still located in main()'s (or whatever) stack memory. Thus if you modify the array, you actually modify the original.

    Last note: you need some space for the array, as I mentioned below. The syntax is easy if you know how much space you need.
    Code:
    int main() {
        int array1[10], array2[100];
        /* ... */
        return 0;
    }
    ---

    The rest of my reply assumes you were trying to create new arrays and return them to the caller, which is somewhat more difficult.

    Almost. You need to write into some valid memory. Also, the syntax in function() would be
    Code:
    (*array1)[i] = i;
    since you want to dereference array1 to get to the actual array, and then index into that array.

    You might want something like this, e.g.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void new_number_list(int **upwards, int **downwards, int length) {
        int n;
        
        *upwards = malloc(sizeof(int) * length);
        *downwards = malloc(sizeof(int) * length);
        
        for(n = 0; n < length; n ++) {
            (*upwards)[n] = n;
            (*downwards)[n] = length - n - 1;
        }
    }
    
    int main() {
        int *up, *down, x;
        new_number_list(&up, &down, 10);
        
        printf("Up:   ");
        for(x = 0; x < 10; x ++) {
            printf("%d ", up[x]);
        }
        printf("\nDown: ");
        for(x = 0; x < 10; x ++) {
            printf("%d ", down[x]);
        }
        putchar('\n');
        
        free(up);
        free(down);
        
        return 0;
    }
    Note: if you're only creating one array like this, you can just return it:
    Code:
    int *new_array(int length) {
        return malloc(length * sizeof(int));
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

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