Click here to load reader
Upload
eagan
View
153
Download
0
Embed Size (px)
DESCRIPTION
程序设计是计算机学科的. 核心和灵魂. 第六章 复合数据类型. 程序设计基础. 2000H. 0x34. x. 0x12. 6.1 变量与赋值. 变量在内存中占用的存储空间的首地址称为变量的地址. 例: int x=0x1234; 则 x 的地址为 2000H. 根据变量的使用属性,变量分:. 值变量: 在其存储空间中存放的是该变量的值,按变量名存取变量值; 地址变量: 在其存储空间中存放的是地址,使用变量名时操作的是变量的地址;. 指针类型的变量是最常用的地址变量. 6.2 指针类型. 指针 : 一个变量的地址称为该变量的指针. - PowerPoint PPT Presentation
Citation preview
int x=0x1234; x 2000H6.1
6.2:int a=1;&a int *bbb*b
12345678 void9.10.11.12.
1.
*1,*n;,: int * p, * q; /* pq*/ float x ,y; pf=&x; /* pfpfValuexy*/ pfValue=&y;
* int *pInt1, pInt2; pInt2** pInt1 pInt1* pInt1 pInt1 pInt2
2.
int a=0, *p;p=&a;*p=*p+2; & &aa * *ppint a,*p; 2000Happ=&a; /* pa */2000H*p=2;26-1
int age = 30; int* age_ptr; age_ptr = & age ;*age_ptr = 50 ;50age_ptr &age*age_ptr age *age_ptr = 50 age = 50;age++; (*age_ptr)++;
1) int *p; *p=2; /* Error! */int a,*p; p=&a; /* pa */*p=2;2)& * int a,*p; p=&a;&*p p *&a a
3)&
p = &45//p = &i + &j //p = &i + 4; //
*
int i;int *p = &i;*p = 45//*i = 34//
3.p = &i;p = &j;2df42df8)............9915612342df01238123c2df42df8............int * p;int i = 156;int j = 99;
6-4
#include void main( ){int a,*ptr1=&a,*ptr2=&a;cout
4unsigned long intsizeof(int *) == sizeof(char *) ==sizeof(float *) == sizeof(char **)
5.6-51#include 2void main( )3{4int a=10,*ptr1=a,*ptr2,*ptr3;5*ptr2=*ptr1;6ptr3=&ptr2;7a=ptr2;8}
p1p2
6-8
#includevoid main(){ int *p1,*p2,i1,i2,i; cin>>i1>>i2; p1=&i1;p2=&i2; cout
p1p2
6-9
6-7#includevoid main(){ int *p1,*p2,*p,i1,i2; cin>>i1>>i2; p1=&i1;p2=&i2; cout
7.
**int a,*q=&a;int **p=&q;ppp
6-10
#include #include void main( ){char **p;char a='x',*q=&a;p=&q;cout
8. voidvoid ()1void void2voidvoid *p1; int *p2; p1 = p2; void *p1; int *p2; p2 = p1;
9.const int *ptr=&x;*ptr=4;ptr=&y; int * const ptr=&x;*ptr=4;ptr=&y;const int * const ptr=&x;*ptr=4;ptr=&y;,,, (1)const(2)const*,((3)const
10.C++C++
111p1++++p1p1----p1/*a()*/2p1+np1-n/*a()n*/3p2-p1/*ab*/int a, b, *p1, *p2;p1=&a;p2=&b;a2000Hb2400Hp1p22000H2400H2004H2004H100H
() ptr+n (ptrn)ptr+n*d(d)n ptr1-ptr2=(ptr1- ptr2) /
p1 == p2p2 > p1p2 < p1
1)char a, *p;p=&a;2)int *p, *q;p=q;3)char a[10], *pa;pa=a;4)int *p, *q, n;p=q+n;p=q-n;p+=n;p-=n;
: void swap(int *p,int *q){int temp = *p;*p = *q;*q = temp;}swap(2, 3);
12.1) & = ;
6.310,void main(){ int w1, w2, w3, w4, w5, w6, w7, w8, w9, w10; int t; cin>>w1>>w2>>&w3>>w4>>w5>>w6>>w7>>w8>>w9>>w10;
1.2.3.4.5.6.7.8.9.10.11.12.13.14.
1..
C++0 w[0]w w[9]w
2. 1[], , n[].,,
: int a[5]; a[0]a[1]a[2]a[3]a[4]a()5,4BYTEa:,int a; float a[5];[ ][]0 int n=5; int a[n];
,0int a[5];5a[0],a[1], a[2], a[3], a[4]int a(10); int n=5;int a[n]char name[0]; float weight[10.3];float array[-100];
3.{ } int a[5]={2, 4, 6, 8, 10};{}
int a[ ]={ 2 , 4 , 6 , 8 , 10 }; int a[ ]; 0 int a[5]={ 2 , 4 }; a[0]=2, a[1]=4, a[2]=0, a[3]=0, a[4]=0 int a[5]={1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 }; (3)0 int a[10]={0};
int a[10]; int i; for(i=0;i>a[i];4.
5.,,,.12 [ ] :C++,.
[] a[3] int a[10] ; float i=3 ; a[i]=10 ;
0 int a[2]; cin>>a[1]>>a[2];1 int a[5]={ 2 , 4 , 6 , 8 , 10 } , b[5] ; b[5]=a[5] ;2 int a[5] ; a[5]={ 2 , 4 , 6 , 8 , 10 } ;aaaa[0]
7. 1999 90 40 80 30 2000 100 50 90 40 2001 95 45 100 50
8. [1][2] int a[3][4];int a[3,4];
:.int a[3][4];/*3*4(34). */a:a[0] a[1] a[2]4 a[0][0] a[0][1] a[0][2] a[0][3]a[1][0] a[1][1] a[1][2] a[1][3]a[2][0] a[2][1] a[2][2] a[2][3] a
int a[0][3]; 0 int i=3 , j=4 ; int a[i][j] ;
9. int a[2][3]={{1 , 2 , 3 } , { 4 , 5 , 6 }}; int a[2][3]={1 , 2 , 3 , 4 , 5 , 6 };
0 int a[3][4]={{1},{5},{9}};int a[3][4]={{1},{5,6}};int a[3][4]={{1},{},{0,0,11}};int a[3][4]={1,0,6,7,8,9};
()int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};int a[][4]={1,2,3,4,5,6,7,8,9,10,11,12};int a[][4]={{0,0,3},{},{0,10}};
10. int a[2*5][3*4], i=15; a[3*3][0], a[1][i-5]
b[1][2]=a[2][3]/2int a[3][4]a[3][4]=3aa aa[0][0]
12. [1][n]int a[2][3] [2];a[0][0][0]a[0][0][1]a[0][1][0]a[0][1][1]a[0][2][0]a[0][2][1]na[1][0][0]a[1][0][1]a[1][1][0]a[1][1][1]a[1][2][0]a[1][2][1]3.
13.1)2) aa[0] [][][] int i, a[5], *pa=a; a[i]pa[i]*(a+i)*(pa+i)
1)int a[5], *pa=a;pa[0],*(a)pa[1],*(a+1)pa[2],*(a+2)pa[3],*(a+3)pa[4],*(a+4)
1)int a[10],*p;2)p = a;3)ip+i,a+i&p[i] &a[i] a[i]*(p + i) a[i] *(p + i) *(a+i) p a *p a[0] *ap[0]*(p + i) a[i] *(a+ i)p[i]p+i a + i &a[i]&p[i]a[2]a[1]a[0]............a[3]ap::a[i] p[i]:*(p+i):*(a+i)
#includevoid main(){ int a[5]={1,2,3,4,5},i;int *p=a;for (i=0;i
2)aint a[3][4];a[0] aa[1]a[2]---------a[0][0] a[0][1] a[0][2] a[0][3]---------a[1][0] a[1][1] a[1][2] a[1][3]---------a[2][0] a[2][1] a[2][2] a[2][3]
int a[3][4] a 0 a , 1 a + 1, 2 a + 2; a &a[0][0]*a a[0]*(a+0)a+1a+i, a[i],*(a+i),&a[i][0] a[i]+j, &a[i][j], *(a+i)+ja+1&a[1][0] a[1]*(a+1)a+2&a[2][0] a[2]*(a+2)
:
1)a[i][j]*(a[i]+j) *(a+i)+j*(&a[0][0]+col*i+j)
2)int *p,a[3][4];p=a[0];a[i][j] *(p+4*i+j)
14. 1)2)1)
*[]int *p[4]
#include #include void main( ){char *proname[ ]={"Fortran","c","c++"};cout
2) (*)[]int (*p)[4]4int a[3][4], (*p)[4];p=a;a[i][j] *(*(p+i)+j)int a[3][3],*p; p=a[0]; a[i][j] *(p+3*i+j)
6.4 \0china 65
char c[10]; c1.
1)char charray[const exp1][const expn],;char a[10],b[2][12];char str1[6]={ h, e, l, l, o};char str2[ ]={hello };
\0hello\0
int c[10] 4 char c[10]1int c[10]4 \0 int c[8]={H,e,l,l,o}; c[0]=H, c[1]= e, c[2]= l, c[3]= l, c[4]= ,o c[5]=\0, c[6]=\0, c[7]=\0
C[0]C[1]C[2]C[3]C[4]C[5]C[6]C[7]C[8]C[9]Iamhappy
C[0]C[1]C[2]C[3]C[4]C[5]C[6]C[7]C[8]C[9]Iamhappy
2) char str[]={Hello} char str[]=Hello char str[]={H, e, l, l, o}; H e l l o H e l l o \0\0char ch[2]={A, B}\0\0char str[]={H, e, l, l, o, \0};
2.1char *str =I love China! ;str1\0 2
1
2 2
char *a=I love China!; char *a; a=I love China!; char str[14]={I love China!}; char str[14]str[]=I love China!;
6.5 -> 1class exe{ int x; public: void set_a(int a) { x=a; } void show_a() { cout show_a();}2void main(){ exe ob[2], *p; ob[0].set_a(10); ob[1].seta(20); p=ob; p->show_a(); p++; p->shoe_a();}
this
thisthisthis,this.,this.,this.
main(){Person p,q;p.setname ("C++");q.setname ("VC");
}C++VCthis void setname(string n){ this->name=n; }100200100200this string getname(void){ return this->name; }cout
this ,;this,.
this ,
newdelete
newdelete-------- new:: newnew3:1 int*ip; ip=newint; 2 int*ip; ip=newint(68); 3 int*ip; ip=newint[5]; delete ip;delete[] ip;delete ip;
delete:new:delete delete[] 1newdelete 2delete 3deletenew 4new
int i=10;char *lpBuf; lpBuf = new char[i]; lpBuficharlpBuf[0],lpBuf[1] ... delete[] lpBuf; NULLNULL*void
6.66.6.11. 2.,,.,3. (*)( ) int (*fp)(); /* fpint*/
5.
18:process a b processa b a b ++--
6.6.21.,2. *([]){} int *pr(x,y); pr().()*,pr()..*,()int int (*p)(int i,int j); int *p(int i,int j);
6.7 .06001 Li Fun M 18 87.5 Beijing Num name sex age score addr,.,++()
struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }struct { .},
struct studentstruct student
3 numstruct studentnum
4
struct date{ int year; int month; int day;}; struct student{ int num; char name[20] char sex; struct date birthday; float score[4];};num score
intcharfloat.1.struct { .}struct struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }; struct student stu1,stu2;
2. struct { .}struct student { int num; char name[20]; char sex; int age; float score; }stu1,stu2; sizeofsizeof(struct student)sizeof(stu1)
.0:struct { .}struct ={} struct student { int num; char name[20]; char sex; int age; char addr[30]; }; struct student stu1={06001,Wang Lin,M,19, 200 Beijing Road};
:struct { .}={} struct student { int num; char name[20]; char sex; int age; char addr[30]; }stu1={06001,Wang Lin,M,19, 200 Beijing Road};
. ,(): struct student { int num; char name[20]; char sex; int age; float score; char addr[30]; }stu1,stu2; stu1.num=10;stu1.score=85.5;stu1.score+=stu2.score; stu1.age++;cin>>stu1; ()stu1={06001,Wan Lin,M,19,87.5,DaLian}; ()
:struct student stu1,stu2;cin>>stu1; () student1.birthday.month:student2.score=student1.score; student1.age++;struct date{ int year; int month; int day;}; struct student{ int num; char name[20] char sex; struct date birthday; float score[4];};
&student1.num student1 stu2=stu1;
,++1.:,.2.: enum {} enum Weekdays {sun, mon, tue, wed, thu, fri, sat}; enum Color{red, yellow, blue, white, black}; enum Sex{male, female};
3.enum weekdays {sun, mon, tue, wed, thu, fri, sat};enum weekdays workday;
enum weekdays {sun, mon, tue, wed, thu, fri, sat} workday;enum {Sun,Mon,Tue,Wed,Thu,Fri,Sat} workday;
:1)2) weekdays sun=0 ; mon=1; sun=mon ;
3),: workday=2; workday=tue; workday =(enum Day)6; a=sat;
4)0012 a=sat; a1 enum weekdays {Sun=, Mon ,Tue, Wed, Thu, Fri, Sat}Sun=Mon=Tue=25).,: if (workday==mon) if (workday>tue)
typedeftypedeftypedef typedef float REAL; /* REALfloat */ float a, b; REAL a, b; NUM typedef int NUM[10]; int n[10], m[10]; NUM n, m; NODE typedef struct student { char num[4]; float score;} NODE; struct student st,*p; NODE st,*p;
1) ()::.,,,,,
2),
[7.14] int Sum(int* pData, int nNum){ int nResult=0; for(int i=0; i