Nox997
Nox997

Reputation: 71

What is meant by this cast? (int (*)[10])

I stumbled across the following cast:

(int (*)[10])

In my opinion, this is a "pointer to pointer to int". Let's assume the following array declaration:

int array[10];

Then I would assume that &array is of type (int (*)[10])

Is this correct?

Upvotes: 3

Views: 375

Answers (4)

venkatesh kambakana
venkatesh kambakana

Reputation: 111

its a pointer to an array of 10 integers.

Upvotes: 0

Eugeniu Rosca
Eugeniu Rosca

Reputation: 5305

Here's an educational example which is aligned to your observations (compiled with no warnings):

int array[10]; /* an array of 10 ints */
int (*ptr)[10] = &array; /* a pointer to an array of 10 ints */

/* a function which receives a pointer to an array of 10 ints as param */
int myfunc (int (*param)[10])
{
    return 10;
}

int main(void)
{
    /* a pointer to a function that accepts a pointer to an array of 10 ints as parameter and returns an int */
    int (*func)(int (*)[10]);

    func = myfunc; /* no cast needed. The objects are of the same type */
    /* func = (int (*)(int (*)[10])) myfunc; */ /* with cast */

    (*func)(ptr);  /* function pointer called with ptr parameter */
    return 0;
}

Upvotes: 2

haccks
haccks

Reputation: 106012

In my opinion, this is a "pointer to pointer to int".

No. Its pointer to an array of 10 ints.

Then I would assume that &array is of type (int (*)[10])

Yes. Your assumption is correct. &array is the address of array array and is of type int (*)[10].

Upvotes: 4

Sourav Kanta
Sourav Kanta

Reputation: 2757

It is a pointer to an integer array of size 10 and not a 'pointer to a pointer of int'.

For example when you pass the type

 char arr[20][10]

to a function it decays to type char(*)[10]

As the compiler has to know the no of columns to effectively convert a 2d array to linear allocation in memory it is not the same as type int **.

Upvotes: 6

Related Questions