View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
There's any way to get item count of a table? |
Posted: Mon Apr 29, 2019 8:32 am |
|
|
I have the following tables
Both tables should have the same item count.
Code: |
const char ID_Index[4][4]=
{"AAA","BBB","CCC","DDD"};
const char ID_Text[][*]=
{"Text 1","Text 2","Text 374"};
|
And I want to get item count, not the sizeof, each table.
In the example above I need to get 4 and 3 so I can compare with a pre-processor command so I get an error when the item count is not equal in both tables. _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Apr 29, 2019 8:41 am |
|
|
You can't.
C doesn't offer any way to find the number of items in a table. Only Sizeof.
If you declare a define for the number of elements per row, then
sizeof/ELEMENTS, will give the number of rows. |
|
|
Jerson
Joined: 31 Jul 2009 Posts: 125 Location: Bombay, India
|
|
Posted: Mon Apr 29, 2019 8:50 am |
|
|
There is a popular macro called Nelements(x)
and is
#define Nelements(x) sizeof(x)/sizeof(x[0])
This will tell you the number of elements in an array
Hope that helps |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Mon Apr 29, 2019 8:52 am |
|
|
Thanks for your answer.
So I suppose then that I must be careful and count them by myself. _________________ Electric Blue |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Mon Apr 29, 2019 8:53 am |
|
|
Jerson wrote: | There is a popular macro called Nelements(x)
and is
#define Nelements(x) sizeof(x)/sizeof(x[0])
This will tell you the number of elements in an array
Hope that helps |
I will try this, thanks for your input. _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Apr 29, 2019 11:18 am |
|
|
That only works for things like floats, int32's etc., where the elements have
a 'size'. So you get the number of elements by dividing the size of the
entire array, by the size of one element.
Problem here is that an array of 'strings', is just an array of characters.... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Apr 29, 2019 11:26 am |
|
|
As a comment, you could use this solution, if you typedef a single
dimensional character array large enough for the line elements, and then
make the final array an array of these elements, instead of a 2D character
array. |
|
|
Jerson
Joined: 31 Jul 2009 Posts: 125 Location: Bombay, India
|
|
Posted: Mon Apr 29, 2019 8:58 pm |
|
|
I beg to disagree here. An array of "pointer to string" has a fixed size for each of its members that point to individual strings. Therefore, the number of elements can be determined accurately. I use it in my projects to determine the number of elements in a menu. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Apr 30, 2019 1:21 am |
|
|
A lot depends on how recent his compiler is.
Historically, 'const' didn't support pointers. So a const array as is shown here
was constructed internally as a 2D char array, not an array of pointers.
However if he is using a modern compiler, this can be built as an array
of pointers, so the size of each element will be the size of a pointer, and
the trick being suggested, will then work.
This is why telling us what compiler version is involved is always
necessary.... |
|
|
Jerson
Joined: 31 Jul 2009 Posts: 125 Location: Bombay, India
|
|
Posted: Tue Apr 30, 2019 4:52 am |
|
|
Ok. I concede your explanation may be related to version. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Tue Apr 30, 2019 4:59 am |
|
|
OK, maybe I'm looking at this wrong, but if the data is always some 'text', could you just read , test every element using 'isalpha(x)' and count them ?
Then compare the two counts and if not equal, do 'something' ?
I know this would not be fast for large tables but it 'works fine' in my head....
Jay |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Tue Apr 30, 2019 7:27 am |
|
|
@temtronic
The two tables are related, is like a dictionary.
I search the text in the first table and if there any coincidence I use the same index to get the text of the second table; so both tables must have the same item count.
Each item in the first table always have the same length.
The second table does not; each element have a variable length of characters.
Time to time I must to edit add some new codes and / or delete some others; So I want some kind of warning / error to let me know that I made a mistake. _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed May 01, 2019 4:00 am |
|
|
So long as you have a modern compiler, the test outlined should work.
However you are asking about doing it with the preprocessor. No.
Quote: | Preprocessing directives are evaluated before the source is parsed (at
least conceptually), so there aren't any types or variables yet to get their size.
|
You could potentially do something that would trigger a compiler error, by
generating a deliberate compile error. So (for example), if you code
something that results in division by zero at compile time, the compiler
will error. So since the tables should be the same size, the results of
the two sizeof divisions should be the same on both. So one minus the other
should equal zero. Make a variable equal 1/!this, and it should error if
they don't match (note the not...). |
|
|
|