CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

There's any way to get item count of a table?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
E_Blue



Joined: 13 Apr 2011
Posts: 416

View user's profile Send private message

There's any way to get item count of a table?
PostPosted: Mon Apr 29, 2019 8:32 am     Reply with quote

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: 19221

View user's profile Send private message

PostPosted: Mon Apr 29, 2019 8:41 am     Reply with quote

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: 122
Location: Bombay, India

View user's profile Send private message Visit poster's website

PostPosted: Mon Apr 29, 2019 8:50 am     Reply with quote

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: 416

View user's profile Send private message

PostPosted: Mon Apr 29, 2019 8:52 am     Reply with quote

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: 416

View user's profile Send private message

PostPosted: Mon Apr 29, 2019 8:53 am     Reply with quote

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: 19221

View user's profile Send private message

PostPosted: Mon Apr 29, 2019 11:18 am     Reply with quote

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: 19221

View user's profile Send private message

PostPosted: Mon Apr 29, 2019 11:26 am     Reply with quote

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: 122
Location: Bombay, India

View user's profile Send private message Visit poster's website

PostPosted: Mon Apr 29, 2019 8:58 pm     Reply with quote

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: 19221

View user's profile Send private message

PostPosted: Tue Apr 30, 2019 1:21 am     Reply with quote

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: 122
Location: Bombay, India

View user's profile Send private message Visit poster's website

PostPosted: Tue Apr 30, 2019 4:52 am     Reply with quote

Ok. I concede your explanation may be related to version.
temtronic



Joined: 01 Jul 2010
Posts: 9104
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Apr 30, 2019 4:59 am     Reply with quote

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 Smile
E_Blue



Joined: 13 Apr 2011
Posts: 416

View user's profile Send private message

PostPosted: Tue Apr 30, 2019 7:27 am     Reply with quote

@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: 19221

View user's profile Send private message

PostPosted: Wed May 01, 2019 4:00 am     Reply with quote

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...).
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group