#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];),這是為什麼呢?
改為:
q[i]=&h[i];//q[i]是指針數組,每個元素都是指針,沒有初始化,不能*q[i]=h[i]