GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-May-2005, 10:42
pointer pointer is offline
Junior Member
 
Join Date: Apr 2005
Posts: 30
pointer is on a distinguished road

Dynamic allocation of multi dimensional array


Hi everyone, could you tell me how to assign values to a dynamically created multi dimensional array?

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


CPP / C++ / C 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",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?
Last edited by admin : 22-Apr-2007 at 10:48. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 12-May-2005, 11:00
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
CPP / C++ / C Code:
				printf("Enter array elements:");
				a[n][m][k])=getche();

Shouldn't that be

CPP / C++ / C Code:
				printf("Enter array elements:");
				a[n][m][l])=getche();

also...

CPP / C++ / C Code:
printf("%d",a[n,m,k]);

should be

CPP / C++ / C Code:
printf("%i", a[n][m][l] );
  #3  
Old 12-May-2005, 13:42
pointer pointer is offline
Junior Member
 
Join Date: Apr 2005
Posts: 30
pointer is on a distinguished road
I tried that also, I am getting same error at the line: scanf("%d",a[n][m][k]);
  #4  
Old 12-May-2005, 13:51
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
Please read my previous posting more carefully. k is not l. l is not k.
  #5  
Old 13-May-2005, 11:37
pointer pointer is offline
Junior Member
 
Join Date: Apr 2005
Posts: 30
pointer is on a distinguished road
Quote:
Originally Posted by maprich
Please read my previous posting more carefully. k is not l. l is not k.

Some how I got answer but some mistakes is there. I will post my code. Can you tell me what is wrong in my code??

CPP / C++ / C 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();


}
Last edited by admin : 22-Apr-2007 at 10:47. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #6  
Old 13-May-2005, 12:33
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
Yes first of all sorry. My previous posting was quite wrong; I did a mistake because c++ and c got mixed up in my mind. It is true that in c you cannot refer to multidimensional array with a[n][m][l].. but in c++ you can because the compiler has more information about the dimensions of the table. .. ok .. but back to the problem.

i think you should write like:
Code:
a[ n*j*i+m*i+l ]
Whenever you need to refer to a coordinate aka a[n][m][l]
this is because l runs from 0 to (i-1), m is the 2.dimension so with every unit increase in m the pointer must jump i and finally n is for the 3.dimension..etc..
so write:
CPP / C++ / C Code:
scanf("%d", &a[n*j*i+m*i+l] )
and
CPP / C++ / C Code:
printf("a[%d][%d][%d]=%d",l,m,n, a[n*j*i+m*i+l] );

hopefully works
  #7  
Old 13-May-2005, 14:26
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
http://www.eskimo.com/~scs/C-faq/q6.16.html
http://www.parashift.com/c++-faq-lit...html#faq-16.16
  #8  
Old 14-May-2005, 00:50
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by pointer
Can you tell me what is wrong...
Yeah, after numerous requests, you refuse to use code tags. Read the sticky and start posting messages that don't have to be cleaned up by the admins...
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic Memory Allocation - Offset Pointers wu_weidong C Programming Language 6 24-Feb-2005 10:48
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 22:26
Creating N string gwk C Programming Language 3 21-Jul-2004 00:27
3D array dynamic memory allocation cjwatchdog C Programming Language 3 20-Feb-2004 16:27

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:55.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.