Problems with size of array

Hi,

Why is

const int pins[6] = {5,6,7,8};
const int numOfPins = sizeof(pins);

returning (with a Serial.print):

numOfPins = 12 ????? and not 4?? because there are 4 items in the array??

sizeof returns the number of bytes in an array. An int (2 bytes) array of 6 elements holds 12 bytes. If you want the number of elements, divide the number of bytes in the array by the number of bytes in the data type. sizeof(array)/sizeof(array[0]) for instance or sizeof(array)/sizeof(int).

When in doubt, there's always the reference

groundfungus has shown you a common idiom for finding array size:

#define ELEMENTS(x) (sizeof(x)/sizeof(x[0]))   // At top of program, outside any function, with global scope

// more code

// perhaps the following is in loop()

   float pressure[10];

// read some pressure sensors...
// ...now show them...

   for (int i = 0; i < ELEMENTS(pressure); i++) {
      Serial.print(pressure[i]);
   }

One of the nice things about this parameterized macro is that it is “typeless”. That is, you can use it with whatever data type you need: char, float, int…whatever.

Ok, thanks !! it works perfect!!

but another question:
how can I fill the matrix
instead of using:

byte addresses[][6] = {"1Node", "2Node", "3Node", "4Node"};

with something like:

  for (int i=0; i< ELEMENTS(pins); i++) { 
    addresses[i][6] = i + "Node"; 
}

But the code above doesn’t work :frowning:

...and yet another demonstration of why you should use code tags when posting code. (A six element array doesn't have index 6, and you can't add strings and ints. - try sprintf)

BramWerbrouck: numOfPins = 12 ????? and not 4?? because there are 4 items in the array??

There are six(!) items in the array, because you declared the array to have six integers! const int pins[6] is actually an array of six elements!

You only initialize four elements in the array, but that doesn't make a difference on the size of the array.

six integers, each 2 bytes in size, makes a total of 12 bytes in the array.

If I'd guess, you'd possibly wanted to code that:

const int pins[] = {5,6,7,8}; 
const int numOfPins = sizeof(pins)/sizeof(int);

In that code I leave the number of elements undeclared, it is actually defined by the numbers of initializing elements.

Then the number of elements is calculated: Number of bytes in the array divided by number of bytes in one element of the array.

@AWOL: spint doesn’t do the trick… :frowning:

BramWerbrouck: @AWOL: spint doesn't do the trick... :-(

No, I'm sure it doesn't, but sprintf will.

AWOL: but sprintf will.

Sorry, I hate autocorrect...

I meant that sprintf doesn't do the trick

I meant that sprintf doesn't do the trick

Are you sure ? Show us what you tried.

byte addresses[][6] = {}; 


char cBuffer[10];

for (int i=0; i< ELEMENTS(pins); i++) { 
   sprintf(cBuffer,  "%dNode", i +1);

    addresses[i][6] = cBuffer;
  }

I already pointed out that a six element array doesn't have an element with index six. Why don't you just sprintf direct into "addresses"?

I already pointed out that a six element array doesn't have an element with index six. It's not about the elements, but the value of the elements:

I expected this in the array addresses[][6] :

addresses[][6]= {"1Node","2Node","3Node","4Node"};

But I see this in the serial monitor (if I use Serial.println(addresses*[6])):* 205 205 205 205

It's not about the elements, but the value of the elements:

OK, good luck with that approach. Let us know how you get on.

addresses*[6] = cBuffer; is not what you want*

byte addresses[][6] = {};


sprintf(cBuffer,  "%dNode", i +1);
addresses[i][6] =  (byte)atoi(cBuffer);
Serial.println(addresses[i][6]);

gives me :

1 2 3 4 5

byte addresses[][6] = {"1Node", "2Node", "3Node", "4Node"};

Serial.println(addresses[i][6]);

gives me 50 51 52 0 254

Anyone???

Let me restate: a six element array has the following valid indices: 0,1,2,3,4,5.

You will notice that 6 does not appear in that sequence.

Why don't you sprintf directly into "addresses"?

Whatever you do, make certain that you're not writing to memory you don't own.

sprintf(addresses[i], "%dNode", i + 1);

and

Serial.println(addresses[i]);

ok I take pity…but think about these things:
why use char rather than byte for addresses?
if 9 becomes 10 or more, then 6 must become at least 7. why?

char addresses [9][6];


for (int ix = 0; ix < sizeof(addresses)/sizeof(addresses[0]); ix++)
{
   sprintf(addresses[ix],"%dNode",ix+1);
   Serial.println(addresses[ix]);
}