#### Howdy, Stranger!

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

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# double dimension initialization

Posts: 26Member
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

• Posts: 9,765Member ✭✭✭
[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]

:
:
:

• Posts: 26Member
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]
:
: :
: :
: :
:
:
:
:

• Posts: 9,765Member ✭✭✭
: 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.
• Posts: 26Member

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.
:

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