6数组与指针计算.ppt
文本预览下载声明
第六章 数组与指针;本章主要内容;6.1.1数组;*;*;*;*;*;存储顺序
按行存放,上例中数组a的存储顺序为:;将所有数据写在一个{}内,按顺序赋值
例如:static int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
分行给二维数组赋初值
例如:static int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
可以对部分元素赋初值
例如:static int a[3][4]={{1},{0,6},{0,0,11}};;*;*;#include iostream
using namespace std;
void RowSum(int A[][4], int nrow)
{ int sum;
for (int i = 0; i nrow; i++)
{
sum = 0;
for(int j = 0; j 4; j++)
sum += A[i][j];
cout Sum of row i is sum endl;
A[i][0]=sum;
}
} ;void main(void)
{ int Table[3][4] = {{1,2,3,4},{2,3,4,5},{3,4,5,6}};
for (int i = 0; i 3; i++)
{ for (int j = 0; j 4; j++)
cout Table[i][j] ;
cout endl;
}
RowSum(Table,3);
for (int i = 0; i 3; i++)
cout Table[i][0]
};运行结果:
1 2 3 4
2 3 4 5
3 4 5 6
Sum of row 0 is 10
Sum of row 1 is 14
Sum of row 2 is 18
10 14 18
;6.1.3 对象数组;对象数组初始化;数组元素所属类的构造函数;例6-3 对象数组应用举例;//Point.cpp
#includeiostream
using namespace std;
#include Point.h
Point::Point()
{ X=Y=0;
coutDefault Constructor called.endl;
}
Point::Point(int xx,int yy)
{ X=xx;
Y=yy;
cout Constructor called.endl;
}
Point ::~Point()
{ coutDestructor called.endl; }
void Point ::Move(int x,int y)
{ X=x; Y=y; }
;//6-3.cpp
#includeiostream
#include Point.h
using namespace std;
int main()
{
coutEntering main...endl;
Point A[2];
for(int i=0;i2;i++)
A[i].Move(i+10,i+20);
coutExiting main...endl;
return 0;
};运行结果:
Entering main...
Default Constructor called.
Default Constructor called.
Exiting main...
Destructor called.
Destructor called.;关于内存地址;6.2.1 指针变量的概念;6.2.2 指针变量的初始化;*;*;*;*;*;6.2.3指针变量的运算1.赋值运算;例6-5 指针的声明、赋值???使用;程序运行的结果是:
Output int i=10
Output int pointer i=10
;例6-6 void类型指针的使用;选学:指向常量的指针变量;选学:指针常量;2.指针变量的算术运算;pa;;关系运算——比较两个指针所指向的地址关系, 只有两种,即相等和不相等。
int a, *p1, *p2;
*p1=a;
则:表达式p1= =p2的值为0(假),只有当p1、p2指向同一元素时,表达式p1= =p2的值才为1(真)。
指向相同类型数据的指针之间可以进行各种关系运算。
指向不同数据类型的指针,以及指针与一般整数变量之间
显示全部