Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: How to write own function to allocation a set of structures? |
| Date | 2012-03-20 21:13 +1300 |
| Message-ID | <9sqshlFc6rU1@mid.individual.net> (permalink) |
| References | <354bb461-708b-4ce5-99ad-ab0b37872797@x10g2000pbi.googlegroups.com> <rmsfm7p8u92iun6n37sn6m6qp7ihabvnl3@4ax.com> <8223ddb2-04c4-435e-9e6a-4c56c3f0cba9@vy8g2000pbc.googlegroups.com> |
On 03/20/12 07:58 PM, Martin De Kauwe wrote:
> Sorry I am not sure I quite sure I followed that. I do have variable
> length structures, e.g. some are X parameters long, some are a set of
> controlling flags. I tried implementing the first example, but I am
> clearly misunderstanding how to do this.
>
> in the .h file
>
> typedef struct {
> FILE *ifp;
> int num_days;
> etc...
> } control_struct;
>
> void allocate_structure(structure **);
>
> in the .c file
>
> int main(int argc, char **argv)
> {
> /* Allocate all structures */
> control_struct *ctrl;
> allocate_structure(&ctrl);
> etc...
>
> void allocate_structure(structure **s)
> {
> if ((*s = malloc(sizeof(*s))) == NULL) {
> fprintf(stderr, "some msg\n");
> exit(EXIT_FAILURE);
> }
>
> }
>
> it says it doesn't know what 'structure' is when it compiles. I must
> be misunderstanding as this is what I didn't get, i.e. how to pass the
> structure, what you call it when you pass it to the function.
It sounds like you are looking for C++ style templates, which C lacks.
In C, you would have to resort to a macro. Something like
#define ALLOCATE_STRUCTURE( s ) \
if ((s = malloc(sizeof(*s))) == NULL) { \
fprintf(stderr, "some msg\n"); \
exit(EXIT_FAILURE); } \
--
Ian Collins
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to write own function to allocation a set of structures? Martin De Kauwe <mdekauwe@gmail.com> - 2012-03-19 19:32 -0700
Re: How to write own function to allocation a set of structures? Ian Collins <ian-news@hotmail.com> - 2012-03-20 15:50 +1300
Re: How to write own function to allocation a set of structures? Barry Schwarz <schwarzb@dqel.com> - 2012-03-19 20:14 -0700
Re: How to write own function to allocation a set of structures? Martin De Kauwe <mdekauwe@gmail.com> - 2012-03-19 23:58 -0700
Re: How to write own function to allocation a set of structures? Ian Collins <ian-news@hotmail.com> - 2012-03-20 21:13 +1300
Re: How to write own function to allocation a set of structures? Martin De Kauwe <mdekauwe@gmail.com> - 2012-03-20 02:32 -0700
Re: How to write own function to allocation a set of structures? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-20 13:06 +0000
Re: How to write own function to allocation a set of structures? ImpalerCore <jadill33@gmail.com> - 2012-03-20 07:27 -0700
csiph-web