gent9
gent9

Reputation: 33

Hello, i am very new to coding in c, but i am getting an error message in c and i dont know why

#include <stdio.h>
int main(){
    int number[];
    scanf("%d", &number);
    int counter = 0;
    while (counter < number){
        printf("%d\n", counter);
        counter += 1;
    }
}

I am getting an error saying that an integer cant be compared with a pointer but im not sure how to fix it and dont understand why it doesnt work,.

type here

Upvotes: 1

Views: 44

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

This declaration of an array of incomplete type

int number[];

is invalid.

Just declare an object of the type int like

int number;

There is no any need to declare an array.

Instead of the while loop

int counter = 0;
while (counter < number){
    printf("%d\n", counter);
    counter += 1;
}

it is better to use for loop like

for ( int counter = 0; counter < number; ++counter ){
    printf("%d\n", counter);
}

because the variable counter is used only in the scope of the loop.

Upvotes: 1

Asheesh Kumar
Asheesh Kumar

Reputation: 1

yes you declare as array number this declare as integer because array access as a pointer varible . whole colde is

int number[];

replace with

int number;

whole code is:

 #include <stdio.h>
int main(){
    int number;
    scanf("%d", &number);
    int counter = 0;
    while (counter < number){
        printf("%d\n", counter);
        counter += 1;
    }
}

Upvotes: 0

Related Questions