-2

Possible Duplicate:
Passing multidimensional array to a function C

I want to pass a 2D structure to a function Here is the prototype and the definition of the function and the "typedef" of the function.

typedef struct {
BOOLEAN valid;
event_t event_list [MAXEVS];
int num_events;
} calentry_t;

calentry_t calendar[12] [31]; // The declaration of the structure

How can i create the definition of the function that i will pass to it the 2d structure and how can i pass it please can someone spot my errors

InitializeCalendar (calentry_t *calendar){
   int x, y;
for(x = 0; x < 12; x ++) {
    if (x==2){
            for(y = 0; y <= 28; y ++){ 
                (*calendar).calendar[x][y].valid = TRUE;
            }
            for (y=29; y<= 31; y++){
                (*calendar[x][y]).valid = FALSE;
        }
    }
    else if  (x==4 || x==6 || x==9 || x==11){
        for(y = 0; y <= 30; y ++){ 
            (*calendar[x][y]).valid = TRUE;
            }
            for (y=30; y<= 31; y++){
                (*calendar)[x][y].valid = FALSE;
            }
        }
    }
}
Community
  • 1
  • 1
user1680944
  • 537
  • 2
  • 13
  • 23

2 Answers2

0
void foo(calentry_t mycal[12] [31]) {
  mycal[0][1].valid =1;
}

int main(void) {
  foo(calendar);
  printf("valid: %d\n", calendar[0][1].valid);
}
James
  • 1,341
  • 7
  • 13
  • I want to pass the structure by reference so here is what i did! but it didn't work! InitializeCalendar (calentry_t *calendar){ int x, y; for(x = 0; x < 12; x ++) { if (x==2){ for(y = 0; y <= 28; y ++){ (*calendar).calendar[x][y].valid = TRUE; } for (y=29; y<= 31; y++){ (*calendar[x][y]).valid = FALSE; } } else if (x==4 || x==6 || x==9 || x==11){ for(y = 0; y <= 30; y ++){ (*calendar[x][y]).valid = TRUE; } for (y=30; y<= 31; y++){ (*calendar)[x][y].valid = FALSE; } } } } – user1680944 Sep 18 '12 at 17:07
  • and how can i access the elements of the structure through the frunction ? – user1680944 Sep 18 '12 at 17:10
  • This is by reference and I added some example manipulation. – James Sep 18 '12 at 17:19
  • @user1680944: There is no such thing as pass by reference in C. The function here actually receives a pointer to array of 12 `calentry_t`. Everything in C is pass by copy, but you can pass pointers to achieve the same affect. There is no array copy going on here, you cannot truly pass arrays to functions. – Ed S. Sep 18 '12 at 17:21
  • @user1680944: And on a side note, structure names ending in `_t` are reserved by POSIX for use by the implementation, so... don't do that. – Ed S. Sep 18 '12 at 17:24
  • @EdS."structure names ending in _t are reserved by POSIX" do you have a link for that? `_t` is used by C99 and I could not find anything declaring it reserved in the spec. I am not sure how POSIX plays into this. – James Sep 18 '12 at 17:31
  • http://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html – Ed S. Sep 18 '12 at 17:56
  • 1
    It's even more obvious that you shouldn't do it when you consider all of the standard types which use `_t` (`size_t`, `ptrdiff_t`, etc). Even if it's not specified by the language spec, it seems like a bad idea, no? – Ed S. Sep 18 '12 at 17:58
  • Thanks for the link. I actually prefer `_t` for typedefs and `_s` for structs. Typically you are not redefining basic types so I would not think there is much worry about collision. – James Sep 18 '12 at 23:00
0

Wrap the 2D structure inside another structure.

typedef struct {
BOOLEAN valid;
event_t event_list [MAXEVS];
int num_events;
} calentry_t;

typedef struct {
    calentry_t calEntries[12][31];
} calendar_t;


calendar_t calendar;

InitializeCalendar(&calendar);


void InitializeCalendar(calendar_t *calendar)
{
    // for example
    calendar->calEntries[0][0].valid = TRUE;
}
Ken A
  • 371
  • 2
  • 4