Pointer anh Matrix

When I run it, it has some mistake but I don't know wher it was

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
#include <iostream>
#include <windows.h>
#include <time.h>
#include <iomanip>
using namespace std;

int khoiTao( int *a, int m, int n)
{
	for (int i=0;i<m;i++)
	for (int j=0;j<n;j++)
		*(a+i*n+j)=i*j;
}
int inMang(int *a, int m, int n)
{
	for (int i=0;i<m;i++)
	{
		for (int j=0;j<n;j++)
			cout<<setw(3)<<left<<(*(a+i*n+j));
		cout<<endl;
	}
}
int main()
{
	int maTran[20][20],*a,soHang,soCot;
	cout<<"Nhap Vao So Hang: ";cin>>soHang;
	cout<<"Nhap Vao So Cot: ";cin>>soCot;
	a=(int*)maTran[20];
	khoiTao(a,soHang,soCot);
	inMang(a,soHang,soCot);
        system("pause");
}
Last edited on
Change statement

a=(int*)maTran[20];

to

a = reinterpret_cast<int *>( maTran );
a=(int*)maTran[20];-- this is of C
a = reinterpret_cast<int *>( maTran );--->C++

I'm true?
The C cast looks as

a = ( int * )maTran;
You can use of course the record

a=(int*)maTran[20];

But in this case a will point beyond the array.
I'm new C++. Do you have basic ebook of C++? Thank so much
I can advice book "Thinking in C++" by Bruce Eckel.

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
Topic archived. No new replies allowed.