for loop and struct

This is a discussion on for loop and struct within the C Programming forums, part of the General Programming Boards category; With the following, what is the proper way to counter of all the entries in a struct for the exit ...

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Boston
    Posts
    12

    for loop and struct

    With the following, what is the proper way to counter of all the entries in a struct for the exit condition of a for loop (sizeof(flights)..? Also, if there is anything else wrong with what I am doing please do let me know ( I know pointers would probably be good, but haven't really got to that yet).

    Thanks!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    struct flight_str {
        char flt_no[30];
        char flt_orgin[4];
        char flt_dest[4];
        char flt_dept_t[5];
        char flt_ariv_t[5];
    };
    
    typedef struct flight_str flight;
    
    void flts_from(char dept_aport[4], flight flights[]) {
        int counter;
        for (counter = 0; counter < sizeof(flights); ++counter) {
            if (strcmp(flights[counter].flt_orgin,  dept_aport) == 0) {
                printf("Flight %s\n", flights[counter].flt_no);
            }
        }
    }
    
    int main() {
        flight flight_a[10];
        strcpy(flight_a[0].flt_no,"12432");
        strcpy(flight_a[0].flt_orgin,"LAX");
        strcpy(flight_a[1].flt_no,"55555");
        strcpy(flight_a[1].flt_orgin,"BOS");
        flts_from("BOS", flight_a);
        return(0);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would say that it's probably best if you pass the number of ACTUAL elements in the flights array to the flts_from function. That way, you don't have to worry about emptying the remainder of the array.

    sizeof(flights) inside your flts_from function will (probably) be 4 - since all arrays decay into a pointer to the first element when passed to a function.

    Are you planning to store the time as "1740" or "17:40"? If the latter, you probably need another element in the array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    Boston
    Posts
    12
    So to pass the number of elements in the array, should that just be another argument to the function like this?

    Code:
    void flts_from(char dept_aport[4], flight flights[], int size) {
        int counter;
        for (counter = 0; counter < size; ++counter) {
            if (strcmp(flights[counter].flt_orgin,  dept_aport) == 0) {
                printf("Flight %s\n", flights[counter].flt_no);
            }
        }
    }
    
    int main() {
        int arraysize = 10;
        flight flight_a[arraysize];
        strcpy(flight_a[0].flt_no,"12432");
        strcpy(flight_a[0].flt_orgin,"LAX");
        strcpy(flight_a[1].flt_no,"55555");
        strcpy(flight_a[1].flt_orgin,"BOS");
        flts_from("BOS", flight_a, arraysize);
        return(0);
    }
    No builtin to return the number of elements in the array ( or possible number of elements ... )?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    C does not track the number of elements in arrays, no.

    Of course, you only ACTUALLY fill in 2 elements, so passing 10 will potentially display rubbish, as there is no initialization of local variables.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2009
    Location
    Boston
    Posts
    12
    matsp -

    I get it now, Thank you for your help and taking the time to help out someone just starting.

    -Kyle

  6. #6
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    kbrandt,

    Code:
        flight flight_a[] = {
            { "12432", "LAX" },
            { "55555", "BOS" }
        };
    
        flts_from("BOS", flight_a, sizeof flight_a / sizeof flight_a[0]);
    it will make array for two elements, every element is a structure, in every structure non-initializated element will be initialized as 0 (or '\0' for char arrays)
    then division will count how many elements in the array (it will take all size of array in bytes and divide it to the size of one structure in this array in bytes too)

    flight_a[] mean after initialization the array will have so many elements how many initializators it had

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 08:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21