how do I make a spill over char counter in C?

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
I need to count chars starting from char 10 and count to 255 and beyond I also need arrays like one for the chars which are up to 255, the next array up to 255,255 the next 255,255,255...etc.
Have a different array for each char size up to eight chars.
How could I do this? Could someone point me in the right direction?
Thanks
 
Last edited:

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Are you ignoring the lower bytes when you cross a byte boundary?

You either need eight arrays of 255/256

Or an array of 8^8.

Your question does not provide enough details
 

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
Thank you for your response. I am ignoring only the first 10 chars everything else is kept.
I am guessing by a byte boundary you mean when the chars turn from 255 to 0,0
I am totally confused about how to go about this.
Please re read the post above I altered it.
Thanks
 

Pia

Golden Member
Feb 28, 2008
1,563
0
0
I teach C programming, and your problem description still isn't clear to me.

It sounds vaguely like a situation where you might be best off using a uint_fast64_t as the "array" so increment is always just array += 1.
You can always read/write the individual bytes with bit operations when you need them. The reading would go something like: array >> ((8-index)*8) & 0xFF.
 

esun

Platinum Member
Nov 12, 2001
2,214
0
0
I agree, just use a 64-byte integer (long long unsigned int or uint64_t if you're using C99, assuming a reasonable platform) and then use some bit operations to get the individual byte counters (write a getter function to abstract this).

It's not clear from the question whether when you do 255 + 1 it should give 1,10 or 1,1 (where you just don't care about it until it's at least 255,10). If the former, then you'd probably want to write your increment operation as a function as well that checks if the number is on a boundary and adds 10 instead of 1 in that case.
 

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
I messed up in my first post what it should have said was how do I make it do this: char1 up to 255 once over 255 it will spill over into char array2 at 0,0 to 255,255 after that it spills over into char array3 at 0,0,0
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,729
4,702
75
Are you sure it should spill over at 0,0, and not 1,0, etc? Then you could have char array3[256][256][256], have char array2=array3[0], and have array1=array2[0].
 

Onceler

Golden Member
Feb 28, 2008
1,262
0
71
Are you sure it should spill over at 0,0, and not 1,0, etc? Then you could have char array3[256][256][256], have char array2=array3[0], and have array1=array2[0].

it was my understanding that a char can hold a value between 0 and 255 so I think that 0,0 would be the next char
 
Last edited:

mv2devnull

Golden Member
Apr 13, 2010
1,533
163
106
So,
Your first index runs [0..255], but all the other indices will have one more "value": null -- [null, 0, 1, .. 255]
?

Edit: "count chars" Does that mean "count how many times each charcode occurs in text", where character set (alphabet) has N different charcodes?

Why arrays? Why not something like std::map?
 
Last edited:

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,729
4,702
75
it was my understanding that a char can hold a value between 0 and 255 so I think that 0,0 would be the next char

I suppose that depends on the character encoding you use.

If characters larger than 255 (or 127 ;)) are rare, I agree with mv2devnull about using a hash.