About Structures and Nested Arrays
Author |
Message |
ahsonmonis
|
Posted: Thu Oct 22, 2009 12:38 am Post subject: About Structures and Nested Arrays |
|
|
struct event schedule[] = {{{9,45},{9,55}},{{13,0},{14,20}},{{15,0},{16,30}}};
So how can I read each individual entry?
I want to use this and compare it to an integer.
So basically lets say i have h = 9,
I want to see if h>9 or h<9 and h>13 or h<14 and h>15 or h<16
but i cant access the numbers in the schedule array because it is an array of arrays.
How can I see what the first element is, the second element etc.
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
OneOffDriveByPoster
|
Posted: Thu Oct 22, 2009 12:23 pm Post subject: Re: About Structures and Nested Arrays |
|
|
First, some formatting:
c: | struct event schedule[] = {
{{ 9, 45}, { 9, 55}},
{{13, 0}, {14, 20}},
{{15, 0}, {16, 30}}
}; |
Then, it may help if you provided a declaration of struct event.
c: | struct event {
int a[2], b[2];
}; |
Then, you can try something like:
|
|
|
|
|
![](images/spacer.gif) |
ahsonmonis
|
Posted: Thu Oct 22, 2009 6:43 pm Post subject: Re: About Structures and Nested Arrays |
|
|
I tried it but gave errors.
code: | struct event schedule[] = {{{9,45},{9,55}},{{13,0},{14,20}},{{15,0},{16,30}}}; |
Ok purpose: Print the first entry and then print the second last entry.
So output be like 9, 16
How can I do that.
code: |
// File name is arrayHelp.c
#include <stdio.h>
#include "arrayHelp.h"
int main (void)
{
struct event schedule[] = {{{9,45},{9,55}},{{13,0},{14,20}},{{15,0},{16,30}}};
struct event {
int a[2], b[2], c[2];
};
printf ("%d\n",schedule[0].a[0]);
printf ("%d\n",schedule[1]);
printf ("%d\n",schedule[2]);
system ("pause");
return 0;
} |
code: |
// Filename is arrayHelp.h
struct tod {
int hour, minute;
};
struct event {
struct tod start, end;
};
int freetime (struct event schedule[], int n, int hour, int min);
|
It is still giving me an error. I attached the programs as well.
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
arrayHelp.h |
Filesize: |
174 Bytes |
Downloaded: |
209 Time(s) |
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
arrayHelp.c |
Filesize: |
341 Bytes |
Downloaded: |
240 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
ahsonmonis
|
Posted: Thu Oct 22, 2009 6:44 pm Post subject: Re: About Structures and Nested Arrays |
|
|
oh also, how do u add code tags so it is properly formatted and colored.
|
|
|
|
|
![](images/spacer.gif) |
OneOffDriveByPoster
|
Posted: Thu Oct 22, 2009 10:08 pm Post subject: Re: About Structures and Nested Arrays |
|
|
ahsonmonis @ Thu Oct 22, 2009 6:44 pm wrote: oh also, how do u add code tags so it is properly formatted and colored. You can see how by clicking on the "quote" button on the top-right corner of a post that has that. It will only get you the colour though. Space it yourself, or dump it in something that will auto-format first.
Anyhow, when I said to provide a declaration, I meant to the forum. The code I gave was only an example. You do have a declaration in your code already. To access a struct member correctly, it is necessary to know how the struct type was declared.
Your relevant declarations are: c: | struct tod {
int hour, minute;
};
struct event {
struct tod start, end;
}; |
So you can refer to the `9' like so:
c: | schedule[0].start.hour |
|
|
|
|
|
![](images/spacer.gif) |
AtoZ
|
Posted: Fri Oct 23, 2009 3:37 am Post subject: Re: About Structures and Nested Arrays |
|
|
Thanks alot. That is exactly what I was looking for.
|
|
|
|
|
![](images/spacer.gif) |
|
|