Home > Software design >  C Pointer error : cannot convert 'int (*)[10]' to 'int*' in initialization
C Pointer error : cannot convert 'int (*)[10]' to 'int*' in initialization

Time:10-30

I'm doing following code.

    #include <stdio.h>
    int main()
   {
    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int *p =a;

    }

But my console shows this error.

cannot convert 'int (*)[10]' to 'int*' in initialization

I don't know why. Can anyone tell me reason & solution?

Here is the screenshot included.[https://imgur.com/a/U94vtKG]

CodePudding user response:

a is 10 int pointers. You cannot assign one int pointer to 10 int pointers.

If you want p to point to the first element of a, change:

int *p = a;

to:

int *p = &a[0]

CodePudding user response:

The error message

cannot convert 'int ()[10]' to 'int' in initialization

means that actually instead of this correct declaration

int *p = a;

you wrote

int *p = &a;

Or if in realty the array a is declared like a two-dimensional array

int a[][10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

then you need to write

int ( *p )[10] = a;

That is your presented code in the question does not corresponds to the error message.

Usually such an error occurs when a function declared for example as

void f( int *a );

is called like

f( &a );

instead of calling it like

f( a );

where a is a one-dimensional array.

  • Related