Passing array to function

Discussion in 'C Programming' started by DeepaK K C, Feb 17, 2005.

  1. DeepaK K C

    DeepaK K C Guest

    Could anybody tell me how to pass array to a function by value?

    -Deepak
    DeepaK K C, Feb 17, 2005
    #1
    1. Advertising

  2. DeepaK K C

    infobahn Guest

    DeepaK K C wrote:
    >
    > Could anybody tell me how to pass array to a function by value?


    Passing an array to a function is actually impossible. Passing by
    value is easy enough, since C doesn't support any other form of
    parameter passing.

    Even /trying/ to pass an array by value smacks of silliness
    (investigate const for when you pass the address of an object
    that you don't wish the function to modify).

    But I'm afraid there /is/ a way to do this damn silly thing.
    Wrap the array in a struct, and pass the struct by value.

    And don't tell anyone I told you.
    infobahn, Feb 17, 2005
    #2
    1. Advertising

  3. DeepaK K C

    osmium Guest

    "DeepaK K C" writes:

    > Could anybody tell me how to pass array to a function by value?


    Make a copy of the array in the calling function. Pass the name of this new
    array to the called function.
    osmium, Feb 17, 2005
    #3
  4. DeepaK K C wrote:
    > Could anybody tell me how to pass array to a function by value?
    >
    > -Deepak


    Three ways depending on how, why and where you want responsibilities
    to be for this.

    1. Put the array in a struct.
    2. Create a copy of the array in the calling function.
    3. Create a copy of the array in the called function.

    --
    Thomas.
    Thomas Stegen, Feb 17, 2005
    #4
  5. On Thu, 17 Feb 2005 14:56:21 +0000, Thomas Stegen wrote:

    > DeepaK K C wrote:
    >> Could anybody tell me how to pass array to a function by value?
    >>
    >> -Deepak


    As others have said taken literally this is not possible, C does not
    support the passing of arrays to functions at all. But you can create an
    equivalent effect.

    > Three ways depending on how, why and where you want responsibilities to
    > be for this.
    >
    > 1. Put the array in a struct.


    If the array is already in a struct for other reason then this is fine.
    But I've never come across a situation where this is a sensible thing to
    do just for the purpose of passing the array "by value".

    > 2. Create a copy of the array in the calling function.


    Possible. In that case the function interface allows the array in question
    to be modified (if it doesn't modify it there's little point in passing by
    value). So maybe some calling functions care about this while others don't.

    > 3. Create a copy of the array in the called function.


    IMO this is the cleanest way. The corresponding parameter would be defined
    as a pointer to const indicating to the caller that the array won't be
    modified. memcpy() can be used to make the copy. Issues are determining
    the size of the array (which is an issue anyway) and how to allocate space
    for the copy.

    Lawrence
    Lawrence Kirby, Feb 17, 2005
    #5
  6. DeepaK K C wrote:

    > Could anybody tell me how to pass array to a function by value?


    > cat main.c

    #include <stdlib.h>
    #include <stdio.h>

    typedef struct doubleArray4 {
    double x[4];
    } doubleArray4;

    int doubleArray4_print(FILE* fp, doubleArray4 A) {
    int characters = 0;
    for (size_t j = 0; j < 4; ++j)
    characters += fprintf(fp, " %f", A.x[j]);
    characters += fprintf(fp, "\n");
    return characters;
    }

    int main(int argc, char* argv[]) {
    double a[4];
    for (size_t j = 0; j < 4; ++j)
    a[j] = j;
    fprintf(stdout, "a = ");

    doubleArray4_print(stdout, *((doubleArray4*)a));

    return EXIT_SUCCESS;
    }

    > gcc -Wall -std=c99 -pedantic -o main main.c
    > ./main

    a = 0.000000 1.000000 2.000000 3.000000
    E. Robert Tisdale, Feb 17, 2005
    #6
  7. DeepaK K C

    SM Ryan Guest

    (DeepaK K C) wrote:
    # Could anybody tell me how to pass array to a function by value?

    Wrap it inside a struct and pass the struct by value, or use Pascal.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    There are subtler ways of badgering a witness.
    SM Ryan, Feb 17, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,002
    Evan Carew
    Jan 21, 2004
  2. Replies:
    9
    Views:
    555
    justanotherguy
    Dec 3, 2004
  3. Jonathan Underwood
    Replies:
    2
    Views:
    606
    Simon Biber
    Aug 13, 2003
  4. JW
    Replies:
    1
    Views:
    428
    David Jeske
    Jul 12, 2003
  5. deadpickle
    Replies:
    1
    Views:
    941
    Jens Thoms Toerring
    Nov 7, 2010
Loading...

Share This Page