Noob question alert - arrays

Hey all

so, I have a pile of arrays.... E.G:

byte dmxuni_1CH[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

my brain is totally fried today for some reason.

I am right in thinking that:

a) I need to declare [10], for the 10 variables (complies fine)
b) The first variable in the array is addressed as 0?

Therefore to write the first variable (example): dmxuni_1CH[0] = 255;

I'm clearly getting old

That's right.

a7

a) not always, as global you can write
byte dmxuni_1CH[10]; all cells declares as 0x00,
or
byte dmxuni_1CH[]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
in the scope you must declare cells self;

b) yes

You can omit the number of elements, if you initialize it just at the time of array creation:

int arr[] = {1,2,3,4};

compiler deduced that you want to create an array of four elements

Everyone is getting old. Younger ppl are getting older faster then older ppl, so there's that.

As you have been shown, there are some extra subtleties around initialisation. Global get zero in,escs you somehow say otherwise.

You can out all this zeroes there, and ppl do. To me it is just noise, but sometimes it make ps a point (to readers, not the compiler) that you are depending on it, and it doing another kind of initialisation elsewhere in code, like at setuo() time.

a7

Yes. @baffled2023 you can get it do more deducing using commonly seen expression, viz:

int arr[] = {1,2,3,4};
const int numberInArr = sizeof arr / sizeof arr[0];

will put 4 into numberInArr, so you can use that all over instead of 4, and never need to change it if you change the number of elements in the array.

HTH

a7

Thanks all.

Been writing some code for a DMX controller...

5x DMX Universes...
10x Channels per Universe
511x Addresses per Channel
255x ON Levels per Address
511x Ramp up timings per Address
511x Hold timings per Address
511x Ramp down timings per Address
511x Toggle states per Address
15x Playback options

All need setting up in a menu, storing in EEPROM and replaying.

My 56yr old brain is having trouble keeping up...

may be worth having a look at C++ Array initialization

what microcontroller are you planning to use?

Using a Teensy 4.1. Has 8x Serial ports and plenty of EEPROM + programming space.

5x Serial ports run the DMX outputs
1x Serial port is connected to a Nextion screen

Works well (so far!)

LOL. A youngster!

and so forth.

You may want to learn about multidimensional arrays.

You might also like the struct feature of C/C++ which lets you place variables of possibly different types that would like to travel around together into a package.

Then… your might really like arrays of structs.

Bookmark it for learning time.

a7

2 Likes

look like a professional software

I try!

It's a hobby project however... nothing amazing

most of them is not arrays

255x ON Levels per Address    >> single byte value
511x Ramp up timings per Address   >> uint16_t 
511x Hold timings per Address   >> uint16_t 
511x Ramp down timings per Address   >> uint16_t 
511x Toggle states per Address  >> uint16_t 

I don't know what code you are reading.

Maybe they aren't, but 10 channels for each of 5 universes sounds awfully like something for which one might consider using a two D array or array of structs.

a7

I mean that OP put in the same list arrays "5x DMX Universes...10x Channels ...." and non-array data

Not arrays?

I don't want to start undoing my thinking here....

int dmxuni_1CH[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
For instance, that holds the 10x possible (1-511) channels for Universe_1

int dmxADD_1CH[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
This holds the 10x addresses (1-511) for each channel in Universe_1

byte dmxRMP_1CH[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
This holds the 10x ramp up times in seconds (0-255) for each address in Universe 1

bool dmxTGL_1CH[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
This holds the 10x toggle states (0-1) for each address in Universe_1

I know what I mean

That is key. You do you; just keep in mind that there is always something new to learn, and that said somethings may be just the ticket one day.

I'll type something when I get to the lab to show you what I am talking about.

a7

Or not. Looking at only the code you posted, it is a guess as to how much data sprawls all over the place and what needs to be 10 of something or 5 of another. And what would be sensibly packaged to travel around together.

In general, however, making new things by appending a digit, as it looks like and by comment can be inferred you are doing, viz:

int dmxuni_1CH[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
For instance, that holds the 10x possible (1-511) channels for Universe_1

is a sign that maybe rather than

int dmxuni_1CH[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int dmxuni_2CH[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int dmxuni_3CH[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int dmxuni_4CH[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int dmxuni_5CH[10] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

a 2D array would work:

const int N_UNIVERSES = 5;
const int NCHANNELS = 10;

int dmxuni[N_UNIVERSES][NCHANNELS];

Then a channel X from universe Y could be accessed

 value = dmxuni[Y][X];

Yes, they would be indexed using 0 through 4 for the universe number. That's just C/C++, get accustomed to treating zero as a perfectly good place to start numbering things. :expressionless:

This would afford advantages to coding around the data - loops and functions can be generalized rather than copied/pasted/edited N - 1 times to make N things happen, as well as a maintenance nightmare.

a7

Interesting.

Not entirely sure in my case that would make things easier.
Can you have more that 2?

E.G.

const int N_UNIVERSES = 5;
const int NCHANNELS = 10;
const int VALUE = 10;
const int TIMING = 10;

int dmxuni[N_UNIVERSES][NCHANNELS][VALUE][TIMING];

It's entirely probable I would get completely lost!

Yes, and yes it might be of mixed value.

In general, again, anything you can make, you make into an array of them.

So an array of 1 dimensional arrays can be made into an array of them, a 2D array then.

An array of N dimensions can be the basis of an array of them, an N + 1 dimensional array.

What processor are you writing for? OIC, a Teensy. I asked because

That's ten timings for ten values for ten channels for five universes, all totally distinct and here amounting to 5000 ints or 10000 bytes.

Which is why some of those that don't need to be 16 bit integers might be better as bytes, for example.

Now you've mixed types, and a structured variable (struct) would be the preferred way to group things that wanted to travel around together.

Again, without seeing 'xactly what you are up to, it is just speculation and hand waving. I just wanted to make sure you have this on the back of your mind for when the perfect sitch arrives.

a7