Link to home
Create AccountLog in
Avatar of zizi21
zizi21

asked on

Urgent : Array

Hi,

This is really strange but this is what i got
test.c:36: warning: excess elements in scalar initializer
test.c:36: warning: (near initialization for 'arr[3]')

i am trying to initialize a list of arr like this

int *arr[10]={{1,2},{10,11,12},{2,3,1}};

thanks a lot.


Avatar of Infinity08
Infinity08
Flag of Belgium image

Did you intend to make a 2D array ? If so, the way you initialized it won't work (it only works for static arrays, not for dynamic arrays).
So, in other words, it would work like this :

        int arr[10][3] = {{1,2},{10,11,12},{2,3,1}};

Or, when using your array declaration :

        int *arr[10];
        int i = 0;
        arr[0] = (int*) calloc(3, sizeof(int));
        arr[0][0] = 1;
        arr[0][1] = 2;
        /* etc. */

Don't forget to free again once the array is no longer needed.
Avatar of zizi21
zizi21

ASKER

it works find with  2 d array a but my problem is if i use 2 d array

i wouldn't know the size of the column

for example for  int arr[10][3] = {{1,2},{10,11,12},{2,3,1}};

the size of the column could be 2 or 3, if the size of the column is 2, zero would be printed..

is there a way to know the size of the column..

thats why,then , i thought better use pointers...




ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of zizi21

ASKER

Thanks