I have the following:
int findPar(char* str)
{
int counter=0;
while (*str)
{
if(str[0] == "(") <---- Warning
{
counter++;
}
else if (str[0]== ")") <---- Warning
{
counter--;
}
if (counter<0)
{
return 0;
}
str++;
}
if (counter!=0)
{
return 0;
}
return 1;
}
The warning i get is comparison between an int and a char.
I tried to do the comparison (first char in the string vs. given char) also with strcmp like this:
if (strcmp(str, ")")==0) { stuff }
but it never goes in to 'stuff' even when the comparison (should) be correct.
how should i do it?
")" == )\0
literally, whereas')' == )
. – Sailorstrcmp( str, ')' ) ==0)
does not work because your are comparing the whole string char* against a single literal character. It would work if you didstrcmp( str[0], ')') == 0
I think – Ruthanneruthestrcmp
expects it parameters to be string. ')', a char of value41
will be converted to(char*)41
. So the function will look into the memory address41
, hoping to see a string and instead - BAM! Segfault. – Eyeshade