Go Down

Topic: what does this code mean? (Read 2754 times) previous topic - next topic

Operation BC

Jan 14, 2010, 01:10 pm Last Edit: Jan 14, 2010, 01:38 pm by Spiros Reason: 1
I found a piece of code that works and I am trying partially to modify it. What does the[glow] (char* This)[/glow] at the below function declaration mean?

char Comp(char* This)

Also what[glow] strcmp[/glow] mean?


Code: [Select]
char inData[1000];  // Allocate some space for the string
char inChar;   // Where to store the character read
byte index = 0;   // Index into array; where to store the character
int ledPin = 13;  // Set the pin to digital I/O 4

void setup(){
 pinMode(ledPin, OUTPUT);

char Comp(char* This){

 while(Serial.available() > 0)
   if(index < 999) // One less than the size of the array
     inChar = Serial.read(); // Read a character
       digitalWrite(ledPin, HIGH);
       //digitalWrite(ledPin, LOW);
     inData[index] = inChar; // Store it
     index++; // Increment where to write next
     inData[index] = '\0'; // Null terminate the string

 if(strcmp(inData,This)  == 0){
   for(int i=0;i<100;i++){



void loop()
   digitalWrite(ledPin, HIGH); // turn the LED on
   digitalWrite(ledPin, LOW); // turn the LED on
delay(100); // Wait 100 milliseconds for next reading



It defines "This" to be a pointer to a "char" type, so it is a good bet that "This" points to a string.
Some people are like Slinkies.

Not really good for anything, but they bring a smile to your face when pushed down the stairs.


First, a request. Please modify this post. Select all your code, then press the # button above. This embeds your code between tags that the forum uses to create a scrollable frame to display code in.

The strcmp function compares two strings. It returns -1, 0, or +1 depending on the order that the two strings would have in the dictionary. The return code of 0 means that they are the same.

The "char Comp(char *This)" line declares a function that returns a char, and takes a pointer to a char as input.

It has several problems. Comp actually returns an int, which the compiler then has to cast to a char. In C++, "this" is a reserved keyword. Changing the spelling to "This" means that the code has two variables with nearly the same name, but completely different meanings and purposes.

In any case, "This" is a pointer to an array of chars, commonly referred to as a string. The strcmp function is used to compare that string to the string composed of characters read from the serial port.
The art of getting good answers lies in asking good questions.

Operation BC

Thanks a lot for the quick and great answers!!!!


Thanks for the info in this topic! I'm working with almost the same code, and have another question about it.

I'm trying to get the string which is being send by processing to the serialport in a variable.
In the case of the example above, the programmer knows what kind of string is being passed, so he can use the if-statement. In my case the string which is being passed is like "X100" or "Y-345". I know the first letter is going to be an X or Y, but the numbers behind it will be different all the time.

Is there a way I can get this out of the example above?
Or are there other (maybe easier) ways to pass a string from processing to arduino?



The topic that you are asking about is parsing. Successful parsing requires a well defined input string, which you don't have.

When reading "X100" or "Y-345" from the serial port, how do you know when you have a complete packet? If, instead, you sent "<X100>" or "Y-345>", the start and end of packet markers (< and > respectively) clearly define when a packet has been received.

Search the forum for "started && ended" for an example of how to read packets from the serial port.

When you have a complete packet (in inData, for instance), you can copy the first character:
Code: [Select]
char one = inData[0];

Then, replace the first character with a space:
Code: [Select]
inData[0] = ' ';

Then, you can convert inData to an integer:
Code: [Select]
int inVal = atoi(inData);

Then, between inVal and one, you know which direction to move, and how far.
The art of getting good answers lies in asking good questions.


Thanks a lot for the fast reply! This brings me a lot further!

Go Up