用指针输出结构体数组【转】

(转自:https://wenku.baidu.com/view/6511f01477c66137ee06eff9aef8941ea76e4bac.html

#include <stdio.h> 
#include <string.h>
#include <math.h> 

int main(void) 
{ 
    struct Student
    {   
        longnum;   
        char name[20];   
        char sex;   
        float score;
    };
 
    //定义结构体数组  
    struct Student stu[3] = {
        {20154511, "liming", 'M', 99},
        {20153424, "xiaowang", 'M', 66.5},
        {20153623, "xiaohong", 'F', 59.5}
    };  
 
    struct Student *p;     
    //用指针输出结构体数组

    /*第一种方法  
    for (p = stu; p < stu + 3; p++) {
        printf("|%-10d|%-10s|%-10c|%-10f\n", p->num, p->name, p->sex, p->score);
    }
    */

    //第二种方法  
    p = stu;//重新使P指向stu开头  

    for (int i = 0; i < 3; i++) {
        printf("|%-10d|%-10s|%-10c|%-10f\n", (p)->num, (p)->name, (p)->sex, (p)->score);
        p++;
    } 

    //delete(p); 
    }

猜你喜欢

转载自blog.csdn.net/biqioso/article/details/83988297