0

我刚刚完成了我的课堂作业,但是当我运行代码时出现错误。第一个错误出现在第 52 行:

size = GetAndSetStd(x);

然后我按继续,程序继续运行,直到第 88 行:

 *x = (student *)malloc( size * sizeof(student) );

我得到的最后一个错误是这个:

Unhandled exception at 0x011f162b in Test01.exe: 0xC0000005: Access violation writing location 0xcccccccc.

正如你所看到的,我已经为它分配了足够的内存,所以我不知道代码中的问题是什么。

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

typedef struct{
    int id;
    char lname[16];
    char fname[11];
    unsigned a[3];
    int flag;
}student;

int GetAndSetStd (student **x);
/*Function name:GetAndSetStd
Description : Function asks the user the number of students receiving data from the students.
Input:  A double pointer to a student struct and  size of the array
Output: size - size of the students in the array
Algorithm: First we get the size of the stuedents array from the user,Second Allocate memory for the array and by a for loop getting the info from the soruce text file..*/
void PrintArray (student *x,int size);
/*Function name:PrintArray
Description : The function prints the students on the screen array
Input:  A pointer to a student struct and  size of the array
Output: none
Algorithm: By a for loop the function prints the array of students.*/
student * MaxAvg(student *x,int size);
/*Function name:MaxAvg
Description : Students function accepts an array returns the address structure of the student with the highest average score.
Input:  A pointer to a student struct and  size of the array
Output: Address the structure of the student with the highest average score.
Algorithm: By a for loop the function calculates the avg score of the student and checks if it's higher than the next score .if it is higher it's save the index of the score and when the FOR loop is finished return the address of the index.*/
float StdAvg(student *x); 
/*Function name:StdAvg
Description : The function gets a pointer to the student and calculate the grades average score.
Input:  A pointer to a student in the struct
Output: returns the average grade score.
Algorithm: The function calculates the sum of the three grades of the student and then divide by the number of grades .the function returns the average score of the student.*/
void ChangeRandStd (student *x,int size);
/*Function name:ChangeRandStd
Description : The function gets the array of students and length of it, taking a random student and replaces the flag field value from 1 to 0.
Input: A pointer to a student struct and  size of the array
Output: none
Algorithm: By DO WHILE loop, using rand function  in the loop. we pick a random student and checks if the flag field is 1,unless keep searching for flag field value=1. after finding it change the value of flag field from 1 to 0.*/
int CopyToAnotherGroup (student *x,int size,student **arr);
void Get_Lost(char* str);
int main()
 { student **x,**y,*p,temp;
   int size,n=1,len;
     size=GetAndSetStd(x);
     while(n)
     {
         printf("Press 1 to see all students\n");
         printf("Press 2 to see two students with biggest average\n");
         printf("Press 3 to change flag for random student\n");
         printf("Press 4 to see all students from the new group\n");
         printf("Press 0 to exit\n");
         printf("enter your choise:");
         scanf("%d",&n);
         switch(n)
         {
         case 1: PrintArray(*x,size);break;
         case 2: PrintArray(p=MaxAvg(*x,size),1);
             temp=*p;
             *p=*x[size-1];
             *x[size-1]=temp;
             PrintArray(MaxAvg(*x,size-1),1);
             break;
         case 3: ChangeRandStd(*x,size);
             break;
         case 4: len=CopyToAnotherGroup(*x,size,y);
             PrintArray(*y,len);
         }
     }
     free(*x);
     free(*y);
     system("pause");
     return 0;
 }
 int GetAndSetStd (student **x)
 { int size,i;
    FILE *f;
    f=fopen("list.txt","r");
     printf("Please enter the number of the students:");
     scanf("%d",&size);
     *x=(student *)malloc(size*sizeof(student));
     if(!x)
         Get_Lost("no memmory");
     for (i=0 ; i<size ; i++)
     {
         fscanf(f,"%d%s%s%u%u%u",(*x)[i].id,(*x)[i].lname,(*x)[i].fname,(*x)[i].a[0],(*x)[i].a[1],(*x)[i].a[2]);
         (*x)[i].flag=1;
     }
     fclose(f);
     return size;
 }
 void PrintArray (student *x,int size)
 {
     int i;
     for (i=0 ; i<size ; i++ )
     {
         printf("%d,%s,%s,%d,%d,%d,%d",x[i].id,x[i].lname,x[i].fname,x[i].flag,x[i].a[0],x[i].a[1],x[i].a[2]);
     }
 }
 float StdAvg(student *x)
 {
     int sum=0;
     float avg=0;
     sum=(*x).a[0]+(*x).a[1]+(*x).a[2];
     avg=sum/3.0;
     return avg;

 }
 student * MaxAvg(student *x,int size)
 {
     float max_avg=StdAvg(x);
     int i,index=0;
     for(i=1 ; i<size ; i++ )
        {
            if(StdAvg(x+i)>max_avg)
            {
                max_avg=StdAvg(x+i);
                index=i;

            }
        }
     return x+i;
 }
 void ChangeRandStd (student *x,int size)
 {
     int temp;
     do
     {
         temp=0+rand()%(size);
     }while(x[temp].flag!=0);
     x[temp].flag=0;

 }
 int CopyToAnotherGroup (student *x,int size,student **arr)
 {
     int i,count=0;
     for ( i=0 ; i< size ; i++ )
     {
         if(x[i].flag==0)
         {
             count++;
             *arr=(student*)realloc(arr,count*sizeof(student));
             if(!*arr)
                 Get_Lost("no memmory");
             (*arr)[count-1]=x[i];
         }
     }
     return count;
 }
 void Get_Lost(char* str)
{
    printf("\n%s",str);
        exit(1);
}
4

3 回答 3

2

您的问题是您的声明和使用xin main()。您应该声明一个单间接指针并传递其地址

student *x = NULL;

GetAndSetStd(&x);

并相应地更新代码的其余部分(这将是很多变化,但这就是编写这么多代码而没有看到它是否真的增量正确的代价)。正如所写的那样,您正在传递一个不确定的指针值,并在被调用的函数中取消引用 for-write 它。在你问之前,是的,y有类似的问题。

剩下的留给你(并且 Aniket 已经指出你对 的错误调用fscanf)。

于 2013-11-06T08:49:45.053 回答
1

scanf,fscanf()也期望你传入变量的地址来存储输入数据。你没有fscanf()在线..这可能是你的段错误的来源。

该错误基本上意味着您正在访问未经授权的内存位置。

     fscanf(f,"%d%s%s%u%u%u",(*x)[i].id,(*x)[i].lname,(*x)[i].fname,(*x)[i].a[0],(*x)[i].a[1],(*x)[i].a[2]);

应该:

     fscanf(f,"%d%s%s%u%u%u",(*x)[i].id,(*x)[i].lname,(*x)[i].fname,&((*x)[i].a[0]),&((*x)[i].a[1]),&((*x)[i].a[2])));
于 2013-11-06T08:48:48.163 回答
0

谢谢你们的帮助。WhozCraig:我必须向某些函数发送双指针而不是单指针。这是我们老师要求的:|

于 2013-11-06T19:26:00.163 回答