Quiescent
Quiescent

Reputation: 1144

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?

Upvotes: 1

Views: 956

Answers (4)

BlackMamba
BlackMamba

Reputation: 10254

Maybe you can do this

typedefine int* INT_PTR;
INT_PTR ptr[N];

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409176

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];

Upvotes: 3

sad1e
sad1e

Reputation: 124

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.

Upvotes: 1

rekire
rekire

Reputation: 47945

It is casting Nth 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 e.g. like this:

#define N 42

Upvotes: 2

Related Questions