Velocity Reviews > Passing Arrays to Functions

# Passing Arrays to Functions

kelvSYC
Guest
Posts: n/a

 10-11-2003
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.

Andreas Kahari
Guest
Posts: n/a

 10-11-2003
In article <111020031258075359%(E-Mail Removed)>, 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

osmium
Guest
Posts: n/a

 10-11-2003
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.

Amol
Guest
Posts: n/a

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

kelvSYC <(E-Mail Removed)> wrote in message news:<111020031258075359%(E-Mail Removed)> ...
> 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()?

August Derleth
Guest
Posts: n/a

 10-12-2003
Andreas Kahari <(E-Mail Removed)> wrote in
news:(E-Mail Removed) on Sat 11 Oct 2003
01:28:55p:

> In article <111020031258075359%(E-Mail Removed)>, 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?

Irrwahn Grausewitz
Guest
Posts: n/a

 10-12-2003

>Andreas Kahari <(E-Mail Removed)> wrote in
>news:(E-Mail Removed) rg on Sat 11 Oct 2003
>01:28:55p:
>
>> In article <111020031258075359%(E-Mail Removed)>, 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
((E-Mail Removed))

Andreas Kahari
Guest
Posts: n/a

 10-12-2003
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

Bertrand Mollinier Toublet
Guest
Posts: n/a

 10-12-2003
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

Peter Shaggy Haywood
Guest
Posts: n/a

 10-15-2003
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

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!

"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"?