Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Passing Arrays to Functions

Reply
Thread Tools

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.
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
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.


 
Reply With Quote
 
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()?

 
Reply With Quote
 
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?

 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      10-12-2003
August Derleth <libertarian232003**@onewest.net> wrote:

>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))
 
Reply With Quote
 
Andreas Kahari
Guest
Posts: n/a
 
      10-12-2003
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
 
Reply With Quote
 
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

 
Reply With Quote
 
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
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"?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Question regarding const arrays and passing them through functions James C Programming 7 11-05-2006 05:12 PM
passing large arrays to functions dave.harper@gmail.com C++ 2 06-07-2005 06:28 PM
Passing and returning arrays to and from functions Pete C Programming 10 09-22-2004 11:42 AM
Passing 2D Dynamic Arrays to Functions? fivelitermustang C++ 6 05-14-2004 08:04 PM



Advertisments