指针变量当用来指向一个结构变量时,称之为结构指针变量

padding: 0px; overflow-wrap: break-word; clear: both; text-indent: 28px; color: rgb(24, 30, 51); font-family: PingFangSC, 微软雅黑, 黑体, Arial, Helvetica, sans-serif; font-size: 18px; background-color: rgb(255, 255, 255);">一个指针变量当用来指向一个结构变量时,称之为结构指针变量。结构指针变量中的值是所指向的结构变量的首地址。通过结构指针即可访问该结构变量,这与数组指针和函数指针的情况是相同的。

padding: 0px; overflow-wrap: break-word; clear: both; text-indent: 28px; color: rgb(24, 30, 51); font-family: PingFangSC, 微软雅黑, 黑体, Arial, Helvetica, sans-serif; font-size: 18px; background-color: rgb(255, 255, 255);">指针变量当用来指向一个结构变量时,称之为结构指针变量

结构指针变量说明的一般形式为:

    struct 结构名*结构指针变量名


例如,在前面的例题中定义了stu这个结构,如要说明一个指向stu的指针变量pstu,可写为:

    struct stu{

        int num;

        char *name;

        char sex;

        float score;

    }

    struct stu *pstu;


当然也可在定义stu结构时同时说明pstu。与前面讨论的各类指针变量相同,结构指针变量也必须要先赋值后才能使用

赋值是把结构变量的首地址赋予该指针变量,不能把结构名赋予该指针变量。如果boy是被说明为stu类型的结构变量,则:

    pstu=&boy;


正确的,而:

     pstu=&stu;

是错误的。

结构名和结构变量是两个不同的概念,不能混淆。结构名只能表示一个结构形式,编译系统并不对它分配内存空间。只有当某变量被说明为这种类型的结构时,才对该变量分配存储空间。因此上面&stu这种写法是错误的,不可能去取一个结构名的首地址。有了结构指针变量,就能更方便地访问结构变量的各个成员。

其访问的一般形式为:

    (*结构指针变量).成员名


    或为:

结构指针变量->成员名

例如:

    (*pstu).num


   或者:

     pstu->num


    应该注意(*pstu)两侧的括号不可少,因为成员符“.”的优先级高于“*”。如去掉括号写作*pstu.num则等效于*(pstu.num),这样,意义就完全不对了。下面通过例子来说明结构指针变量的具体说明和使用方法。

例7-9】通过指针输出学生的信息

#include<iostream.h>

struct stu{

    int num;

    char*name;

    charsex;

    floatscore;

} boy1={102,"Zhangping",'M',78.5},*pstu;

int main()

{

   pstu=&boy1;

   cout<<"Number="<<boy1.num<<"\nName="<<boy1.name<<"\n";

cout<<"Sex="<<boy1.sex<<"\nScore="<<boy1.score<<"\n";

   cout<<"Number="<<(*pstu).num<<"\nName="<<(*pstu).name<<"\n";

cout<<"Sex="<<(*pstu).sex<<"\nScore="<<(*pstu).score<<"\n";

cout<<"Number="<<pstu->num<<"\nName="<<pstu->name<<"\n";

cout<<"Sex="<<pstu->sex<<"\nScore="<<pstu->score<<"\n";

}

 

本例程序定义了一个结构stu,定义了stu类型结构变量boy1并作了初始化赋值,还定义了一个指向stu类型结构的指针变量pstu。在main函数中,pstu被赋予boy1的地址,因此pstu指向boy1。然后在printf语句内用三种形式输出boy1的各个成员值。从运行结果可以看出:

结构变量.成员名

(*结构指针变量).成员名

结构指针变量->成员名

 

这三种用于表示结构成员的形式是完全等效的。

例7-10】创建一个链表。

#include<iostream.h>

#include "stdlib.h"

struct list

{

int data;

struct list *next;

};

typedef struct list node;

typedef node *link;

int main()

{

link ptr,head;

int num,i;

ptr=(link)malloc(sizeof(node));

ptr=head;

cout<<"please input 5numbers==>\n";

for(i=0;i<=4;i++)

{

                  cin>>num;

 

ptr->data=num;

ptr->next=(link)malloc(sizeof(node));

if(i==4) ptr->next=NULL;

else ptr=ptr->next;

}

ptr=head;

while(ptr!=NULL)

{

cout<<"The value is==>"<<ptr->data<<"\n";

ptr=ptr->next;

}

return 0;

}

 

在上例中,我们通过struct list *next;指向下一个节点,从而构建起一个首尾相连的链表结构。

例7-11】反向输出一个链表。  

/*reverse output a list*/

#include "stdlib.h"

#include "stdio.h"

#include<iostream.h>

#include "stdlib.h"

struct list

{

int data;

struct list *next;

};

typedef struct list node;

typedef node *link;

int main()

{

link ptr,head,tail;

int num,i;

tail=(link)malloc(sizeof(node));

tail->next=NULL;

ptr=tail;

cout<<"\nplease input 5data==>\n";

for(i=0;i<=4;i++)

{

cin>>num;

ptr->data=num;

head=(link)malloc(sizeof(node));

head->next=ptr;

ptr=head;

}

ptr=ptr->next;

while(ptr!=NULL)

{

cout<<"The value is==>"<<ptr->data<<"\n";

ptr=ptr->next;

}

return 0;

}

 

在上例中,我们通过struct list *next; 指向下一个节点,从而构建起一个首尾相连的链表结构,并将其反向输出出来


Top