sizeof() function doesn´t work with a string from a function-parameter

hi!

I try to create a funktion in the Adafruit_GFX Library, that calls a modified Adafruit drawChar()-function to write a word instead of single Characters:

void Adafruit_GFX::drawSquareString(int16_t x, int16_t y, char text,
uint16_t color, uint16_t bg, uint8_t size) {

char exampletext=“SQUARE”; //example-text

for (int8_t o=0; o<sizeof(text)-1; o=o+1){

drawSquareChar(x+(o*31),100,text[o],0x0000,0xFFFF,1);

}
}

The problem, what I dont understand is the sizeof()-Function that gives me the length of the string.
If I call sizeof(text) it returns 1, whatever length the string has.
sizeof(exampletext) works fine and returns the correct number. What is the different betreen the string declared in this function to a string given with the function-call?

thanks for your help! :cold_sweat:

You want strlen(), not sizeof().

I am not sure why sizeof(text) yielded 1, I would have thought it would have yielded the size of the pointer, not the size of the char.

Arrays are passed as pointers so functions have no knowledge of the number of elements.

I take it you realise that, at no point do you do anything with exampletext?

I think he stuck it in as a test of his issue.

When passing an array to a function, if you do not specify a reference for the input parameter its really just a declaration to a pointer:

void func( char arr[] ){ void func( char arr[5] ){

Inside either of these declarations, sizeof( arr ) will be 2 ( sizeof a pointer ). To handle this you could use strlen like mentioned, or pass in a length.

To use a real array so sizeof will work inside the function you have to use a slightly different syntax.

I have written an article showing both methods here: http://arduino.land/FAQ/content/6/32/en/passing-arrays-to-functions.html

If I call sizeof(text) it returns 1, whatever length the string has.

That's really, really serious - it should return at least 2.

AWOL:
That’s really, really serious - it should return at least 2.

I’d suspect the loop only doing a single iteration has given the OP the impression it returns 1: for (int8_t o=0; o<sizeof(text)-1; o=o+1){

pYro_65: I have written an article showing both methods here: http://arduino.land/FAQ/content/6/32/en/passing-arrays-to-functions.html

That's a nice bit of documentation.

The expression:

  for (int8_t o=0; o<sizeof(text)-1; o=o+1){

should only make one pass since the sizeof(text) is a pointer and, therefore, is 2. However, he proceeds to substract 1 from it, leaving one pass through the loop.

I love this expression:

int8_t o=0;

No chance of ever reading that wrong.

the sizeof(text) is a pointer and, therefore, is 2

sp. "the sizeof(text) is a pointer and, therefore, is at least 2" (we don't know that it isn't a Due)

sp. "the sizeof(text) is a pointer and, therefore, is at least 2" (we don't know that it isn't a Due)

Well, the fact that he said it only made one pass through the loop, I'd say, yes, we can say it's a 2-byte pointer.

KenF: That's a nice bit of documentation.

Cheers, always appreciate feedback.

Thanks! strlen() makes it :)

I only found length() for arduino but it didn´t work like I expected..