Thread: Functions on array of structures

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    5

    Functions on array of structures

    Hello to everyone in the community first of all Programming newbie here who appreciates any help

    Straight to the point :
    I am trying to write a sample program which uses some functions on an array of structures. The actual problem is about a block of apartments (5 floors of 10 apartments each). Each one of the apartment are my structures and the whole of the apartment makes my array. And the functions make simple things like telling which is the bigger appartment and things like that.

    The data of the apartments (the structures) is considered to be already in the program and not given via input.

    Here is my part of the code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int athr=0;
    int i,j;
    
    /* 1st function */
    
    void sin_tetr(struct polikatoikia[5][10])
    {
        for (i=1;i<6;i++)
        {
            for (j=1;j<11;j++)
            {
                athr=athr+polikatoikia[i][j].tetr_metra;
            }
            printf("Total sum of  %d  floor: %d \n", i,athr);
            athr=0;
        }
    }
    
    /* My structure */
    
    struct diamerisma
    {
        char name[50];
        int arithm_diam;
        int tetr_metra; 
        int orofos;
    };
    
    /* The data of the structures */
    
    struct diamerisma d101;
    strcpy(d101.name,"Katoikos 101"); //keno meta tin parenthesi?
    d101.arithm_diam=101;
    d101.tetr_metra=50;
    d101.orofos=1;
    
    /* ommited 102 to 509 for this post */
    
    struct diamerisma d510;
    strcpy( d510.name, "Katoikos 510"); 
    d510.arithm_diam=510;
    d510.tetr_metra=40;
    d510.orofos=5;
    
    /* My array of the structures */
    
    struct diamerisma polikatoikia[5][10]={
    {d101, d102, d103, d104, d105, d106, d107, d108, d109, d110},
    {d201, d202, d203, d204, d205, d206, d207, d208, d209, d210},
    {d301, d302, d303, d304, d305, d306, d307, d308, d309, d310},
    {d401, d402, d403, d404, d405, d406, d407, d408, d409, d410},
    {d501, d502, d503, d504, d505, d506, d507, d508, d509, d510}
    };
    
    /* MAIN not yet finished nor calling functions */ 
    
    int main()
    {
    printf("Select a-d \n");
    scanf("%c", &option);
    switch(option)
    {
        case 'a' :
        break;
        case 'b' :
        break;
        case 'c' :
        break;
        case 'd' :
        break;
        default :
        printf("Lathos epilogi.\n");
    }
    
    return 0;
    }
    When I try to compile with gcc on linux, I get many errors though they seem to be coming from 2 main problems :
    a) Something with how I enter my array of structures into the function :
    imgur: the simple image sharer
    b) Initializing my array :
    imgur: the simple image sharer

    Thanx in advance for your time! Any help is appreciated! Excuse me for my bad code too..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Since you are new to programming, one of the rules that you should follow quite strictly is to avoid global variables. That is, declare your variables within functions (e.g., the main function), then pass them to other functions that need them. Doing this should address one possible problem that you're facing now.

    Other issues:
    • In your sin_tetr function, you want to name your parameter polikatoikia, but instead of writing struct diamerisma polikatoikia[5][10] you wrote struct polikatoikia[5][10].
    • You should move your definition of struct diamerisma to be before the definition of sin_tetr.
    • Your compiler is complaining that you cannot initialise polikatoikia using non-constant expressions. One solution is this: instead of coming up with a huge number of variables named d101, d102, etc, declare the array first, and then assign to polikatoikia[0][0], polikatoikia[0][1], etc. In fact, you may be able to do this in a loop.
    • You need to indent your code better. Good to see that you made some effort, but the main function needs a little more work in this area.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    5
    Thanx a lot for the answer That was fast! I will go back to the laptop where my linux resides, follow your advices and help (thanx) and return with hopefully good news! Thanx again!

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you're also going to go out of the bounds of your array in sin_tetr. array indexes start at zero and go to n-1, where n is the number of elements in the array. you are trying to access index 1 thru n, which will produce undefined behavior.

    I'd rewrite your function to look more like this:

    Code:
    #define rows 5;
    #define columns 10;
    
    void sin_tetr(struct polikatoikia[rows][columns])
    {
        for (i=0;i<rows;i++)
        {
            for (j=0;j<columns;j++)
            {
                athr=athr+polikatoikia[i][j].tetr_metra;
            }
            printf("Total sum of  %d  floor: %d \n", i,athr);
            athr=0;
        }
    }
    note the use of constants to define the size of the array and the number of iterations of the for loop.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    5
    First of all thankx again for the VERY helpful replies and I'm glad I joined this community!

    I did exactly as told Laserlight you were right about all! I declared correctly the function with the missing word and reordered the whole code that while the function is declared in the beggining, i put it at the bottom after main() and also I move the declaration of the structure at the top (left it global). Put the the 50 structures themselves with the array declaration inside the main, - did NOT though change the declaration of the array,see bellow-). Elkvis thank you, I corrected the error inside the function with the array and getting out of bounds!

    After the above gcc just compiled it! Of course, I do not even call the function in main to check if it does what it should let alone the rest of the things, but if that at least compiled with no error I think I am in a good road and thank you again !

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, these lines contain typo errors:
    Code:
    #define rows 5;
    #define columns 10;
    Elkvis meant to write:
    Code:
    #define rows 5
    #define columns 10
    but then it is conventional for such macro names to be in uppercase, since macros do not obey the normal rules of scope:
    Code:
    #define ROWS 5
    #define COLUMNS 10
    and in a larger program, you might use names that are less likely to introduce a name collision, e.g., POLIKATOIKIA_ROWS.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2014
    Posts
    5
    Thanx again Laserlight I actually just changed inside the ifs the i and j to be from 0 to 5 and 0 to 10 , without using the global variables to define them so I didn't notice the typo in the declaration!
    sing
    The only problem I seem to have now, is probably the array itself and its initialization.. While I dont get any errors when compiling without using the initialization you proposed, and instead keeping just declaring the array with the structs inside as I did in my first post, it seems that maybe it creates problem in the end with the function, since that after I tried to call the function, the compiler gives error :

    The code on this part :

    Code:
    switch(option) 
    { 
        case 'a' :
        sin_tetr(polikatoikia[5][10]); 
        break; 
        case 'b' : 
        break; 
        case 'c' : 
        break; 
        case 'd' : 
        break; 
        default : 
        printf("Lathos epilogi.\n"); 
    }
    Code:
    void sin_tetr(struct diamerisma polikatoikia[5][10]) 
    { 
        for (i=0;i<5;i++) 
        { 
            for (j=0;j<10;j++) 
            { 
                athr=athr+polikatoikia[i][j].tetr_metra; 
            } 
            printf("Sinolika Tetragonika %d  Orofou: %d \n", i,athr); 
            athr=0;
        } 
    }
    Whichever way I try to call the function I get error.. Above I left it as I should I think but I even tried to call the function with other typing (like diamerisma polikatoikoia[5][10] etc ) and got only errors :

    imgur: the simple image sharer

    The first error (..expected (*)[10]...) is what I get if I call it the above way and the other errors are just retries with other typing..

    Only thing is to try to initialize it another way as Laserlight told me, but still not sure how to do that..

    Thanx a lot for the help already, yesterday night I felt no progress would be done and I'm stuck and today I'm beggining to see the end of the tunnel for the case! Thanx again!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should call it with:
    Code:
    sin_tetr(polikatoikia);
    polikatoikia[5][10] is a particular element of the array polikatoikia[5], which itself is a particular element of the array polikatoikia. Worse still, these elements don't exist since the indices are invalid.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by laserlight View Post
    By the way, these lines contain typo errors:
    Code:
    #define rows 5;
    #define columns 10;
    I initially had them as const int values, but realized that's not really the C way, and forgot to remove the semicolons when I changed them to macros.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  10. #10
    Registered User
    Join Date
    Feb 2014
    Posts
    5
    Again Many thanx for your help, the code is ready and working as intended on all my functions

    One last small but concering trivial bit is my menu though.. Right now it just does one of the 4 functions and exits or exits after wrong input.. Can you help me fixing that so that it only exits from a certain 5th option and doess NOT exit by wrong input or after one of the functions? I googled and saw Basic C menu too, but the solutions I tried give me errors, gcc in ubuntu linux gives various warnings to the things proposed there.. imgur: the simple image sharer

    here is my not very good menu:
    Code:
    char option; 
    printf("welcome\n"); 
    printf("Select an option\n"); 
    printf("a.option a\n"); 
    printf("b.option b.\n"); 
    printf("c.option c.\n"); 
    printf("d.option d.\n"); 
    scanf("%c", &option); 
    switch(option) 
    { 
        case 'a' : 
        sin_tetr(polikatoikia); 
        break; 
        case 'b' : 
        pliromes(polikatoikia); 
        break; 
        case 'c' : 
        epilogi(polikatoikia); 
        break; 
        case 'd' : 
        megal(polikatoikia); 
        break; 
        default : 
        printf("Lathos epilogi.\n"); 
    } 
     
    return 0; 
    }
    Again, awesomeness, with your great help in one day I completed it ty

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and structures in C
    By Kurse in forum C Programming
    Replies: 7
    Last Post: 08-10-2012, 02:17 AM
  2. Functions in Structures
    By pritin in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2007, 01:40 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Array of Structures and Functions
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 05-04-2003, 07:06 AM
  5. passing array structures to functions
    By lukejack in forum C Programming
    Replies: 2
    Last Post: 04-08-2003, 02:17 PM