Pointers, casting and various compilers

Now I take a course in the ANSI C programming language and try to run this code from the lecturer's slider:

#include<stdio.h>

int main()
{
    int a[5] = {10, 20, 30, 40, 50};
    double *p;

    for (p = (double*)a; p<(double*)(a+5); ((int*)p)++)
    {
        printf("%d",*((int*)p));
    }

    return 0;
}

Unfortunately this will not work. On MacOS, Xcode, Clang, I get the error message: "Assignment to cast is illegal, lvalue casts are not supported"and on Ubuntu gcc I get the following error:"lvalue required as increment operand"

I suspect the problem is the compiler, since we are studying ANCI C, and it has its own requirements, which can lead to other standards.

+3
source share
1 answer

relative ((int*)p)++:

(int *) p . lvalue. lvalue () . , int x = 3; x , . , y = 2*x, x . , x = 5, x .

(int *) p p int. - . lvalue, , .

++ . lvalue. (int *) p lvalue, ++.

, , , , C. (C , C, .)

(double*)a (int*)p:

C . . , , .

, . char , int , double . C C. .

double * int *, , , ( , ). . int *, double *, . , a of int, , a[0], a[1] a double, , , . , int * double * C.

C, .

C , , . , , : int * double * int *, .

double * double, . , C , . , . , .

+5
source

All Articles