Click here to Skip to main content
15,311,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student
{
	char ime[20];
	char prezime[20];
	int broj_ispita;
	
}student;

FILE *otvarac(char* ime,char* nacin)
{
	FILE* dat;
	dat=fopen(ime,nacin);
	if(dat==NULL)
	{
		printf("\n Greska prilikom otvaranja datoteke!\n");
		exit(1);
	}
return dat;
}
student* uzmi_studenta(FILE* dat,int* p,int* q)
{
	char ime_studenta[20];
	char prezime_studenta[20];
	int br_i;
	int suma=0;
	int brojac=0;
	student* niz;
	p=&suma;
	q=&brojac;
	niz=(student*)malloc(0);
	while(!feof(dat))
	{
	fscanf(dat,"%s %s %d",ime_studenta,prezime_studenta,&br_i);
	niz=(student*)realloc(niz,(*q+1)*sizeof(student));
	if(niz==NULL)
	{
	printf("\n Doslo je do greske prilikom alociranja memorije!\n");
	exit(1);
    }
		
		strcpy((niz+*q)->ime,ime_studenta);
		strcpy((niz+*q)->prezime,prezime_studenta);
		(niz+*q)->broj_ispita=br_i;
		
		*p=*p+br_i;
		*q=*q+1;
	}
	
	return niz;
}

float prosek(int* a,int* b)
{
float srv;	
	srv=(float)*a/(*b);	
	return srv;
}

void ispis_studenata(student* a,int* p,int* q)
{
	int i=0;
	for(i;i<*q;i++)
	{
		if((a+i)->broj_ispita>prosek(*q,*p))
		printf("%s %s %d ",(a+i)->ime,(a+i)->prezime,(a+i)->broj_ispita);
	}
	printf("\n Prosek je:%f\n",prosek(*q,*p));
	
}
int main()
{
	FILE* in;
	student* str;
	int* a=NULL;
	int* b=NULL;
	in=otvarac("studenti.txt","r");
	str=uzmi_studenta(in,a,b);
	ispis_studenata(str,a,b);
	
	return 0;
	
}


What I have tried:

I don't know what to try, maybe is problem with memory allocation, but I am not sure.
Posted
Updated 5-Jan-22 13:22pm
Comments
Richard Deeming 5-Jan-22 9:57am
   
The return code 0xc0000005 is an access violation. At some point, your program is reading from or writing to memory which it shouldn't.

We can't tell - we have no access to your code while it is running with your data and you need both in order to begin working out what the problem is.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
Quote:
I don't know what to try,

Without the data file, there is nothing we can try.
A run with debugger will at least tell you where is the problem in code.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900