Thread: Number of elements in multiarray

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Number of elements in multiarray

    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

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    Thanks, that solved the problem.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Location
    Austria
    Posts
    10
    C treats the following definitions the same (sad but true).
    Code:
    int foo(int bar[10]);
    int foo(int bar[]);
    int foo(int *bar);
    therefore sizeof bar is always the size of a single pointer (4 on most 32 bit machines).

    I think the compiler should warn about the first definition, as there is unused and therefore misleading information in the prototype, but compilers don't seem to care what I'm thinking.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I was just thinking... perhaps the compiler does look at the array size in the prototype/header. But not for the example you've given. What about
    Code:
    int foo(int bar[][10]);
    In that case inside the function the compiler knows what the width of the array is while calculating row offsets. I haven't tested this but it seems reasonable. Still, any number in the FIRST axis, if present, is always ignored.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. number of elements in double linked list
    By cnu_sree in forum C++ Programming
    Replies: 4
    Last Post: 01-25-2008, 12:18 PM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  4. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  5. letting user input number of elements in array
    By iamthejake2000 in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2005, 12:35 PM

Tags for this Thread