Hot answers tagged

10

You want strlen. char foo [100] = "bar"; Serial.println (strlen (foo)); // --> prints 3


6

The problem is in your C++ program at the other end of the line (not the Arduino side): char* c = ""; int len = strlen(c); while(c == "") { serial.ReadData(c, len); } Here, len is zero because that is the length of "" , the empty string! Thus, in your loop, you ask ReadData to read 0 bytes. Hence, the condition of your while loop is always true as, c ...


6

Not sure it has a bearing on your problem or not, but I can't help highlight this: wait: while((tmp = serialReceive()) != 2){ goto wait; } That has to be the the most evil piece of code ever written in the history of mankind. I'm sorry, but it is. while((tmp = serialReceive()) != 2); is all you need. Or if you must be "pedantic": while((...


3

I know you have your answer from @NickGammon, but I'd just like to add a bit more in-depth information about how all this works. sizeof() is not a function in the normal sense of the word. It is an operator which gives you the number of bytes of memory allocated to whatever you pass to it. If you pass it an array then it returns the number of bytes that ...


3

The problem is you send a character of type char, thus exactly one byte. However, when you send it you have to give the length and you use strlen. However, msg is an array (at least that is what you want), but that array contains only the character to sent. What you want is to send only 1 character, so use 1 instead of strlen. Also, you can get rid of the ...


3

One obvious problem with the code is that s is not 0-terminated. Taking strlen of such string techically is an Undefined Behaviour, and all bets are off. I can only speculate that it does return something larger than the max value an integer may hold (keep in mind that it returns size_t). It means that the loop for(int i=0; i<strlen(s); i++){ ...


3

You malloc but don't free. Of course it's going to leak. Also sizeof(text) doesn't work like that. Instead you can do strlen to get the number of characters in a null-terminated char array. You can do this without the longText buffer by doing partial copies into screen. const char space[16] = " "; char screen[16]; for (int i = 0; i < ...


2

You can't simply concatenate C strings like that. Instead you have to build them up into a new string. For formatting like that the simplest way is with sprintf: char outMessage[strlen(outTopic) + strlen(msg) + 3]; // Room for topic, message, [ and ] and terminating \0 sprintf(outMessage, "[%s]%s", outTopic, msg); client.publish(outTopic, outMessage); ...


2

Probably the best way would be to define below it const int eye_len = sizeof(eye)/sizeof(eye[0]); If the linker does link time optimization, or if you only use it within the same file (and declare it static), it shouldn’t cost any memory, otherwise it will take up two bytes of ram.


2

The function I think you are looking for is strlen - STRing LENgth. It counts the number of characters in a string up until it finds the "NULL" end of string marker. Serial.println(strlen(t));


1

strcat is a function to concatenate strings. It expects a char array as second parameter. You have only a single char newChar to add to your string. size_t l = strlen(inputs[i]); inputs[i][l] = newChar; inputs[i][l + 1] = 0; Note: It would be better to track the length of the inputs then use strlen. strlen iterates over the array until terminating ...


1

char* c = ""; while(c == "") The variable named c is a misnomer, because it is a pointer to a string, not a character. For example c = 0x123456. You should use a more explicit name for your variable. This first line assigns a pointer to an empty string to the c pointer. So, the value stored at address 0x123456 == whatever pointer to a static memory ...


Only top voted, non community-wiki answers of a minimum length are eligible