Passing Arrays to Functions

K

kelvSYC

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()?
 
A

Andreas Kahari

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.
 
O

osmium

kelvSYC said:
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.
 
A

Amol

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

August Derleth

28:55p:
kelvSYC said:
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?
 
I

Irrwahn Grausewitz

August Derleth said:
28:55p:
kelvSYC said:
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
 
A

Andreas Kahari

[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.
 
B

Bertrand Mollinier Toublet

August said:
Andreas said:
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);
}
 
P

Peter Shaggy Haywood

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top