简体   繁体   中英

What does the declaration '(int *) ptr[N]' mean in C?

I found a puzzling array when I tried to put parenthesis to emphasize the declaration of array of pointers as in (int *) ptr[N]; .

The GCC C compiler says:

error: ptr undeclared (first use in this function) .

Can anyone explain the origin of the error please?

It's very simple: The variable ptr have not been declared. And no, (int *) ptr[N]; is not a declaration, it's a typecast of an array subscript expression.

If you want an array of pointers, you should do

int *ptr[N];

It is casting th element of the array ptr to an integer pointer. 个元素转换为整数指针。

The error itself points to that ptr is never declared. You forgot or deleted my misstake a line like this:

int *ptr[123];

about the N it seems to be a constand which is normally defined eg like this:

#define N 42

Maybe you can do this

typedefine int* INT_PTR;
INT_PTR ptr[N];

I think the compiler will cast ptr[N] to type (int *), just like

    int a;
    double b;
    b = (double)a;

so the (int *)ptr[N] dosen's have left value, and u never declare ptr before. then gcc compiler will tell u ptr undeclared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM