C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 2nd, 2003, 01:20 PM
cjwatchman cjwatchman is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 cjwatchman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
passing an array of structures to a function

I am relatively new to C and I am having a problem passing an array of structures to a function. Here is how I define the the structure,

typedef struct {
double CL;
double CP;
} TypeMu;

I then define the array as,

TypeMu Mu[MAX_ARRAY];

Now I am not sure how to go from here to pass it to a function.

I have tried passing the array itself but that does not seem to workout correctly. I would appreciate any help any one could give me. Thanks.

Reply With Quote
  #2  
Old July 2nd, 2003, 01:42 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,824 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 1 h 9 m 26 sec
Reputation Power: 446
A basic fact of life in C is that an array name is equivalent to a pointer. Therefore, there are two ways that you can choose from that are identical:
Code:
void foobar1(TypeMu mu[]);
void foobar2(TypeMu *mu);

int main(void)
{
    /* the actual passing of the argument is the same syntax in both cases */
    foobar1(Mu);
    foobar2(Mu);

    return 0;
}

Within the function body, you can refer to individual elements of the arrays either through array indexing or through pointer arithmetic:
Code:
void foobar1(TypeMu mu[])
{
    mu[2].CL = 0.0;  /* accessing third element */
    (mu+2)->CL = 0.0;  /* same thing */
}

void foobar2(TypeMu *mu)
{
    /*  same thing works here too */
    mu[2].CL = 0.0;  /* accessing third element */
    (mu+2)->CL = 0.0;  /* same thing */
}

Obviously, array indexing is easier to write and read than pointer arithmetic.

BTW, neither notation tells the function how many elements are in the array. You would either have to pass that value as a second parameter or make the last array element a special invalid "end of array" value, like the null-terminator of a character string.

Last edited by dwise1_aol : July 2nd, 2003 at 01:47 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > passing an array of structures to a function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway