strcmp being weird

So I'm a beginner with arduino, and for some reason that I cannot explain,

for(int i; i<=7; i++)
  {
    Serial.println(strcmp(topic,Topics[i]));
    if(strcmp(topic,Topics[i])==0)
    {
      return i;
    }
  }

Works but as soon as i remove the Serial.println, which was only there for debugging, it stop and the strcmp doesn't return a match.

for(int i; i<=7; i++)
  {
    //Serial.println(strcmp(topic,Topics[i]));
    if(strcmp(topic,Topics[i])==0)
    {
      return i;
    }
  }

This doesn't work

What is topic?
And what is Topic[0]?

So, how do you confirm what? With that snippet I see noting that would verify that statement.

Try making a MCVE. Only then we can help. But we probably don't even need to. Because in >50% of the cases you notice the error while doing so :wink:

Both char*, the topic is a message I receive over MQTT and the Topics[] array contains some predefined messages to compare to the sent topic, if there is a match I get the index where the topic is saved in Topics.

septillion:
So, how do you confirm what? With that snippet I see noting that would verify that statement.

Try making a MCVE. Only then we can help. But we probably don't even need to. Because in >50% of the cases you notice the error while doing so :wink:

This is the barebones code, the problem is that without the println the strcmp isn't working, and I can't view the output of the strcmp because as soon as I do, it starts to give the correct output (anyone got Schrodinger's digits?). I have half a mind to just leave the println in at this point, been up for a while trying to figure this one out lol.

hashttl:
Both char*, the topic is a message I receive over MQTT and the Topics[] array contains some predefined messages to compare to the sent topic, if there is a match I get the index where the topic is saved in Topics.

char* is not nesscesary/guaranteed a string. Perhaps it's time you post complete code.

This is the barebones code

But it isn't a

Minimal, Complete, and Verifiable example

This is the barebones code

No, that is a barebones snippet. Post ALL of your code.

What is likely happening is that the Serial.print() statement is writing to a full buffer, so it has to wait for room to be made, and, during the time it is waiting, more serial data arrives, and you actually have something valid to compare.