double dimension initialization

int [][]arr1 = {{1,0,1,1,1,0,0,1,1,0},
{1,1,1,1,1,1,1,1,1,1},
{0,1,0,1,0,1,0,1,0,1},
{1,1,1,1,1,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1}};


Hi,

I am getting an error on the above line. Not sure why. Can anyone explain.

Also,

How to initialize the array if I have used malloc.


That is,

int **arr;
int i;

arr = (int **)malloc(10*sizeof(int *));

for(i=0;i<10;i++)
{
arr[i] = (int *)malloc(10*sizeof(int));
}




After this,

how can do the group initialization.

arr = {{1,0,1,1,1,0,0,1,1,0},
{1,1,1,1,1,1,1,1,1,1},
{0,1,0,1,0,1,0,1,0,1},
{1,1,1,1,1,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1}};


Is this correct.

Thanks
db


Comments

  • [b][red]This message was edited by stober at 2006-3-11 19:46:14[/red][/b][hr]
    : int [][]arr1 = {{1,0,1,1,1,0,0,1,1,0},
    : {1,1,1,1,1,1,1,1,1,1},
    : {0,1,0,1,0,1,0,1,0,1},
    : {1,1,1,1,1,0,0,0,0,0},
    : {1,1,1,1,1,1,1,1,1,1}};
    :
    :
    : Hi,
    :
    : I am getting an error on the above line. Not sure why. Can anyone explain.
    :
    [blue]This is how to do it. The second dimension must be specified.[/blue]
    [code]
    int arr1[][10] = {{1,0,1,1,1,0,0,1,1,0},
    {1,1,1,1,1,1,1,1,1,1},
    {0,1,0,1,0,1,0,1,0,1},
    {1,1,1,1,1,0,0,0,0,0},
    {1,1,1,1,1,1,1,1,1,1}};
    [/code]
    : Also,
    :
    : How to initialize the array if I have used malloc.
    :
    :
    : That is,
    :
    : int **arr;
    : int i;
    :
    : arr = (int **)malloc(10*sizeof(int *));
    [blue]^^^ is you are writing a C program, not C++, remove the typecast.[/blue]
    :
    : for(i=0;i<10;i++)
    : {
    : arr[i] = (int *)malloc(10*sizeof(int));
    : }
    :
    :
    :
    :
    : After this,
    :
    : how can do the group initialization.
    :
    : arr = {{1,0,1,1,1,0,0,1,1,0},
    : {1,1,1,1,1,1,1,1,1,1},
    : {0,1,0,1,0,1,0,1,0,1},
    : {1,1,1,1,1,0,0,0,0,0},
    : {1,1,1,1,1,1,1,1,1,1}};
    :
    :
    : Is this correct.
    :
    [blue]The allocations are correct, but its not possible to achieve what you desire. You have to initialize (or set) the integer values one at a time, the hard way, or read the values from a data file.[/blue]

    :
    :
    :



  • why do u ask me to remove the typecast. I thought its better this way.




    : [b][red]This message was edited by stober at 2006-3-11 19:46:14[/red][/b][hr]
    : : int [][]arr1 = {{1,0,1,1,1,0,0,1,1,0},
    : : {1,1,1,1,1,1,1,1,1,1},
    : : {0,1,0,1,0,1,0,1,0,1},
    : : {1,1,1,1,1,0,0,0,0,0},
    : : {1,1,1,1,1,1,1,1,1,1}};
    : :
    : :
    : : Hi,
    : :
    : : I am getting an error on the above line. Not sure why. Can anyone explain.
    : :
    : [blue]This is how to do it. The second dimension must be specified.[/blue]
    : [code]
    : int arr1[][10] = {{1,0,1,1,1,0,0,1,1,0},
    : {1,1,1,1,1,1,1,1,1,1},
    : {0,1,0,1,0,1,0,1,0,1},
    : {1,1,1,1,1,0,0,0,0,0},
    : {1,1,1,1,1,1,1,1,1,1}};
    : [/code]
    : : Also,
    : :
    : : How to initialize the array if I have used malloc.
    : :
    : :
    : : That is,
    : :
    : : int **arr;
    : : int i;
    : :
    : : arr = (int **)malloc(10*sizeof(int *));
    : [blue]^^^ is you are writing a C program, not C++, remove the typecast.[/blue]
    : :
    : : for(i=0;i<10;i++)
    : : {
    : : arr[i] = (int *)malloc(10*sizeof(int));
    : : }
    : :
    : :
    : :
    : :
    : : After this,
    : :
    : : how can do the group initialization.
    : :
    : : arr = {{1,0,1,1,1,0,0,1,1,0},
    : : {1,1,1,1,1,1,1,1,1,1},
    : : {0,1,0,1,0,1,0,1,0,1},
    : : {1,1,1,1,1,0,0,0,0,0},
    : : {1,1,1,1,1,1,1,1,1,1}};
    : :
    : :
    : : Is this correct.
    : :
    : [blue]The allocations are correct, but its not possible to achieve what you desire. You have to initialize (or set) the integer values one at a time, the hard way, or read the values from a data file.[/blue]
    :
    : :
    : :
    : :
    :
    :
    :
    :

  • : why do u ask me to remove the typecast. I thought its better this way.
    :

    C programs to not need typecast for functions that return void.

  • i thought malloc returns the pointer to the memory created ??



    : : why do u ask me to remove the typecast. I thought its better this way.
    : :
    :
    : C programs to not need typecast for functions that return void.
    :

  • :
    : i thought malloc returns the pointer to the memory created ??
    :
    :
    [blue]It does -- that has nothing to do with typecasting the return value[/blue]
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion