Thread: How do I count the records of a Struct? (Beginner)

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    12

    Question How do I count the records of a Struct? (Beginner)

    Code:
    #include <stdio.h>
    
    struct DbNames {
       char Names[5];
    };
    
    int main() {
       struct DbNames List[] = { {"A"}, {"B"}, {"C"} };
    }
    I created 3 records in this Struct. How can I tell how many records she has? How to identify that she has 3 records?

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    627
    The basic answer is that you already know how many records there are. Three. Problem solved.

    If you wish to calculate the size, then in the case of a statically-allocated array for which you have an actual array variable (not just a pointer), you can determine the size like this:
    Code:
    size_t size = sizeof DbNames / sizeof DbNames[0];
    sizeof yields the total number of bytes in the object. Dividing the total number of bytes in the array by the number of bytes in a single element yields the number of elements in the array.

    However, this will not work once you've passed the array to a function since the array "decays" to a pointer and thus loses its total size information. In that case you can pass the size into the function as well as the array.

    For an array that is only partially filled, you would keep track of how many records there are. If you add one in, increment the size variable. If you remove one, decrement it.

    Another way to determine the size of an array is to store a special "null" element at the end, like the '\0' at the end of a string.
    Code:
    #include <stdio.h>
     
    typedef struct Person {
        char name[50];
        int age;
    } Person;
     
    void print_people(const Person *people) {
        for (size_t i = 0; people[i].name[0]; ++i)
            printf("%-20s %3d\n", people[i].name, people[i].age);
    }
     
    int main() {
        Person people[] = { {"Alan", 18}, {"Bob", 24}, {"Carol", 21}, {"", 0} };
        print_people(people);
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    12

    Lightbulb

    Quote Originally Posted by john.c View Post
    Code:
    Person people[] = { {"Alan", 18}, {"Bob", 24}, {"Carol", 21}, {"", 0} };
        print_people(people);
    }
    Firstly thank you very much for your reply. I liked the idea you gave! It's just that I believed the NULL or \0 element already existed in Struct. I have tried many times unsuccessfully to locate him.
    Code:
       if(List[3].Names == 0)
          puts("OK 3"); // NO WORK
    
       // if(List[3].Names == '\0') // Don't accept it | warning: comparison between pointer and zero character constant
       //    puts("OK 4");
    
       if(List[3].Names == "\0")
          puts("OK 5"); // NO WORK
    
       // if(List[3].Names == '0') // Don't accept it | warning: comparison between pointer and integer
       //    puts("OK 6");
    
       if(List[3].Names == NULL)
          puts("OK 7"); // NO WORK
    
       if(List[3].Names == "")
          puts("OK 8");  // NO WORK
    This below seemed right, it counts 3 Records. This is true? Or is it wrong what I did?
    Code:
    for(int i = 0; i < (sizeof(List) / sizeof(List[0])); i++)
       puts("X");

  4. #4
    Registered User
    Join Date
    Nov 2018
    Posts
    12

    Talking

    Quote Originally Posted by john.c View Post
    If you wish to calculate the size, then in the case of a statically-allocated array for which you have an actual array variable (not just a pointer), you can determine the size like this:
    Code:
    size_t size = sizeof DbNames / sizeof DbNames[0];
    I believe I could understand your example now. It helped a lot!
    Code:
    size_t size = sizeof List / sizeof List[0];
       printf("%d\n", size);

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    500
    Ok, @marcelo.br (from Brazil? I'm brazillian too!), but keep in mind what john.c told you: If you pass an array as a function argument, it will be "transformed" in a pointer and you'll loose the capability to get que sizeof the entire array. In this case, it is safer to use a terminal element to check if you got at the end of the array... His example is very clear (my comments):
    Code:
    #include <stdio.h>
      
    typedef struct Person {
        char name[50];
        int age;
    } Person;
    
    void print_people(const Person *people) {
      // the criteria, here, is to stop the loop when
      // an empty string is found on 'name' array (the first char
      // is a nul char). You could choose to check if people[i].age is zero,
      // instead of people[i].name[0] is '\0'.
      for (size_t i = 0; people[i].name[0]; ++i)
        printf("%-20s %3d\n", people[i].name, people[i].age);
    }
    
    int main() {
      // Notice the last element!!
      Person people[] = { {"Alan", 18}, {"Bob", 24}, {"Carol", 21}, {"", 0} };
      print_people(people);
    }
    Second: Remember you declared your structure as
    Code:
    struct DbNames {
       char Names[5];
    };
    Here "Names" is an array. The identifier 'Names' will be converted to a pointer in statements like this:
    Code:
    char *p = list[0].Names;
    But 'Names' isn't a poniter! You cannot assign NULL, 0 or a literal string (which is a pointer anyway) to it. You can assign a NUL char to the first element of 'Names' array and this will assign an empty string to this array:
    Code:
    list[0].Names[0] = '\0';
    Last edited by flp1969; 08-12-2019 at 01:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Edit and Delete struct records that are saved in file?
    By Supremezzy in forum C Programming
    Replies: 2
    Last Post: 05-04-2016, 10:31 AM
  2. Replies: 13
    Last Post: 11-18-2009, 01:05 AM
  3. fread records into a struct
    By Machiaveli in forum C Programming
    Replies: 1
    Last Post: 10-14-2006, 03:35 PM
  4. display records held in a struct
    By colinuk in forum C Programming
    Replies: 3
    Last Post: 02-02-2005, 07:51 AM
  5. Structure Records (Beginner)
    By zacpack in forum C Programming
    Replies: 2
    Last Post: 03-13-2002, 12:33 PM

Tags for this Thread