Warning - comparison between pointer and integer on String

J

J.Broeden

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)) && (test2 != "."))
{
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 !=".") as
an integer.
Could someone possibly explain why this is the case please ?
Thank you

Jay
 
C

Chris Dollin

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)) && (test2 != "."))
(fx:snip)

I don't understand why the compiler is seeing the (test2 !=".") as
an integer.


`test2` 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` with the pointer
(to the first element of an anonynmous char array with first
element '.' and second 0) ".".

BOOM.
 
M

Martin Ambuhl

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)) && (test2 != "."))
{
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 !=".") as
an integer.

test2 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 != '.')
since '.' _is_ a integer.
 
R

Roland Pibinger

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)) && (test2 != ".")) // see other postings
{
printf("i= %d is punct\n",i);
}
}
return 0;
}
 
J

J.Broeden

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)) && (test2 != "."))


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


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
 
R

Richard Heathfield

(e-mail address removed) said:

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top