Get correctly the sizeof()

I have several byte arrays and an array of that byte arrays.
I need to get the size of the byte array in a certain index.

byte A[]={B01111110,B10001000,B10001000,B10001000,B01111110};
byte B[]={B11111110,B10010010,B10010010,B10010010,B01101100};
byte gamma[]={B11111110,B10000000,B10000000,B10000000,B10000000};

byte *test[]={A,B,gamma}

Serial.println(sizeof(gamma));
Serial.println(sizeof(test[2]));

The first serial pront gives 5 that is correct the second give 2(probably the size of the pointer).

How can i get the correct result cause i want to use the test in a for loop or i will have to use nested else if.

How can i get the correct result

The correct result for what?

test[2] is a pointer. The size of the pointer IS 2.

You can NOT get the size of the block of memory that the pointer points to. That's why C strings are NULL terminated arrays of chars. The NULL terminator is how functions that take strings (via pointers) determine the "size" of the pointed to data.

So there is no way to do it this way i will have to manage something else then?

So there is no way to do it this way i will have to manage something else then?

Correct. The easiest ways are to create another array that contains the sizes or add some kind of end-of-data marker to each array that test points to. If 0 or 255 are not legal values, they can be used as end-of-data markers. If they are, then a separate array of sizes is needed.

If you use a multidimensional array you can get the size of each dimension:

byte test[][5] = {
 {B01111110,B10001000,B10001000,B10001000,B01111110},
 {B11111110,B10010010,B10010010,B10010010,B01101100},
 {B11111110,B10000000,B10000000,B10000000,B10000000},
};

void setup() {
  Serial.begin( 9600 );
  Serial.println( sizeof( test ) / sizeof( *test ) );
  Serial.println( sizeof( test[2] ) / sizeof( *test[2] ) );
  Serial.println( sizeof( test[2][2] ) );
}

void loop() {}

The only way using your current method is to use both the data arrays and 'test' for information:

byte A[]={B01111110,B10001000,B10001000,B10001000,B01111110};
byte B[]={B11111110,B10010010,B10010010,B10010010,B01101100};
byte gamma[]={B11111110,B10000000,B10000000,B10000000,B10000000};

byte *test[]={A,B,gamma};

void setup() {
  Serial.begin( 9600 );
  Serial.println( sizeof( test ) / sizeof( *test ) );
  Serial.println( sizeof( gamma ) / sizeof( *gamma ) );
  Serial.println( sizeof( *test[2] ) );
}

void loop() {}

Both codes output the result:

3
5
1

As PaulS says, if 0 can be a terminator, you can use strlen() to calculate the length. ( but then you are simply hiding a nested loop ).

i am going to make it a container probably to hold the size also cause every byte array does not the same size some have 3 bytes

  Serial.println( sizeof( *test[2] ) );

Why does this output 1? And, how, when gamma contains 5 elements, is that useful information?

it really does.and i can not explain it why it is 1.

I am going to use a container to store the size and the byte array anyway.

I have one more question plz.

I am declaring the container that in the constructor gets the byte array and the sizeand makes an object that i can use.I do not want to declare and initialize 234(not the real number) byte arrays and then 234 containers in order to do this.

byte A[]={B01111110,B10001000,B10001000,B10001000,B01111110};
Container* A=new Container(A,5);

i want to do it like this in one line

Container* A=new Container({B01111110,B10001000,B10001000,B10001000,B01111110},5)

It give error on of non matching funtion for call to 'Container::Container(,int)'

the constructor is

Container::Container(byte character[],int size) {
	this->character=character;
	this->size=size;

PaulS:

  Serial.println( sizeof( *test[2] ) );

Why does this output 1? And, how, when gamma contains 5 elements, is that useful information?

Because test[ 2 ] is the pointer to gamma, when dereferenced it references a single element of gamma.
Not really useful, unless the type of the array can change between compilations.

You can't declare an array on the fly like that. Why not use a struct to hold the array and the size. Then, the size can be determined at the time the array is declared, since it won't be a pointer.

You can then create an array of structs.

i am making a class that has the byte array and the legnth. And i will use the objects.

I wanted to not to declare all the byte arrays and then all the structs or the objects of the class.I wanted to declare the class and then make object that when i create them i pass the byte array.

and then make an array of the objects of the class

I wanted to not to declare all the byte arrays and then all the structs or the objects of the class.

How many arrays are you talking about? On which Arduino? A 328-based Arduino has only 2048 bytes of SRAM. Arrays and dynamic memory allocation will use that up in a flash.

i am talking about 50 byte arrays and the same number of container objects and some 5-6 arrays for indexing them. I am having this problem with SRAM and i am trying to deal with.Any solution?

i am talking about 50 byte arrays

How many of them?

and the same number of container objects

Different types of containers take different amounts of space. In general, a struct is smaller than a class.

and some 5-6 arrays

Of what type and how big?

I am having this problem with SRAM

What a surprise.

Any solution?

Generally, starting with realistic expectations works better.

I understand that 48 or 52 byte arrays does not make a big difference.
the arrays that i need for difference sorting and indexing have from 20-30 instances. This also does not have big difference except if i know the excatck amound of memory space each instance and array gets which i do not in order to calculate the maximum instance i can hav eand the maximum number of arrays with the exact size.

also i suppose that except the irony the suggestion of the struct instead of a class is your only proposal?

also i suppose that except the irony the suggestion of the struct instead of a class is your only proposal?

I think that at this point, you need to back up and tell us the point of the project. We've already dealt with the topic title.