Search code examples
carrayspointersstructdereference

getting this error only in eclipse "not a structure or union"


i'm getting this error only in eclipse. my code run without any error in other compiler
THIS IS THE ERROR I'M GETTING IN ECLIPSE

 21:20:16 **** Incremental Build of configuration Debug for project Sieve of Eratosthenes ****
Info: Internal Builder is used for build
gcc -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\Sieve of Eratosthenes.o" "..\\src\\Sieve of Eratosthenes.c" 
..\src\Sieve of Eratosthenes.c: In function 'main':
..\src\Sieve of Eratosthenes.c:16:15: error: request for member 'number' in something not a structure or union
   *(pri+(i-2)).number=i;
               ^
..\src\Sieve of Eratosthenes.c:17:15: error: request for member 'value' in something not a structure or union
   *(pri+(i-2)).value =1;
               ^
..\src\Sieve of Eratosthenes.c:20:23: error: request for member 'number' in something not a structure or union
   printf("%d",*(pri+i).number);
                       ^
..\src\Sieve of Eratosthenes.c:21:23: error: request for member 'value' in something not a structure or union
   printf("%d",*(pri+i).value);
                       ^

21:20:16 Build Failed. 4 errors, 0 warnings. (took 107ms)

i tried to run my code in "https://www.onlinegdb.com/online_c_compiler" works perfectly

#include <stdio.h>
#include <stdlib.h>

struct prime {

        int number;
        int value;
    };

int main() {
    int n;
    printf("enter range");
    scanf("%d",&n);
    struct prime pri[n-2];
    for(int i=2;i<=n;i++){
        pri[i-2].number=i;
        pri[i-2].value =1;
    }
    for(int i=0 ;i<=n-2;i++){
        printf("%d ",pri[i].number);
    }

}

Solution

  • The issue you are facing is not related to Eclipse, but to the fact that the code you use on the different platforms seem to be different.

    *(pri+i).number
    

    and

    pri[i].number
    

    are not the same.

    The . has higher precedence, so it binds tighter then the *.

    To make the 1st version work do:

    (*(pri+i)).number