Passing Arrays to Functions

Discussion in 'C Programming' started by kelvSYC, Oct 11, 2003.

  1. kelvSYC

    kelvSYC Guest

    Suppose I had an array of some arbitrary size

    struct foo a[10];

    and I pass that into some function

    void bar(struct foo *b);

    How do I calculate the number of elements in a[] (10) from within bar()?

    --
    I am only a mirage.
     
    kelvSYC, Oct 11, 2003
    #1
    1. Advertising

  2. In article <111020031258075359%>, kelvSYC wrote:
    > Suppose I had an array of some arbitrary size
    >
    > struct foo a[10];
    >
    > and I pass that into some function
    >
    > void bar(struct foo *b);
    >
    > How do I calculate the number of elements in a[] (10) from within bar()?


    This question has popped up a couple of times the last couple of
    days.

    The answer is, you don't. You declare bar as

    void (struct foo *b, int blen) { /* ... */ }

    so that you can pass the length of the array to it as well.

    The only way you can calculate the length of the array is if
    you have some sentinel value at the end of the array (a known
    end-of-array-marker). Loop over the array until you find the
    special value and count how many iteration you had to perform.

    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 11, 2003
    #2
    1. Advertising

  3. kelvSYC

    osmium Guest

    kelvSYC writes:

    > Suppose I had an array of some arbitrary size
    >
    > struct foo a[10];
    >
    > and I pass that into some function
    >
    > void bar(struct foo *b);
    >
    > How do I calculate the number of elements in a[] (10) from within bar()?


    You can't. The information was lost when the call was made. Pass the
    number of elements as an additional argument in the parameter list. C
    programs often skip this step because the programmer often knows the answer
    via. some other means.
     
    osmium, Oct 11, 2003
    #3
  4. kelvSYC

    Amol Guest

    You cannot. You have to pass another parameter that specifies the size
    of the array being passed.


    kelvSYC <> wrote in message news:<111020031258075359%>...
    > Suppose I had an array of some arbitrary size
    >
    > struct foo a[10];
    >
    > and I pass that into some function
    >
    > void bar(struct foo *b);
    >
    > How do I calculate the number of elements in a[] (10) from within bar()?
     
    Amol, Oct 11, 2003
    #4
  5. Andreas Kahari <> wrote in
    news: on Sat 11 Oct 2003
    01:28:55p:

    > In article <111020031258075359%>, kelvSYC
    > wrote:
    >> How do I calculate the number of elements in a[] (10) from within
    >> bar()?

    >
    > The answer is, you don't. You declare bar as
    >
    > void (struct foo *b, int blen) { /* ... */ }


    #define MAXLEN 2300

    struct foo {
    char array[MAXLEN];
    int len;
    };

    void bar(struct foo *b);

    Would that not work?

    Within bar, you'd access the len member by saying b->len, thereby giving
    you a hopefully accurate idea of just how long the array is.

    Or is that too simple and, therefore, wrong?
     
    August Derleth, Oct 12, 2003
    #5
  6. August Derleth <libertarian232003**@onewest.net> wrote:

    >Andreas Kahari <> wrote in
    >news: on Sat 11 Oct 2003
    >01:28:55p:
    >
    >> In article <111020031258075359%>, kelvSYC
    >> wrote:
    >>> How do I calculate the number of elements in a[] (10) from within
    >>> bar()?

    >>
    >> The answer is, you don't. You declare bar as
    >>
    >> void (struct foo *b, int blen) { /* ... */ }

    >
    >#define MAXLEN 2300
    >
    >struct foo {
    > char array[MAXLEN];
    > int len;
    >};
    >
    >void bar(struct foo *b);
    >
    >Would that not work?


    It would, provided that the len member is set correctly.

    >
    >Within bar, you'd access the len member by saying b->len, thereby giving
    >you a hopefully accurate idea of just how long the array is.
    >
    >Or is that too simple and, therefore, wrong?


    No, but after all it's just another way of explicitly passing the
    size of an array to a function, which seemingly is what the OP
    wants (or has) to avoid.

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 12, 2003
    #6
  7. In article <Xns94123A6F06069libertarianonewestne@63.223.8.240>,
    August Derleth wrote:
    [cut]
    > #define MAXLEN 2300
    >
    > struct foo {
    > char array[MAXLEN];
    > int len;
    > };
    >
    > void bar(struct foo *b);

    [cut]
    >
    > Or is that too simple and, therefore, wrong?


    I see no difference.

    You still pass the length of the array to the function, which is
    what you must do.

    --
    Andreas Kähäri
     
    Andreas Kahari, Oct 12, 2003
    #7
  8. August Derleth wrote:
    > Andreas Kahari wrote:
    >> kelvSYC wrote:
    >>
    >>>How do I calculate the number of elements in a[] (10) from within
    >>>bar()?

    >>
    >>The answer is, you don't. You declare bar as
    >>
    >> void (struct foo *b, int blen) { /* ... */ }

    >
    >
    > #define MAXLEN 2300
    >
    > struct foo {
    > char array[MAXLEN];
    > int len;
    > };
    >
    > void bar(struct foo *b);
    >
    > Would that not work?
    >
    > Within bar, you'd access the len member by saying b->len, thereby giving
    > you a hopefully accurate idea of just how long the array is.
    >
    > Or is that too simple and, therefore, wrong?
    >

    Actually, I think it is rather overkill. If your array nature is
    preserved (by being held within a struct), there is no need to have an
    extra member indicating its length:

    #define MAXLEN (2300)

    struct foo
    {
    char array[MAXLEN];
    };

    void bar(struct foo *b)
    {
    size_t array_length = sizeof (b->array);
    }

    --
    Bertrand Mollinier Toublet
    "Uno no se muere cuando debe, sino cuando puede"
    -- Cor. Aureliano Buendia
     
    Bertrand Mollinier Toublet, Oct 12, 2003
    #8
  9. Groovy hepcat kelvSYC was jivin' on Sat, 11 Oct 2003 18:58:07 GMT in
    comp.lang.c.
    Passing Arrays to Functions's a cool scene! Dig it!

    >Suppose I had an array of some arbitrary size
    >
    >struct foo a[10];
    >
    >and I pass that into some function
    >
    >void bar(struct foo *b);
    >
    >How do I calculate the number of elements in a[] (10) from within bar()?


    Change the declaration of bar() to the following, and pass the
    addrerss of a to it:

    void bar(struct foo (*)[10]);

    int main(void)
    {
    struct foo a[10];

    bar(&a);

    return 0;
    }

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Oct 15, 2003
    #9
    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. fivelitermustang

    Passing 2D Dynamic Arrays to Functions?

    fivelitermustang, May 14, 2004, in forum: C++
    Replies:
    6
    Views:
    2,620
    John Harrison
    May 14, 2004
  2. Replies:
    2
    Views:
    413
    Howard
    Jun 7, 2005
  3. Pete
    Replies:
    10
    Views:
    560
    John F. Bode
    Sep 22, 2004
  4. James
    Replies:
    7
    Views:
    320
    James
    Nov 5, 2006
  5. Philipp
    Replies:
    21
    Views:
    1,140
    Philipp
    Jan 20, 2009
Loading...

Share This Page