Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Dynamic allocation of multi dimensional array
Closed Thread
Old 05-12-2005, 11:37 AM   #1 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default Dynamic allocation of multi dimensional array

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???
Mohan Giri is offline  
Old 05-12-2005, 10:27 PM   #2 (permalink)
 
Ultra Techie

Join Date: Apr 2005

Posts: 950

M4A1 is on a distinguished road

Send a message via AIM to M4A1
Default

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
M4A1 is offline  
Old 05-12-2005, 11:34 PM   #3 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default

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.
Mohan Giri is offline  
Old 05-13-2005, 12:11 AM   #4 (permalink)
 
Monster Techie

Join Date: Sep 2004

Posts: 1,447

Alex81388

Send a message via AIM to Alex81388
Default

What line of code does the error point to?
__________________
Sig removed due to foul language. Please read the rules regarding the allowable content of sigs before reposting your COMPLIANT sig.
~Trotter
4/21/06
Alex81388 is offline  
Old 05-13-2005, 10:08 AM   #5 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default

I am getting error at scanf function at first for loop set. ie while assigning values to the array
Mohan Giri is offline  
Old 05-13-2005, 12:40 PM   #6 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default

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();


}
Mohan Giri is offline  
Old 05-13-2005, 01:50 PM   #7 (permalink)
 
Super Techie

Join Date: Mar 2005

Posts: 259

C.Ingram

Send a message via AIM to C.Ingram Send a message via Yahoo to C.Ingram
Default

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.
__________________
Christopher Ingram
Principal Consultant, Souken Group, LLC.
C.Ingram@SoukenGroup.com
(856) 392 5244 -- (866) Go Souken
C.Ingram is offline  
Old 05-13-2005, 02:15 PM   #8 (permalink)
 
True Techie

Join Date: Dec 2004

Posts: 145

Mohan Giri

Default

OK Mr.C.Ingram. Thank you for pointing out my error. I will try to solve this.
Mohan Giri is offline  
Old 05-13-2005, 04:46 PM   #9 (permalink)
 
Super Techie

Join Date: May 2005

Posts: 479

furtivefelon

Default

ok.. next time use [CODE ] [ /CODE] to post your code, minus the spaces within the bracket
__________________
lisp hacker
running: FreeBSD 5.4 - still learning
develop with: SBCL + emacs for lisp, Anjuta IDE +gcc for c, SPE for python..
browse with: opera
furtivefelon is offline  
Old 05-15-2005, 01:00 AM   #10 (permalink)
 
Ultra Techie

Join Date: Apr 2005

Posts: 950

M4A1 is on a distinguished road

Send a message via AIM to M4A1
Default

If that's too complicating to follow, you can always use www.rafb.net/paste
M4A1 is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On