Sizeof with char*

I’m using sizeof and strlen. With the char* strings I get back the right number from strlen, but it always kicks back “2” from sizeof.
Doing the same with a string - that doesn’t use char* - I get the right numbers back (from sizeof and strlen).

char *testStrings[5] = {"abcd", "abcde", "abcdef", "abcdefg", "abcdefgh"};
int numero;

const char fixedstr[] = "tenletters";

void setup() 

void loop() 
  Serial.print("strlen tS_[0] = ");
  numero = strlen(testStrings[0]);
  Serial.print("sizeof tS[0] = ");
  numero = sizeof(testStrings[0]);

  Serial.print("strlen tS[4] = ");
  numero = strlen(testStrings[4]);
  Serial.print("sizeof tS[4] = ");
  numero = sizeof(testStrings[4]);

  Serial.print("strlen FC = ");
  numero = strlen(fixedstr);
  Serial.print("sizeof FC = ");
  numero = sizeof(fixedstr);


The size of a pointer is 2 (bytes) and an 8-bit AVR processor.

You’re saying that it’s kicking back the size of the pointer (not the number of characters in the string)?

Correct. You asked for sizeof() a pointer variable and that’s what it gave you.

If you want the number of characters in a C string, use ‘strlen(char *pointer)’. It counts bytes until it reaches a zero byte.

If the pointer points to an array that is NOT a C string (does NOT have a null character terminator) then you have to pass the size separately. Pointers are just an address.

sizeof() is evaluated at compile time, not runtime.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.