结构体中“.”操作符合“->”操作符的区别

作者是一名刚接触C语言不就的新手,最近在学习结构体部分的知识遇到了一些问题。先上代码

/*using an array of structures*/
#include <stdio.h>
#include <string.h>

#define TSIZE 45/*size of array to hold title*/
#define FMAX  5/*maximum number of film titles*/

struct film{
    char title[TSIZE];
    int rating;
};

char *s_gets(char *st,int n);

int main(){
    struct film movies[FMAX];
    int i=0;
    int j;
    
    puts("enter first movie title:");
    while(i<FMAX&&s_gets(movies[i].title,TSIZE)!=NULL&&movies[i].title[0]!='\0'){
        puts("enter your rating<0-10>:");
        scanf("%d",&movies[i++].rating);
        while(getchar()!='\n')
            continue;
        puts("Enter next movie title(empty line to stop;)");
    }
    if(i==0)
        printf("no data entered\n");
    else 
        printf("here is the movie list :\n");
    
    for(j=0;j<i;j++){
        printf("Move :%s Rating:%d\n",movies[j].title,movies[j].rating);
    }
    printf("Bye\n");
    return 0;
}

char *s_gets(char *st,int n){
    char *ret_val;
    char *find=NULL;
    ret_val=fgets(st,n,stdin);
    if(ret_val)
    {
        find =strchr(st,'\n');//look for newline
        if(find)                //if the address is not NULL
            *find='\0';         //place a null character there
        else
            while(getchar()!='\n')
                continue;       //dispose of rest of line
    }
    return ret_val;
}

这个函数的主要功能是把电影名存入结构体数组中去,每次输入完电影名后再输入序号。如果输入电影名的数目大于结构体能容纳的数目或者输入空白行程序默认输入结束,程序打印输出结构体中的内容。程序运行的结果如下:

在此程序中通过宏定义规定输入的电影名数目为5

#define TSIZE 45/*size of array to hold title*/
#define FMAX  5/*maximum number of film titles*/

为了能通过键盘输入控制电影名的最大数目,使用动态分配内存的函数malloc分配内存区域用于存放电影名。使用了malloc函数必须使用free函数将内存释放,否则会导致内存泄漏!!!;修改后的代码如下

/*using an array of structures*/
/*this program have not finish*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define TSIZE 45/*size of array to hold title*/
#define FMAX  5/*maximum number of film titles*/
struct film{
    char title[TSIZE];
    int rating;
};
char *s_gets(char *st,int n);
int main(){
    struct film *movies;
    int i=0;
    int j,n;

    printf("enter the number of the movies\n");
    scanf("%d",&n);
    movies=(struct film *)malloc(n*sizeof(struct film));
    puts("enter first movie title:");
    s_gets(movies->title,TSIZE);
    while(i<n&&s_gets((movies+i)->title,TSIZE)!=NULL&&(movies+i)->title[0]!='\0'){
        puts("enter your rating<0-10>:");
        scanf("%d",&(movies+i)->rating);
        while(getchar()!='\n')
            continue;
        puts("Enter next movie title(empty line to stop;)");
        i++;
    }
    if(i==0)
        printf("no data entered\n");
    else
        printf("here is the movie list :\n");
   
    for(j=0;j<i;j++){
        printf("Move :%s Rating:%d\n",(movies+j)->title,(movies+j)->rating);
    }
    printf("Bye\n");
    free(movies);
    return 0;
}
{
    char *ret_val;
    char *find=NULL;
    ret_val=fgets(st,n,stdin);
    if(ret_val)
    {
        find =strchr(st,'\n');//look for newline
        if(find)                //if the address is not NULL
            *find='\0';         //place a null character there
        else
            while(getchar()!='\n')
                continue;       //dispose of rest of line
    }
    return ret_val;
}
在修改代码的过程中遇到比较有意思的情况,

    puts("enter first movie title:");
    s_gets(movies->title,TSIZE);
    while(i<n&&s_gets((movies+i)->title,TSIZE)!=NULL&&(movies+i)->title[0]!='\0'){
        puts("enter your rating<0-10>:");
        scanf("%d",&(movies+i)->rating);
        while(getchar()!='\n')
            continue;
        puts("Enter next movie title(empty line to stop;)");
        i++;

    }

将“.”操作符换成“->”操作符后必须在while循环之前使用调用char *s_gets(char *st,int n))程序才能正常进入循环体,注释掉程序中的s_gets(movies->title,TSIZE);后的运行结果如下

由程序结果推测可知

s_gets函数是通过标准输入函数获得电影名字符串,使用“.”操作符时

while(i<FMAX&&s_gets(movies[i].title,TSIZE)!=NULL&&movies[i].title[0]!='\0')

在while循环的判断条件中,结构体会访问到movies中的成员。并且会执行s_gets函数。程序能正常进入循环

使用“->”操作符时:

   while(i<n&&s_gets((movies+i)->title,TSIZE)!=NULL&&(movies+i)->title[0]!='\0')

经过本人测试注释掉(movies+i)->title[0]!='\0'以及s_gets(movies->title,TSIZE);部分代码时程序能进入循环但是在程序运行时会导致不能提前退出程序和打印结果,程序运行结果如下:

程序未能正常进入循环

 48 char *s_gets(char *st,int n){
 49     char *ret_val;
 50     char *find=NULL;
 51     ret_val=fgets(st,n,stdin);
 52     if(ret_val)
 53     {
 54         find =strchr(st,'\n');//look for newline
 55         if(find)                //if the address is not NULL
 56             *find='\0';         //place a null character there
 57         else
 58             while(getchar()!='\n')
 59                 continue;       //dispose of rest of line
 60     }
 61     return ret_val;
 62 }

通过对比可知“.”操作符和“->”操作符在访问结构体里面的成员时有明显的区别。在使用->操作符应当注意指向的成员是否被修改,能否满足程序执行的条件。

程序摘自C Primer Plus 书中p602页的例程,本人第一次写博客。水平有限,错误之处希望能指出来,以便修改。有什么问题欢迎讨论,一起共同学习。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值