Dynamic allocation of multi dimensional array

Status
Not open for further replies.

Mohan Giri

In Runtime
Messages
144
Hi everyone,
could you tell me how to assign values to dynamically created multi dimensional array???

I tried this code. I am getting error : Invalid direction



#include<stdio.h>
#include<conio.h>
main()
{
int *a;
int i,j,k;
int l,m,n;
//clrscr();
printf("Enter i,j,k value to create multidimensional array:");
scanf("%d %d %d",&i,&j,&k);
printf("I=%d J=%d K=%d",i,j,k);
a=(int *)malloc(i*j*k*sizeof(int));
for(l=0;l<i;l++)
{
for(m=0;m<j;m++)
{
for(n=0;n<k;n++)
{
printf("Enter array elements:");
a[n][m][k])=getche();
}
}
}
for(l=0;l<i;l++)
{
printf("\n");
for(m=0;m<j;m++)
{
for(n=0;n<k;n++)
{
printf("%d",a[n,m,k]); /* Error at this line: Invalid indirection */

}
}
}

getch();


}


Anyone can help me to assign values???
 
I would read it but it's not properly formatted and I'm not going to sit here and decipher it. Also, all of your questions seem like homework questions. I may be wrong, however if they are, the approach you're taking is wrong :(
 
I dont know what is wrong with me. I dont know how to do here. I tried a quite enough. So I am just asking you friends to help me. Not to write entire code. Just asking what can I do there. Thats it. More over I am novice in programming. I am trying to make me expert. Is there anything wrong in my attitude. Ya. I accept my logical thinking level is low. So I am trying to improvise it. If you have any idea guide me. Thank u. Bye


After this I tried this code: But this is also not working properly. Can u tell me what is wrong in my concept???

#include<stdio.h>
/*#include<alloc.h>*/
#include<conio.h>
main()
{
int *a;
int i,j,k;
int l,m,n;


clrscr();

printf("Enter i,j,k value to create multidimensional array:");
scanf("%d %d %d",&i,&j,&k);

printf("I=%d J=%d K=%d\n",i,j,k);

a=(int *)malloc(i*j*k*sizeof(int));
/* printf("%u",a[i,j,k]); *
a++;
printf("%u",a); */
if(a==NULL)
printf("Error:array not initialised\n");
else
{
for(l=0;l<i;l++)
{
printf("\n");
for(m=0;m<j;m++)
{
for(n=0;n<k;n++)
{
printf("Enter Elements:");
scanf("%d",&a[l,m,n]);

}

}
} }
for(l=0;l<i;l++)
{
printf("\n");
for(m=0;m<j;m++)
{
printf("\n");
for(n=0;n<k;n++)
{
printf("%d ",a[l,m,n]);

}
}
}

getch();


}

More over while I am writing my query I am indenting my code properly. But when I post it it becomes like this.
 
I am getting error at scanf function at first for loop set. ie while assigning values to the array
 
Some how I got a answer. But its not correct. While I am print the values at where I receive the values its printing correctly. But in final result the result is not correct. Anyone can pointout what I did wrong??

Here is my code::

#include<stdio.h>
#include<conio.h>
main()
{
int *a;
int i,j,k;
int l,m,n;


clrscr();

printf("Enter i,j,k value to create multidimensional array:");
scanf("%d %d %d",&i,&j,&k);

printf("I=%d J=%d K=%d\n",i,j,k);

a=(int *)malloc(i*j*k*sizeof(int));

if(a==NULL)
printf("Error:array not initialised\n");
else
{
for(l=0;l<i;l++)
{
printf("\n");
for(m=0;m<j;m++)
{
for(n=0;n<k;n++)
{
printf("Enter Elements:");
scanf("%d",(((a+l)+m)+n));
printf("a[%d][%d][%d]=%d",l,m,n,*(((a+l)+m)+n));

}

}
} }
for(l=0;l<i;l++)
{
printf("\n");
for(m=0;m<j;m++)
{
printf("\n");
for(n=0;n<k;n++)
{
printf("a[%d][%d][%d]=%2d ",l,m,n,*(((a+l)+m)+n));

}
}
}

getch();


}
 
If
Code:
l = 1
m = 2
n = 1
You would be using the
Code:
 1 + 2 + 1 = 4
4th int in the memory you allocated.

If
Code:
l = 2
m = 1
n = 1
You would be using the
Code:
 2 + 1 + 1 = 4
4th int in the memory you allocated.

This is a problem.

If you know the size of each dimension, you can seperate out blocks logically. If you havea 2x2x2 array, you need 8 bytes to store the entire array. The first dimension is 2x2=4 bytes. The second dimension is 2 bytes. The third dimension is one byte each. Knowing this, you can calculate a uniuqe position in the memory for each index.
 
Status
Not open for further replies.
Back
Top Bottom