@結構體陣列指向結構體變數的指標
一、結構體陣列的定義
struct student
{ int
num;
char name[20];
char sex;
int age;
float score;
char addr[30];
} ;
[struct]
student stu[3];
struct student
{ int
num;
char name[20];
char sex;
int age;
float score;
char addr[30];
} stu[3];
陣列各元素在記憶體中連續存放
二、結構體陣列的初始化
struct student
{ int
num;
char name[20];
char sex;
} stu[3]={{1011, “Li Lin”,‘M’},
{1012,“Wang Lan”,‘F’},
{1013,“Liu Fang”,‘F’}};
struct student
{ int
num;
char
name[20];
char sex;
} stu[ ]={ {1011,“Li Lin”,‘M’},
{1012,“Wang Lan”,‘F’},
{1013,“Liu Fang”,‘F’}};
• 輸入10個學生的姓名、學號和成績,將其中不及格者的姓名、學號和成績輸出(P163.17)
Struct student
{
char
name[10];
int number;
int score;
}
void input(student *p_stu)
{
cout<<“Please
input the name ,number and score of a student:”<<endl;
for(int
i=0;i<10;i++)
{
cin>>p_stu[i].name;
cin>>p_stu[i].number;
cin>>p_stu[i].score;
}
}
void output(student *p_stu)
{
cout<<“Output
the name ,number and score of a student:”<<endl;
for(int
i=0;i<10;i++)
{
if(p_stu[i].score<60)
{
cout<<p_stu[i].name<<" ";
cout<<p_stu[i].number<<" ";
cout<<p_stu[i].score<<endl;
}
}
}
指向結構體變數的指標
一個結構體變數的指標就是該變數所佔據的記憶體段的起始地址。
可以設一個指標變數,用來指向一個結構體變數,此時該指標變數的值是結構體變數的起始地址。
指標變數也可以用來指向結構體陣列中的元素。
int main( ){
struct
Student
{ int num;
char name[10];char sex; float
score;};
Student stu, *p=&stu;
stu.num=10301; strcpy(stu.name,″Wang Fu″);
stu.sex=‘f’; stu.score=89.5;
cout<<stu.
num<<″ ″<<stu.name<<″ ″<<stu.sex<<″ ″<<stu.score<<endl;
cout<<(*p).num<<″
″<<(*p).name<<″ ″<<(*p).sex<<″
″<<(*p).score<<endl;
cout<num<<″
″<< p-> name<<″ ″<< p-> sex<<″ ″<< p->
score<<endl;
return 0;
}
• 指標與記憶體動態分配
通過使用new與delete單目運算子來實現動態變數的分配與撤消
1)動態申請記憶體操作符 new
使用格式:
new <型別名> //動態變數
new <型別名> ( <初值> )
new <型別名> [ <元素個數> ]
//動態陣列
功能:
生成一個(或一批)所給型別的無名動態變數。
結果值:
成功:所生成變數型別的指標(首地址) ,指向新分配的記憶體。
失敗:0(NULL)
• 指標與記憶體動態分配
例:
int *pi, *pj, a=10;
char *pc;
pi = new int;
*pi = a*a;
pc = new char('A');
pj = new int[10];
2)釋放記憶體操作符delete
使用格式:
delete <指標>
delete [ ] <指標>
功能:
釋放通過new生成的動態變數(或動態陣列),但指標變數仍存在。
例:
int *pi, *pj;
pi = new int;
pj = new int[10];
...
delete pi;
//釋放動態變數*pi, 但指標變數pi仍存在
delete []pj;
以變數形式分配記憶體比較死板。有了new和delete,就可以實現一種動態分配記憶體的形式,即通過指標引用,而記憶體的分配和釋放可以在程式的任何地方進行。
int *pint;
char *pchar; float *pfloat;
pfloat = new
float; //生成1個浮點型變數
pchar = new
char; //生成1個字元型變數
pint = new
int; //生成1個整型變數
這些變數都沒有名字。
三個變數的地址分別存在指標pfloat,pchar 和pint,在程式中使用這三個變數時,全通過指標:
*pchar = ‘A’;
*pint = 5;
*pfloat = 4.7;
• 當不再需要這些變數時,可在程式的任何地點釋放掉它們:
delete pchar;
delete pint; delete pfloat;
這裡釋放的是動態的(char,int,float)變數,而不是指標變數(pchar, pint, pfloat)。
• 在使用動態變數時應注意的是,要保護動態變數的地址。
例如在執行
pi=new int; 之後,不要輕易地衝掉指標pi中的值,假如執行了pi=&a;語句之後,再釋放原來生成的動態變數:
delete pi; 已經達不到原來的目標了。
這將造成記憶體的洩漏,如果過多的話,將佔用大量系統記憶體資源,造成記憶體資源的浪費。
動態建立結構體變數舉例
#include
#include
using namespace std;
struct Student
{ int num;
float score[3];
}stu={12345,67.5,89,78.5};
void print(Student *p)
{
cout<num<<"
";
cout<score[0]<<"
";
cout<score[1]<<"
";
cout<score[2]<<endl ;
}
int main( )
{
void
print(Student *);
int i;
Student
*pt=&stu;
print(pt);
pt=new Student;
cin>>pt->num;
for(i=0;i<3;i++)
cin>>pt->score[i];
print(pt);
delete pt;
return 0;
}
(1) 用結構體變數作函式引數
#include
#include
using namespace std;
struct Student //宣告結構體型別Student
{ int num;char name[20]; float score[3]; };
void print(Student stu)
{
cout<<stu.num<<″
″<<stu.name<<″ ″;
cout<<stu.score[0]<<″
″
cout<<stu.score[1]<<″
″<<stu.score[2]<<endl;
}
int main( )
{
Student
stu; //定義結構體變數
stu.num=12345;
stu.name=″Li
Fung″;
stu.score[0]=67.5;
stu.score[1]=89;
stu.score[2]=78.5;
print(stu);
return 0;
}
(2) 用指向結構體變數的指標作實參
#include
#include
using namespace std;
struct Student
{ int num;string name; float score[3];
}stu={12345,″Li Fung″,67.5,89,78.5};
void print(Student *p)
{
cout<num<<″ ″<name<<″ ″;
cout<score[0]<<″ ″;
cout<score[1]<<″ ″<score[2]<<endl;
}
int main( )
{
void print(Student *);
Student
*pt=&stu;
print(pt);
return 0;
}