I created 3 records in this Struct. How can I tell how many records she has? How to identify that she has 3 records?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?Code:#include <stdio.h> struct DbNames { char Names[5]; }; int main() { struct DbNames List[] = { {"A"}, {"B"}, {"C"} }; }
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:
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.Code:size_t size = sizeof DbNames / sizeof DbNames[0];
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); }
A little inaccuracy saves tons of explanation. - H.H. Munro
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.
This below seemed right, it counts 3 Records. This is true? Or is it wrong what I did?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
Code:for(int i = 0; i < (sizeof(List) / sizeof(List[0])); i++) puts("X");
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):
Second: Remember you declared your structure asCode:#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); }
Here "Names" is an array. The identifier 'Names' will be converted to a pointer in statements like this:Code:struct DbNames { char Names[5]; };
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:char *p = list[0].Names;
Code:list[0].Names[0] = '\0';
Last edited by flp1969; 08-12-2019 at 01:08 PM.