-3

I'm trying to fill an array with integers 0-100.

Here's my code:

int main()
{   
    int a[100];
    for(int i = 0; i < a; i++)
    {
        a[i] = i;
        printf("\n%d\n", a[i]);
    }
}

I get this error:

comparison between pointer and integer (int and int) over the for line.

I can't figure it out. Any thoughts?

Azeem
  • 11,148
  • 4
  • 27
  • 40
Joakim
  • 155
  • 11
  • 1
    Why would you vote here to close this question as "typographical error where the problem does not persist anymore" ? I would understand if this might be a duplicate of [iterate-through-a-c-array](https://stackoverflow.com/questions/1597830/iterate-through-a-c-array) But it is clearly that OP does not know the syntactic and semantic mechanism of looping through an array. If this is to be closed with the reasoning of a typographical error, then we can close a lot of questions that are in reality syntactic of semantic problems in nature – Mong Zhu Jan 22 '18 at 07:57
  • 1
    "a problem that can no longer be reproduced" what is here not reproducible? I really don't get this. IMHO this is simply wrong. The post is event in itself **the shortest program necessary to reproduce the problem"" – Mong Zhu Jan 22 '18 at 07:59
  • 1
    There is no canned close-reason for 'OP does not know the syntactic and semantic mechanism of looping through an array'. Using such a custom close-reason, accurate though it may be, is likely to get flagged as 'rude and/or abusive' and get the voter suspended. – Martin James Jan 22 '18 at 07:59
  • @MartinJames Isn't this is a very usual format of a question. Someone is trying to learn, programmed a loop, made a mistake and is now asking for help. The question includes the code, the error message a minimal example as to reproduce the problem, which can clearly be reproduced here! I agree that there is a great lack of research in it, OP could have found the duplicate if he'd tried. But still im IMHO this seems a valid question and post to me. – Mong Zhu Jan 22 '18 at 08:05

3 Answers3

1

Yes you are comparing pointer with an integer. This is why the error.

for(size_t i=0; i<sizeof(a)/sizeof(a[0]); i++) {

is what you wanted it to be and can be done in C language. Remember that sizeof operator results in value in size_t.

If you are not aware what that sizeof is doing - it is basically total number of bytes that the array object has - divided by each element's size - resulting in number of elements.

In case you are thinking from where did that pointer come?

Note one thing here a is converted into pointer (array decaying)to the first element - so it is nothing other than a pointer to the first element.

Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56
  • Sorry for the accidental insertion, it was meant to be a fix for accidental deletion in a different answer. I misclicked and you were faster with the rollback. – Yunnosch Jan 22 '18 at 07:57
1

The cancel condition in your for loop is wrong. i<a You are comparing an int i variable with the pointer a that points to the memory location of the array. You would need to calculate the length of the array:

for(int i=0; i<sizeof(a)/sizeof(int); i++) {

But you could have found this solution in this 9 year old answer

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • Sorry for the accidental deletion. On the other hand, if you know a duplicate then please indicate so with a close vote. – Yunnosch Jan 22 '18 at 07:55
  • @Yunnosch no worries, I actually just doing this. Had to write this very long comment before ;) – Mong Zhu Jan 22 '18 at 07:57
0

This should work for you,

    int main()
{   
    int a[100];
    int n = sizeof(a) / sizeof(a[0]); //Get the size of the array
    for(int i=0; i<n; i++) { // loop through each elements of the array
        a[i] = i;
        printf("\n%d\n", a[i]);
    }
}
imanshu15
  • 734
  • 3
  • 21