twinkle_snow
2015-12-13 08:14
采纳率: 76.5%
浏览 1.6k
已采纳

结构体指针数组指向结构体类型数组的问题

 #include<stdio.h>
#include<stdlib.h>
struct student
{
    char num;
    char name[10];
    int grade;
};

void swap(struct student *p[],int n)
{
    int k,m,i,j;
    struct student *d;
    k=0;m=n-1;
    while(k<m)
    {
        j=m-1;m=0;
        for(i=k;i<=j;i++)
        if(((*p[i]).grade)>((*p[i+1]).grade))
        {d=p[i];p[i]=p[i+1];p[i+1]=d;m=i;}
        j=k+1;k=0;
        for(i=m;i>=j;i--)
        if(((*p[i-1]).grade)>((*p[i]).grade))
        {d=p[i];p[i]=p[i-1];p[i-1]=d;k=i;}
    }
    return;
}

main()
{
    int i;  
    struct student *q[13];
    struct student h[13]=
    {
        {2,"Lin",92},
        {3,"Zhang",87},
        {4,"Zhao",72},
        {5,"Ma",91},
        {9,"Zhen",85},
        {11,"Wang",100},
        {12,"Li",86},
        {13,"Xu",83},
        {16,"Mao",78},
        {17,"Hao",95},      
        {20,"Lu",82},
        {21,"Song",76},
        {22,"Wu",88}
    };  
    for(i=0;i<13;i++)
**      *q[i]=h[i];**
    printf("学号(num)  姓名(name)  成绩(grade)\n");
    for(i=0;i<13;i++)
        printf("  %02d    %s   %d\n",h[i].num,h[i].name,h[i].grade);
    printf("------------------------------\n");
    swap(q,13);
    for(i=0;i<13;i++)
    printf("  %02d    %s   %d\n",(*q[i]).num,(*q[i]).name,(*q[i]).grade);
}

0x00BB16B0 处有未经处理的异常: 0xC0000005: 写入位置 0xCCCCCCCC 时发生访问冲突。(代码中加粗的行: *q[i]=h[i];),这是为什么呢?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • ysuwood 2015-12-13 08:18
    已采纳
    改为:
     q[i]=&h[i];//q[i]是指针数组,每个元素都是指针,没有初始化,不能*q[i]=h[i]
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题