Passing array to function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DeepaK K C

    #1

    Passing array to function

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

    -Deepak
  • infobahn

    #2
    Re: Passing array to function

    DeepaK K C wrote:[color=blue]
    >
    > Could anybody tell me how to pass array to a function by value?[/color]

    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.

    Comment

    • osmium

      #3
      Re: Passing array to function

      "DeepaK K C" writes:
      [color=blue]
      > Could anybody tell me how to pass array to a function by value?[/color]

      Make a copy of the array in the calling function. Pass the name of this new
      array to the called function.


      Comment

      • Thomas Stegen

        #4
        Re: Passing array to function

        DeepaK K C wrote:[color=blue]
        > Could anybody tell me how to pass array to a function by value?
        >
        > -Deepak[/color]

        Three ways depending on how, why and where you want responsibilitie s
        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.

        Comment

        • Lawrence Kirby

          #5
          Re: Passing array to function

          On Thu, 17 Feb 2005 14:56:21 +0000, Thomas Stegen wrote:
          [color=blue]
          > DeepaK K C wrote:[color=green]
          >> Could anybody tell me how to pass array to a function by value?
          >>
          >> -Deepak[/color][/color]

          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.
          [color=blue]
          > Three ways depending on how, why and where you want responsibilitie s to
          > be for this.
          >
          > 1. Put the array in a struct.[/color]

          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".
          [color=blue]
          > 2. Create a copy of the array in the calling function.[/color]

          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.
          [color=blue]
          > 3. Create a copy of the array in the called function.[/color]

          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

          Comment

          • E. Robert Tisdale

            #6
            Re: Passing array to function

            DeepaK K C wrote:
            [color=blue]
            > Could anybody tell me how to pass array to a function by value?[/color]
            [color=blue]
            > cat main.c[/color]
            #include <stdlib.h>
            #include <stdio.h>

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

            int doubleArray4_pr int(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_pr int(stdout, *((doubleArray4 *)a));

            return EXIT_SUCCESS;
            }
            [color=blue]
            > gcc -Wall -std=c99 -pedantic -o main main.c
            > ./main[/color]
            a = 0.000000 1.000000 2.000000 3.000000

            Comment

            • SM Ryan

              #7
              Re: Passing array to function

              kcdeepakkc@hotm ail.com (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.

              Comment

              Working...