A general coding question for the 'Experts' - I'm not one of them!

I cannot find anything (that makes sense to me) about the error I receive.



char s[60];  //For text

//Read string from Windows Form app.
serial.scanf("%s", &s);

int length = strlen(s);

//Remove the first three characters from the string received
string s2 = string(s).substr(3, length - 3);  //Something wrong with substr()?

//Test results are what I expected
serial.printf("s:  %s   Length: %d \r\n", s, len); //OK
serial.printf("part: %s \r\n", s2); //OK


I received the error when using substr()

I do not mean that Something is wrong with substr()… Only on how I am using it…

Warning: Non-POD class type passed through ellipsis in “main.cpp”, Line: 145, Col: 45

What in the code above is the non C compatible type?

The app compiles and runs OK.

Thanks!

My guess would be that its talking about the string type. I think that’s more of a C++ class. However, if it compiles & runs OK then you can probably just ignore it. Alternatively, try using char arrays instead.

1 Like

@ ianlee74 -

I was using a char array initially(char read[10]:wink: but I was trying to find a way to get part of a string without counting through the array and finding it’s length etc.

@ willgeorge - Looking at the code you posted I am guessing that it is actually the following line that is resulting in the warning


serial.printf("part: %s \r\n", s2); 

You are passing a non primitive type (POD) to a function taking variable arguments. You should only pass primitive types like int, double short etc. to these types of functions.

While this might work now, it might not work with later library updates, as this would be dependent on the memory layout of the object.

You can use c_str() function to get around the warning and the potential issue it might cause. The c_str() function gives you a pointer to the internal char array of the string class.


serial.printf("part: %s \r\n", s2.c_str()); 

3 Likes

@ taylorza -

Thank you…

I was looking at the c_str() function but I did not understand how to use it…

Now I do! Thanks again.

@ willgeorge - It is a pleasure!

A couple of nitpicking points.

The above would work fine but since this is for something that is very tight on memory why use the c++ string class at all?
If all you want to do is drop the first 3 characters then create a pointer to the 4th character of the string. It avoids making a copy of the whole string making it faster as well as needing a lot less memory.


char s[60];  //For text

char *s2;

//Read string from Windows Form app.
serial.scanf("%s", &s);

int length = strlen(s);

//Remove the first three characters from the string received
if (length > 3)
  s2 = s+3; // s2 starts 3 characters after s
else // or if less that 3 long point to the end of the string.
  s2 = s+length;

//Test results are what I expected
serial.printf("s:  %s   Length: %d \r\n", s, len); //OK
serial.printf("part: %s \r\n", s2); //OK

Also serial.scanf("%60s", &s); would be safer unless your windows form app is enforcing maximum lengths on the strings it passes to the mbed.
c doesn’t perform any string length checks as standard, without that change any string over 59 characters would cause memory corruption.

1 Like