user1313386
user1313386

Reputation: 179

Comparison between a pointer and an integer in C

I want to write each char on the screen:

int i = 0;
char str[50] = {'s', 'a', 'm', 'p','l','e'}; //only for test
while (str[i] != NULL) {
    putchar(str[i]);
    i++;
}

But my compiler says:

warning: comparison between pointer and integer.

Why?

Upvotes: 9

Views: 65600

Answers (8)

Munipratap
Munipratap

Reputation: 529

You need to compare str[i]'s value with the terminating \0 of a string, rather than NULL, which is considered a pointer. Your

while (str[i] != NULL ) {

changed to

while (str[i] != '\0') {

below:

int i = 0;
char str[50] = {'s', 'a', 'm', 'p','l','e'}; //only for test
while (str[i] != '\0') {
    putchar(str[i]);
    i++;
}

Upvotes: 2

Nitish Saxena
Nitish Saxena

Reputation: 1

Here str[i] is a character, a NULL is a pointer, so we cannot compare it logically.

Use \0 to compare with str[i] as it is defined to be a null character, so comparing a character str[i] with a null character \0 is a right method and will not throw any warning.

Upvotes: 0

Santhosh
Santhosh

Reputation: 30

Try this:

int i = 0; 
char str[50] = {'s', 'a', 'm', 'p','l','e'}; //only for test 
while (str[i] != "\0") { 
    putchar(str[i]); 
    i++; 
}

Upvotes: -3

flolo
flolo

Reputation: 15526

NULL is a pointer, and str[i] is the i-th char of the str array. char is an integer type, and as you compare them, you get the warning.

I guess you want to check for end of string, that would you do with a check for the char with the value 0 (end of string), that is '\0'.

But: this won't help you as you define it just as an array of chars and not as a string, and you didn't define the terminating 0 in the char array (you get just lucky that it is implicit there).

Upvotes: 10

Keith Thompson
Keith Thompson

Reputation: 263647

The biggest problem with that code is that, depending on your implementation, it might compile without error.

The problem, as others have said, is that NULL is intended to represent a null pointer value, not a null character value. Use '\0' to denote a null character. (Or you can use 0, which is equivalent, but '\0' expresses the intent more clearly.)

NULL is a macro that expands to an implementation-defined null pointer constant. A null pointer constant can be either an integer constant expression with the value 0, or such an expression cast to void*. Which means that NULL may be defined either as 0 or as ((void*)0) (among other variations).

Apparently your implementation defines it as something like ((void*)0), which is why you got the warning message. (It could, and IMHO should, have been treated as a fatal error).

So never try to use NULL other than as a null pointer constant -- and don't count on the compiler to warn you if you misuse it.

Upvotes: 2

Kilian Foth
Kilian Foth

Reputation: 14396

str[i] is a character. NULL is a pointer. You can't meaningfully compare those two data types (although they may or may not be implemented as the same size of integer internally). That's all the error message means.

Not that the comparison is not only type-incorrect, it also doesn't do what you probably mean. You seem to assume that a character array with an incomplete initializer would be automatically terminated with a \0 character, but that rule applies to string literals, not to character arrays you create yourself. As it is, you're invoking undefined behaviour.

Upvotes: 1

Matt
Matt

Reputation: 7160

NULL is defined as a pointer type, usually (void*)0. This means it should not be compared to characters (which are promoted to integers when used on their own). The problem is with the following: str[i] != NULL. The correct way to do this is compare it to something of the same type, in this case you are looking for the null character, '\0'.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490738

NULL should only be used in pointer contexts, but here you're comparing it to a character.

You'd normally want something like:

while (str[i] != '\0') {

[or, of course, something like puts(str); or printf("%s", str);]

Upvotes: 9

Related Questions