Pointer question

Hi Guys,

Wonder if there's a way to initilize the ten arrays in the first loop without having to run through:

pointer[0]=a1;
...
...
pointer[9]=a10;


Thanks,

Mike

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const int MAX=10;


int main(){
	
	char temp='a';
	int i,j;
	int *pointer[MAX];
	int a1[MAX]={0,10,20,30,40,50,60,70,80,90};
	int a2[MAX]={100,110,120,130,140,150,160,170,180,190};
	int a3[MAX]={200,210,220,230,240,250,260,270,280,290};
	int a4[MAX]={300,310,320,330,340,350,360,370,380,390};
	int a5[MAX]={400,410,420,430,440,450,460,470,480,490};
	int a6[MAX]={500,510,520,530,540,550,560,570,580,590};
	int a7[MAX]={600,610,620,630,640,650,660,670,680,690};
	int a8[MAX]={700,710,720,730,740,750,760,770,780,790};
	int a9[MAX]={800,810,820,830,840,850,860,870,880,890};
	int a10[MAX]={900,910,920,930,940,950,960,970,980,990};


	for(i=0;i<MAX;i++)//is there a way to initilize arrays in this loop?
		*(pointer+i)=new int[MAX];
		
	pointer[0]=a1;
	pointer[1]=a2;
	pointer[2]=a3;
	pointer[3]=a4;
	pointer[4]=a5;
	pointer[5]=a6;
	pointer[6]=a7;
	pointer[7]=a8;
	pointer[8]=a9;
	pointer[9]=a10;


	for(i=0;i<MAX;i++)
		for(j=0;j<MAX;j++)
			cout<<*(*(pointer+i)+j)<<",";
	

	_getch();
	return 0;
}
the memcpy() function may help.
You need to delete. But actually, you don't even need to make new.
1
2
3
int a1[MAX];
int *pointer[MAX;
pointer[0] = a1;


You can't really make it any better the way you have it set up. It could be set up better though:
1
2
3
4
5
6
7
int a[MAX][MAX];
int *pointer = a;

// fill the arrays with something

while (pointer < a + (MAX * MAX)) //I think
  cout << *pointer++ << ",";
Last edited on
1
2
while (pointer < a + (MAX * MAX)) //I think
  cout << *pointer++ << ",";


I assume you wanted to increment the pointer.
Be careful, the dereference operator (*) takes precedence.

1
2
while (pointer < a + (MAX * MAX))
  cout << *(pointer++) << ",";
It's right the way it is, try it.
Topic archived. No new replies allowed.