Solve my Error

I got this error [Error] ISO C++ forbids comparison between pointer and integer [-fpermissive] #include <stdio.h>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  int main()
{
    int i[1];
    
    int r = 4;
    
    {
        printf("enter a number between 1-10\n");
        while (i != r);
        {
        scanf("%d,&i[0]");
        }
        printf("good job\n :)");
    }
}


https://www.njmcdirect.tips/
Last edited on
What are you trying to do?
Rachel69 wrote:
while (i != r);

Because of the semicolon immediately after the while condition, this loop does nothing!

The code in the curly braces (lines 10 to 12) is executed exactly once; it totally ignores the while in line 9.

Furthermore: i is an array of integers (of length 1), whereas r is a single integer. Obviously, it doesn't make much sense to compare an array to an integer. Actually, in the C programming language, an array is a pointer to the first element. That is why you get the compiler error about comparing a pointer to an integer.


Rachel69 wrote:
scanf("%d,&i[0]");

This use of scanf() function is wrong too. You probably want:
scanf("%d", &i[0]);


But why i needs to be an array at all? Why not just do something like this, to read a single integer:
1
2
int myInt;
scanf("%d", &myInt);
Last edited on
Even in C, there is distinct type for array (it is not the same as a pointer type).

However,
Any lvalue expression of array type, when used in any context other than
. as the operand of the address-of operator
. as the operand of sizeof
. as the string literal used for array initialization
. as the operand of _Alignof (since C11)
undergoes an implicit conversion to the pointer to its first element.
https://en.cppreference.com/w/c/language/array#Array_to_pointer_conversion
The whole thing looks like a tutor assigned "fix the errors in this code" assignment.

Topic archived. No new replies allowed.