Home > Back-end >  Hello, i am very new to coding in c, but i am getting an error message in c and i dont know why
Hello, i am very new to coding in c, but i am getting an error message in c and i dont know why

Time:11-15

#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

CodePudding user response:

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.

CodePudding user response:

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;
    }
}
  • Related