#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    53
    Rep Power
    12

    Send/return array


    C++

    I want to send an array to a function, do something with it inside, and then return it. Then later send it to another function and return it and so on.
    How do I do this? Itīs easy with a regular int or something, but I canīt get it to work with arrays. The array looks like this int f[26].

    Can you please tell me how to declare the array, and how to send/return it to/from functions? And how to declare the functions (if itīs any different from regular int functions)

    I guess I donīt have to tell you that Iīm pretty new to this :)
  2. #2
  3. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    i don't know c++ but i think it'll be the same situation in c, which is like this:

    you never pass a whole array to a function as such - it's impossible. what you actually do is pass a pointer to the array. even if you see code that appears to be passing an array to a function, it's in fact passing a pointer. what this means is that when you alter the array (via a pointer to it), the array gets effected directly, externally to that function - so *no need for any return in the function*, which is pretty handy. even if you don't understand pointers it doesn't matter too much in this particular case as you can make use of them fairly easily almost without realising you're using them.

    so bearing in mind your array is f[26] you'd define the function like:
    Code:
    void function(int f[])	/* a pointer to f is being passed rather than the array itself */
    {
    	f[3]=12;	/* treat it as usual array (although in fact you're using pointers) */
    			/* and no need for return - the array has been changed directly */
    }
    and you call that function like:
    Code:
    function(f);
    so the array name needs passing (which actually behaves as a pointer to the array) to the function.

    it maybe a little different in c++. not sure.
    Last edited by balance; March 10th, 2003 at 09:40 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    53
    Rep Power
    12
    Thank you!

    Your code worked perfectly, so I guess that it isnīt different from C in this particular thing.
    Thanks!
  6. #4
  7. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Originally posted by balance
    it maybe a little different in c++. not sure.
    I am fairly certain that this is the same way you would do it in C++. It adds nothing or take nothing away from the way you would do it in C.

    There is another method of doing this, though, which is legal in both C and C++:

    Since you are essentially passing a pointer into the function, you can actually accept it as a pointer, and it will still work. Ex: Instead of doing this:
    void function(int f[])
    you can do this:
    void function(int *f)
    and the following code will still work:
    f[3]=12;

    In fact, the above array accessing code can be changed to pointer notation, as well. So, instead of doing this:
    f[3]=12;
    you can do this:
    *(f+3)=12;
    AND, this will work with either of the above two function headers. So, arrays and pointers are really interchangable.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    421
    Rep Power
    12
    How can you make a void function when you are setting an array in the parameters? What is the point of a parameter if nothing is returned to it? I don't understand.

    -andy
    hmmm...
  10. #6
  11. jasondoucette.com
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Location
    Canada
    Posts
    378
    Rep Power
    12
    Question 1: How can you make a void function when you are setting an array in the parameters?

    place void as the return type of the function to make it void. The fact that you are accessing an array that is passed in as one of the parameters (which you may or may not change) has absolutely nothing to do with the return value of the function. Of course, you can 'return' more than just the return value of the function if you pass in parameters that are pointers to global variables (or references to global vars), as you can change these values (by dereferencing the pointer, or simply storing a value in the reference), which change their values outside of the function, as well (i.e. any changes made are made directly to the global vars, not the copies inside the function). Passing in an array is the same thing as passing in a pointer to an array, and therefore this global array's elements can be changed. Therefore, in a sense you are 'returning' more than just the return value of the function.

    Question 2: What is the point of a parameter if nothing is returned to it?

    Parameters are not only meant for the sole purpose of returning values to the calling mechanism. Take, for instance, a function called sqr, whose purpose is to return the square of the value passed in. You would declare it as so:
    double sqr(double x)
    {
    return(x*x);
    }

    This does not pass back any value through x (in fact, if x was changed within the function, it would not change the parameter you passed in - only the copy of that parameter is changed, and its value is forever lost after the function terminates). To return a value, we use the function's return value. It would not make any sense to have this function change the value of x (as a reference or a pointer to some global variable), since you couldn't use the function like this:
    printf("2*2 = ",sqr(2));
    and, if you couldn't do this, either:
    printf("x*x + y*y = ",sqr(x)+sqr(y));
    if you didn't want the values of x or y to change after this print statement is run.

    I hope this clears things up.
    Last edited by Jason Doucette; March 13th, 2003 at 06:23 AM.
  12. #7
  13. No Profile Picture
    Dissident
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2003
    Location
    New York
    Posts
    1,671
    Rep Power
    49
    Pointers can be a confusing topic, so allow me to add my two cents...

    Normally when you pass a variable into a function, you are creating a copy of that variable for the function to work on. If you change the value of the variable in the function, it does not affect the original value.

    When you use pointers, instead of making a copy of the variable, you are actually telling the function where the original variable resides in memory space and allowing the function to act directly on the original. Therefore, if you make any changes within the function, the orginal variable will be changed as well. That's why you don't need a return value if you are passing pointers as parameters.

IMN logo majestic logo threadwatch logo seochat tools logo