Return from the sizeof() function

Hello everybody,

On another forum, I went to help a user with a question to determine how many elements a char pointer array had.

I managed to solve it (by sheer luck I think), and as I have no training in software, I could not understand the value returned by the function.

The code I'm posting is pretty simple.
I used the function : sizeof().

My difficulties to understand:
Because when using this function with the square brackets "[ ]", and any index, it always returns the value 2, and when I use it without the square brackets it returns twice the value of the number of elements.

Thank you very much in advance for your time and for your reply.

char* color[] = {"blue", "red", "green", "gray", "black", "orange"};
//----------------------------------------------------------------------
void setup() {
  Serial.begin(115200);
  delay(10);
  int elements = sizeof(color)/sizeof(color[0]);
  Serial.print("sizeof(color)    ");Serial.println(sizeof(color));
  Serial.print("sizeof(color[])  ");Serial.println(sizeof(color[0]));
  Serial.print("elements          ");Serial.println(elements);
}
//----------------------------------------------------------------------
void loop() {}

Results:

sizeof(color)    12
sizeof(color[])   2
elements          6

An array of 6 pointer occupies 12 bytes (on an 8-bit AVR).
A pointer to a char occupies 2 bytes (on an 8-bit AVR).
12/2 = 6 ---> An array with 6 element has 6 elements.

BTW, sizeof is not strictly a "function" as its value is computed at compile time.

1 Like

You mean this pattern?:
sizeof(x) / sizeof(x[0])
Maybe you forgot the "0".

?
I don't think I forgot.

Oh, it was this part I was looking at...

But who is a pointer, color[0] or color?

char* color[] = {"blue", "red", "green", "gray", "black", "orange"};

color is an array of pointers.
color[0] is a pointer to the text "blue".

Tks

I thought it was a function as when I was researching the subject of size on the web I came across this site.

https://www.it.uc3m.es/pbasanta/asng/course_notes/dynamic_memory_sizeof_en.html

Thanks for clarifying.

it's actually an operator. see sizeof operator - cppreference.com

note: you use parenthesis if you pass a type. if it's an expression, you don't need the parenthesis so you could write

  size_t elements = sizeof color / sizeof color[0];

or

  size_t elements = sizeof color / sizeof *color;

PS: as your array contains constant text, its type should match

const char* color[] = {"blue", "red", "green", "gray", "black", "orange"};

you would get some issues compiling on an ESP32 otherwise

Tks :+1:

color[0] is the pointer, color is array, you are lucky it did not decay to pointer otherwise your result would have been 2/2 = 1

PS sorry replied to wrong person

It must be a side effect of the radiation.

1 Like

Arrays in C/C++ is just a pointers to row of variables of the same type.
color is an array and, simultaneously - the pointer to its first (zero indexed) element.
color[0] is an array element and since color is an array of pointers - color[0] is itself a pointer - it point to string "blue"

1 Like

@b707
Thank you very much for your explanation.

That is an absolute lie, you should not mislead people if you do not know yourself

[quote="killzone_kid, post:16, topic:1064410]
That is an absolute lie,
[/quote]

probably strongly put :cold_face: but indeed you are correct.

@b707 - The loss of type and dimensions of an array is known as array decay. You should explore that.

intentionally because

Strictly speaking you are right, even arrays with same type elements, but different length considered as different data types, for example when used as template arguments.

But from the point of view of pointer arithmetic, the name of an array is just a pointer to its first element.
So I think that this statement is too strong:

But I agree that I wrote inaccurately and should not mislead beginners

It is not. Seriously stop it before you embarrass yourself further. It only becomes a pointer when array decays to a pointer