How do you make this "Sizeof" work?

Arduino 101 executiing -- Got an error.

char myStr[] = "this is a test";
int i;

void setup(){
  Serial.begin(9600);
}

void loop() { 
  for (i = 0; i < sizeof(myStr) - 1; i++){
    Serial.print(i, DEC);
    Serial.print(" = ");
    Serial.write(myStr[i]);
    Serial.println();
  }
  delay(5000); // slow down the program
}

Hi PoleMac,
I believe your code doesn't reflect your intentions :slight_smile:
sizeof(myStr) reports the size of myStr, which is a pointer indeed.
What you want to do is calling strlen(myStr), which returns the actual size of the string.

I want to do something like this :
char myStr[] = {"this is a test", "testing 123", "working"};
This code only works regular Arduino & WeMos D1.

I've wasted one year not making this code work on 101.
Back and forth.... WeMos D1, brought another Arduinos.

I want to go back on abandon 101. Intel (Made in USA) brand.
How do you make it work on Strlen?


warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < strlen(myStr); i++){
^

Sketch uses 17,872 bytes (11%) of program storage space. Maximum is 155,648 bytes.

How do you make it work on Strlen?

The compiler is telling you how to fix it:

warning: comparison between signed and unsigned integer expressions

strlen returns unsigned. your counter i is signed. This will probably still work, but if you want to get rid of the warning, just use "unsigned int" for your loop counter.

I want to do something like this :
char myStr[] = {"this is a test", "testing 123", "working"};
This code only works regular Arduino & WeMos D1.

I don't think this has anything to with Arduino101, since this line is just incorrect C++. Try this instead;

const char *myStr[] = {"this is a test", "testing 123", "working"};