Problems with pointers and strstr()...

Hello,
I am trying to extract part of text in an array of char.
Below, I try to extract : "123.145.789.123", but the second step already does not work.
I am also confused between pointers, arrays, etc.
If anyone wants to explain to me, I'm interested.
Thank you.

  char t1[50];
  char t2[10];
  char* t3;

  snprintf(t1, sizeof(t1), "+CDNSGIP: 1,\"www.truc.com\",\"123.145.789.123\"");
  snprintf(t2, sizeof(t2), ",");
  t3 = strstr(t1, t2);
  Serial.print(t3);            // j'obtiens biens : ,"www.truc.com","123.145.789.123"
  t3 = strstr(t3, t2);
  Serial.println(t3);          // j'obtiens toujours : ,"www.truc.com","123.145.789.123"

What does "Does Not Work" mean?

How does the code as written behave?

How do you want it to behave differently?

Hi gfvalvo,

Thanks for your reply.

The first step works correctly regarding strstr() :

snprintf(t1, sizeof(t1), "+CDNSGIP: 1,\"www.truc.com\",\"123.145.789.123\"");
  snprintf(t2, sizeof(t2), ",");
  t3 = strstr(t1, t2);
  Serial.print(t3);            // I get : ,"www.truc.com","123.145.789.123"

But if continue with strstr() on t3, I expect to get : ,"123.145.789.123".

t3 = strstr(t3, t2);
  Serial.println(t3);          // I still get : ,"www.truc.com","123.145.789.123" and not ,"123.145.789.123"

But I get the same return as the first step : ,"www.truc.com","123.145.789.123"...

What I want is to extract only the IP address.

Thanks

You will have to increment t3 before the second step. After the first step, that first char is a comma. Next you use strstr and what will it find? The first character.

 t3 = strstr(t1, t2);
  Serial.print(t3);            // j'obtiens biens : ,"www.truc.com","123.145.789.123"

  // increment t3 so it points to the next character
  t3++;
  t3 = strstr(t3, t2);
  Serial.println(t3);

If you're only looking for a single character, use strchr(). E.g.

 t3 = strchr(t1, ',');
  Serial.print(t3);            // j'obtiens biens : ,"www.truc.com","123.145.789.123"

  // increment t3 so it points to the net character
  t3++;
  t3 = strchr(t3, ',');
  Serial.println(t3);

There is also the function strtok() which is usually used for parsing; it however is destructive in the sense that it destroys the input string.

// Note
t3++ increments the pointer, not the character that it points at.

For the second call to strstr(), try:

t3 = strstr(t3+1, t2);

Untested.

Thanks gfvalvo, sterretje,

It works perfectly now !

Bruno.