I'm trying to determine the number of elements in a multiarray; The approach I've been trying to use so far is:
Code:
sizeof(multi_arr) / sizeof(multi_arr[0])
but sizeof(multi_arr) seems to be returning the size of the individual components.
Anyone know of a different approach i could use?
Here's the program I'm currently working on:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
enum weekday { sun = 1, mon, tue, wed, thu, fri, sat};
typedef enum weekday weekday;
struct team{
char shortName[4];
char longName[30];
int homeWins;
int homeLoses;
int homeDraws;
int homeGoalsFor;
int homeGoalsAgainst;
int awayWins;
int awayLoses;
int awayDraws;
int awayGoalsFor;
int awayGoalsAgainst;
};typedef struct team team;
struct match{
int round;
weekday day;
int time;
char homeTeam[4];
char awayTeam[4];
int homeGoals;
int awayGoals;
};typedef struct match match;
team make_team(const char shortName[4], const char longName[30]);
team *make_league(const char *shortNames[4], const char *longNames[30]);
void add_teams(const char *shortNames[4], const char *longNames[30], team *league);
team *make_superliga(void);
team *make_superliga(void){
char shortNames[12][4] = {"FCN", "FCK", "FCM", "EFB", "SE", "RFC",
"HBK", "SIF", "BIF", "OB", "AGF", "AAB"};
char longNames[12][30] = {"FC Nordsjælland" , "F.C. København" ,
"FC Midtjylland" , "Esbjerg fB" ,
"Sønderjyske" , "Randers FC" ,
"HB Køge" , "Silkeborg IF" ,
"Brøndby IF" , "Odense Boldklub" ,
"Aarhus Gymnastikforening", "Aalborg Boldspilklub"};
return make_league(shortNames, longNames);
}
void add_teams(const char *shortNames[4], const char *longNames[30], team *league){
static int thisTeam = 0;
if(thisTeam == (sizeof(league) / sizeof(team))){
thisTeam = 0;
return;
}
else if(thisTeam > (sizeof(league) / sizeof(team))){
league[thisTeam] = make_team(shortNames[thisTeam], longNames[thisTeam]);
thisTeam++;
}
else{
printf("Should not happen ... bye!");
exit(-1);
}
}
team *make_league(const char *shortNames[4], const char *longNames[30]){
team *league;
league = malloc(sizeof(team) * (sizeof(shortNames) / sizeof(shortNames[0])));
add_teams(shortNames, longNames, league);
return league;
}
team make_team(const char shortName[4], const char longName[30]){
team result;
strcpy(result.shortName, shortName);
strcpy(result.longName, longName);
result.homeWins = 0;
result.homeLoses = 0;
result.homeDraws = 0;
result.homeGoalsFor = 0;
result.homeGoalsAgainst = 0;
result.awayWins = 0;
result.awayLoses = 0;
result.awayDraws = 0;
result.awayGoalsFor = 0;
result.awayGoalsAgainst = 0;
return result;
}
int main(void){
team *superliga;
superliga = make_superliga();
printf("%s\n", superliga[1].longName);
return 0;
}
where the interesting part is in the function make_league, when i try to allocate space for a league of teams