Working with pointers

The compiler gives an error with this code. It does not like the (req == “/”) comparison.
Please tell me how to fix it.
```
*char HTTP_req[REQ_BUF_SZ] = {0};
int requestType = 0;

requestType = findRequestTypet(HTTP_req, REQ_BUF_SZ);

//---------------------------------------------------------
int findRequestType (char* req, char length)
{

Serial.println(req[0]);

for (int i = 0; i < length; i++)
{
	// find first "/" in req      
	if (req[i] == "/")
	{
		if ((req[i+1] == int("c")) && (req[i+2] == int("-")))
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
}

}*
```

if (req[i] == '/')

Hands off the Shift key. Single quotes ( ' ) for individual characters like this, don't cast the strings to ints. You need to fix more than just that line.

Because "/" is a pointer to a two element char array consisting of '/' and '\0 '. This array is stuffed in memory someplace.

If you want a string comparison use strcmp(). If you want a character comparison use '/'

Jiggy-Ninja:
Hands off the Shift key. Single quotes ( ' ) for individual characters like this, don't cast the strings to ints. You need to fix more than just that line.

Got it. Thanks to all.

I think I may still be doing something wrong.
I am gettinga requestType of 3 when I should be getting 0.

Revised code:

char HTTP_req[REQ_BUF_SZ] = {0};
int requestType = 0;

requestType = findRequestTypet(HTTP_req, REQ_BUF_SZ);

//---------------------------------------------------------
int findRequestType (char* req, char length)
{
 
	Serial.println(req[0]);
 
	for (int i = 0; i < length; i++)
	{
		// find first "/" in req      
		if (req[i] == '/')
		{
			if ((req[i+1] == 'c') && (req[i+2] == '-'))
			{
				//TODO: make sure we have all numbers
            			char commandNumber[5];
            			commandNumber[0] = req[i+3];
            			commandNumber[1] = req[i+4];
            			commandNumber[2] = req[+i+5];
            			commandNumber[3] = req[i+6];
            			//commandNumber[4] = "0";
            			int b = atoi(commandNumber);
            			return b;
			}
			else
			{
				return 0;
			}
		}
	}
}

You need to nul terminate commandNumber. The last character needs to be '\0' - and not '0' which is ascii zero.

The serial monitor does not show me the slash when it should so I guess it is not getting there at all.

if (req[i] == '/')
		{
Serial.print ("Character:  ");
    Serial.println(HTTP_req[i]);
}

You're not printing the thing you're comparing, it seems to me

if (HTTP_req [ i ] == '/' )

--Michael
edit: oops. forgot about the italics thing.

AWOL:
You're not printing the thing you're comparing, it seems to me

I added the serial printout just to test if it is getting inside the if statement which it should but is not because there is no serial monitor output.

I'd love to spend longer on this with you, but if you can't be bothered to post anything but uncompilable snippets, it's just a waste of time.

I'll try once more. You've declared your array

HTTP_req[i]

not

req[i]

best,
Michael

mjward:
if (HTTP_req [ i ] == '/' )

--Michael
edit: oops. forgot about the italics thing.

I fixed it. the serial statement should be

Serial.println (req[i]);

Oh. I see now that you're passing a pointer to the array.
Is there a typo in this line? requestType = findRequestTypet(HTTP_req, REQ_BUF_SZ);
(that last lower case t at the end of findRequestTypet)
--Michael

mjward:
Oh. I see now that you’re passing a pointer to the array.
Is there a typo in this line? requestType = findRequestTypet(HTTP_req, REQ_BUF_SZ);
(that last lower case t at the end of findRequestTypet)
–Michael

Yes, thanks. Good catch. There is a typo there because I entered it manually to shorten up the code for here.
It is OK in my program.

And yes, I am using req because it is in the scope of the procedure although I could use HTTP_req because it is global.

I now know what the problem is.

It is the length passed to the procedure.

Please suggest a change.

Use strlen() to calculate the length in the function.

KeithRB:
Use strlen() to calculate the length in the function.

Thank you all so much.