C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 May 10th, 2003, 01:43 PM
bass20 bass20 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 16 bass20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
How-to: Passing arrays of structures

Aloha!

I nedd to pass an array of structures on to a function. How can this be done? Simply by using "new(struct *myarray)" ? Actually, I tought about passing to the function a pointer to that same array, but how do I declare that pointer?

Thanx in advance!

Reply With Quote
  #2  
Old May 10th, 2003, 02:07 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 50 m
Reputation Power: 1974
I assume that you are using C++ (the use of "new" gave it away).

Yes, to pass an array of structs to a function, you would pass a pointer to that array. However, this is simplified by the fact that the name of that array IS ITSELF a pointer; e.g.:
Code:
struct MyStruct  // you can get away with simplified struct declarations in C++
{
    int  field1;
    int  field2;
};

MyStruct  someArray[10];   // as a statically declared array
MyStruct *anotherArray;   // as a dynamically declared array
                                         // before it's used, must be initialized:
                                         //    anotherArray = new MyStruct[10];

void myFoo(MyStruct *elMio);  // one way
void myOtherFoo(MyStruct elMio[]); // another way

int main(void)
{
    myFoo(someArray);
    return 0;
}


However, if you want to create the array dynamically inside of the function and make that pointer available to the calling function, then you will need to pass a pointer to that pointer:
Code:
struct MyStruct  // you can get away with simplified struct declarations in C++
{
    int  field1;
    int  field2;
};

MyStruct  someArray[10];   // as a statically declared array
MyStruct *anotherArray;   // as a dynamically declared array
                                         // before it's used, must be initialized:
                                         //    anotherArray = new MyStruct[10];

void myFoo(MyStruct **elMio,int arraySize)  
{
    *elMio = new MyStruct[arraySize];
}

// please note that 
// void myOtherFoo(MyStruct *elMio[]); 
//   would mean something different: an array of pointers to MyStruct objects

int main(void)
{
    myFoo(&anotherArray);
    return 0;
}

You could also use a reference to a pointer instead of a pointer to a pointer, but I'm not sure of the exact syntax. You could play around with it if that's what you're trying to do.

Last edited by dwise1_aol : May 10th, 2003 at 02:09 PM.

Reply With Quote
  #3  
Old May 10th, 2003, 03:33 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
"I nedd to pass an array of structures on to a function. How can this be done?"

No differently than passing an array of int's to a function.

Reply With Quote
  #4  
Old May 11th, 2003, 05:35 PM
bass20 bass20 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 16 bass20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally posted by dwise1_aol
the name of that array IS ITSELF a pointer

OK, this was pretty obvious, I'm sorry I even asked it

Can someone point out how to pass a structure to a function? I have this function:

int ler(int maxWords, char *filename) {...}

and I need to set it to receive a termios structure; I just can find the syntax anywhere!

Reply With Quote
  #5  
Old May 11th, 2003, 08:56 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
bass20,

You mean you think there are examples that dwise1_aol didn't provide code for?

How do you declare a function to accept an int as an argument:

void func1(int);

How do you declare a function to accept a reference to an int as an argument:

void func2(int&);

How do you declare a function to accept a pointer to an int as an argument:

void func3(int*);

Since an array name acts like a pointer you can do this:

int int_array[20];
func3(int_array);

How do you declare a function to accept a structure as an argument(pass-by-value):

void func4(MyStruct)

How do you declare a function to accept a reference to a structure as an argument(pass-by-reference):

void func5(MyStruct&);

How do you declare a function to accept a pointer to a structure as an argument:

void func6(MyStruct*);

Since an array name acts like a pointer, you can do this:

MyStruct a[20];
func6(a);

If its easier for you, write your function declaration for an int type, and then substitute your structure name in place of int.

Last edited by 7stud : May 11th, 2003 at 09:18 PM.

Reply With Quote
  #6  
Old May 12th, 2003, 12:07 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 50 m
Reputation Power: 1974
Of course, if the function is to make any changes to that structure that the rest of the program needs to know about, you'll need to use a pointer or reference (assuming C++) to the structure.

BTW, if you pass a struct by value, then a copy of it will be created on the stack in the function's stack frame. I've always been uneasy about doing that, so I almost always use a pointer or a reference to the struct. Just a programming habit and personal aesthetic sense.


Or if you need an example of passing a termios structure itself, then look at the prototype for tcgetattr() or tcsetattr().

Last edited by dwise1_aol : May 12th, 2003 at 12:11 AM.

Reply With Quote
  #7  
Old May 12th, 2003, 10:25 AM
bass20 bass20 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 16 bass20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Man, this is turning into a headache
What I need to do is pass an array of structures on to a function that will print it's contents, so the call by value method will do just fine. From what I could understand from the C (not C++) manual I declared it this way:

struct {
char word[31], dia[11], hora[9];
int ocorre, duracao;
}palavras[3];

So I have an array with 4 structures, correct? So can I pass it on simply using, for example:

void printIt(int maxWords, struct *palavras);


Or should I do it this way:

struct {
char word[31], dia[11], hora[9];
int ocorre, duracao;
};

struct Palavra palavras[3];


Don't I have to specify the type of the array, or this will do?

Thanx in advance!

Last edited by bass20 : May 12th, 2003 at 10:29 AM.

Reply With Quote
  #8  
Old May 12th, 2003, 01:22 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,134 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 20 h 50 m
Reputation Power: 1974
You're just suffering from some minor confusion. Let's take this on from a strictly C point of view; struct declarations a lot easier in C++, I feel. Actually, I'm a bit weak in this C subject because I was spoiled by C++; I always have to look it up.

First, if you are going to use the struct later after having declared palavras, then you must give it a name. In C, we give it a "tag name", but when we use that tag name later we always have to use the keyword "struct" with it as well. At the end, I'll quickly cover the typedef, which eliminates the need for using "struct" everywhere.

So, try this:
Code:
struct Palavra
{
    char word[31], dia[11], hora[9];
    int ocorre, duracao;
};

struct Palavra palavras[3];

void printIt(int maxWords, struct Palavra *palavras);

The tag name is Palavra. The keyword "struct" must be used in declaring the array palavras and the struct pointer in printIt().

Quote:
Originally posted by bass20
...
So I have an array with 4 structures, correct?
...

No, you have an array with only three (3) structures. Their indices range from 0 to 2. If you want an array with four structures, then you need to declare it as such:
Code:
struct Palavra palavras[4];

Now that's an array of four structures whose indices range from 0 to 3. You knew that the number of elements in an array and the maximum index differed by one, but you just got confused as to which is one less than the other -- the number used in the array declaration is how many elements are in the array; the last element's index is always one less than the total number of elements.

OK, now for typedef. You can create your own data types with typedef. A very common usage is to rename some of the built-in types to more meaningful names or to give a more complex type definition a single name; e.g.:
Code:
typedef unsigned long ulong;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned char BOOL;
typedef unsigned char BYTE;         /* unsigned char (8 bit)    */
typedef unsigned int WORD;          /* unsigned int (16 bit)    */
typedef unsigned long DWORD;        /* unsigned int (32 bit)    */
typedef uchar __far *   FPTR ;      /* Far pointer to byte data */

But where we personally really use typedef a lot is to create a data type for a struct so that we don't have to mess with tag names. In other words, to make it as easy to use as in C++.
Code:
typedef struct  /* define a data type for the struct and call it "Palavra" */
{
    char word[31], dia[11], hora[9];
    int ocorre, duracao;
}Palavra;

Palavra palavras[4];

void printIt(int maxWords, Palavra *palavras);


Hope all that helps.

EDIT:
BTW, it is also possible to combine the struct declaration with the array declaration. It's just that I customarily use two separate statements to do that. However, you must still use a tag name if you need to reference the struct again later, eg in a function header:
Code:
/* both declare struct Palavra and the array palavras in a single statement */
struct Palavra
{
    char word[31], dia[11], hora[9];
    int ocorre, duracao;
} palavras[4];

void printIt(int maxWords, struct Palavra *palavras);

However, if you use typedef to define a struct type, then you do still need to keep them in separate statements.

Last edited by dwise1_aol : May 12th, 2003 at 02:00 PM.

Reply With Quote
  #9  
Old May 14th, 2003, 02:26 PM
bass20 bass20 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 16 bass20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

Thank you for the good, precise and complete info!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How-to: Passing arrays of structures

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap