By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

Warning - comparison between pointer and integer on String

P: n/a
Hi, I've written a some code to assist in my understanding of strings
using some of Cs built in character handling functions but I am not
sure why I'm getting the following error.
I hope someone can explain the errors of my ways.

/* Looking for special chars in testlist */
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main ()
{
int i;
char test2[10] = "ab c1% 4!.";

for (i=0; i <= 10; i++)
{
if ((ispunct((int) test2[i])) && (test2[i] != "."))
{
printf("i= %d is punct\n",i);
}
}
return 0;
}

When I use gcc -Wall -Werror to compile I get the following warning:
searchstring_ispunct.c: In function `main':
searchstring_ispunct.c:14: warning: comparison between pointer and
integer

I don't understand why the compiler is seeing the (test2[i] !=".") as
an integer.
Could someone possibly explain why this is the case please ?
Thank you

Jay

May 26 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
J.*******@gmail.com wrote:
Hi, I've written a some code to assist in my understanding of strings
using some of Cs built in character handling functions but I am not
sure why I'm getting the following error.
I hope someone can explain the errors of my ways.
(fx:snip)
{
int i;
char test2[10] = "ab c1% 4!.";

for (i=0; i <= 10; i++)
{
if ((ispunct((int) test2[i])) && (test2[i] != "."))
(fx:snip)
I don't understand why the compiler is seeing the (test2[i] !=".") as
an integer.
`test2[i]` has type `char`, which promotes to `int` in a relational
expression. `"."` is a /string/, not a character - it's a literal
with type `char[2]`, which decays into pointer-to-char.

Your comparing the integer (value of) `test2[i]` with the pointer
(to the first element of an anonynmous char array with first
element '.' and second 0) ".".

BOOM.

--
Took Me Ages To See It! Hedgehog
"The path to the web becomes deeper and wider" - October Project

May 26 '07 #2

P: n/a
J.*******@gmail.com wrote:
Hi, I've written a some code to assist in my understanding of strings
using some of Cs built in character handling functions but I am not
sure why I'm getting the following error.
I hope someone can explain the errors of my ways.

/* Looking for special chars in testlist */
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main ()
{
int i;
char test2[10] = "ab c1% 4!.";

for (i=0; i <= 10; i++)
{
if ((ispunct((int) test2[i])) && (test2[i] != "."))
{
printf("i= %d is punct\n",i);
}
}
return 0;
}

When I use gcc -Wall -Werror to compile I get the following warning:
searchstring_ispunct.c: In function `main':
searchstring_ispunct.c:14: warning: comparison between pointer and
integer

I don't understand why the compiler is seeing the (test2[i] !=".") as
an integer.
test2[i] is a char, A char is an integer.
"." is a string literal. It is not an integer. You are comparing
a pointer (the address of the string literal) to an integer.
You wanted
(test2[i] != '.')
since '.' _is_ a integer.
May 26 '07 #3

P: n/a
On 26 May 2007 01:26:13 -0700, J.*******@gmail.com wrote:
>Hi, I've written a some code to assist in my understanding of strings
using some of Cs built in character handling functions but I am not
sure why I'm getting the following error.
I hope someone can explain the errors of my ways.
It's important to note that C has no string type. The string.h
functions assume '\0' terminated arrays of char.
>/* Looking for special chars in testlist */
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main ()
{
int i;
char test2[10] = "ab c1% 4!.";
too short, your string literal actually is "ab c1% 4!.\0"
char test2[11] = "ab c1% 4!.";
better:
char test2[] = "ab c1% 4!.";
for (i=0; i <= 10; i++)
the test2 index runs from 0 - 10, test2[10] == '\0', therfore
for (i=0; i < 10; i++)
prefer sizeof (for arrays) or strlen (for char*) to hardcoded numbers.
{
if ((ispunct((int) test2[i])) && (test2[i] != "."))
// see other postings
{
printf("i= %d is punct\n",i);
}
}
return 0;
}

--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
May 26 '07 #4

P: n/a
On May 26, 8:51 pm, rpbg...@yahoo.com (Roland Pibinger) wrote:
On 26 May 2007 01:26:13 -0700, J.Broe...@gmail.com wrote:
Hi, I've written a some code to assist in my understanding of strings
using some of Cs built in character handling functions but I am not
sure why I'm getting the following error.
I hope someone can explain the errors of my ways.

It's important to note that C has no string type. The string.h
functions assume '\0' terminated arrays of char.
/* Looking for special chars in testlist */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main ()
{
int i;
char test2[10] = "ab c1% 4!.";

too short, your string literal actually is "ab c1% 4!.\0"
char test2[11] = "ab c1% 4!.";
better:
char test2[] = "ab c1% 4!.";
for (i=0; i <= 10; i++)

the test2 index runs from 0 - 10, test2[10] == '\0', therfore
for (i=0; i < 10; i++)
prefer sizeof (for arrays) or strlen (for char*) to hardcoded numbers. {
if ((ispunct((int) test2[i])) && (test2[i] != "."))

// see other postings
{
printf("i= %d is punct\n",i);
}
}
return 0;
}

--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Ahhh I see. " " are for strings and ' ' are for chars.
I was curious as I can't see the end of array what would be at the end
but you've answered my question.
However does that mean in a multi lined text file like this:
abced
12345

would look like:

abcde\0\n
12345\0\n

Is this right ?

Jay

May 26 '07 #5

P: n/a
J.*******@gmail.com said:

<snip>
However does that mean in a multi lined text file like this:
abced
12345

would look like:

abcde\0\n
12345\0\n

Is this right ?
No. Text files do not contain null characters (for any reasonable
definition of "text file"). When you read a line from a file, you know
you've reached the end of the line when you hit the newline character.
If you're reading data from a text file into an array that you wish to
treat as a string, it's your job to bang a null terminator on the end
(or use a function that does it for you, such as fgets).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 26 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.