#include <stdio.h>
#include <stdlib.h>
typedef struct Ogrenciler {
int no;
char adi[50];
char soyadi[50];
double vize;
double final;
double notu;
} Ogr;
int ogrenciSayisi = 0;
void KayitEkle(Ogr *ogrenci) {
int simdikiOgr = ogrenciSayisi;
if (ogrenciSayisi == 0) {
ogrenciSayisi++;
ogrenci = (Ogr *) malloc(ogrenciSayisi*sizeof(Ogr));
} else {
ogrenciSayisi++;
ogrenci = (Ogr *) realloc(ogrenci, ogrenciSayisi * sizeof(Ogr));
}
printf("No:");
scanf("%d", &ogrenci[simdikiOgr].no);
printf("Adi:");
scanf("%s", ogrenci[simdikiOgr].adi);
printf("Soyadi:");
scanf("%s", ogrenci[simdikiOgr].soyadi);
printf("Vize:");
scanf("%lf", &ogrenci[simdikiOgr].vize);
printf("Final:");
scanf("%lf", &ogrenci[simdikiOgr].final);
ogrenci[simdikiOgr].notu = (ogrenci[simdikiOgr].vize * 0.4) + (ogrenci[simdikiOgr].final * 0.6);
printf("Notu: %lf", ogrenci[simdikiOgr].notu);
printf("\n\n");
printf("Adi: %s\nNo: %d\nVize: %lf\nFinal: %lfNotu: %lf\n",
ogrenci[simdikiOgr].adi, ogrenci[simdikiOgr].no, ogrenci[simdikiOgr].vize, ogrenci[simdikiOgr].final,
ogrenci[simdikiOgr].notu);
}
int main() {
int c;
while (c != 5) {
printf("\n1-\tYeni Kayit Ekle\n2-\tKayit Sil\n3-\tKayitlari Listele\n4-\tOrtalama Hesapla\n5-\tCikis\n");
scanf(" %d", &c);
Ogr *ogrenci;
switch (c) {
case 1:
KayitEkle(ogrenci);
break;
case 2:
KayitSil(ogrenci);
break;
case 3:
KayitListele(ogrenci);
break;
case 4:
OrtHesapla(ogrenci);
break;
case 5:
printf("Cikiliyor");
break;
default:
printf("Gecerli bir girdi yapiniz\n");
break;
}
}
return 0;
}
As u can see, I use malloc() and realloc() for my typedef struct and I'm able to enter only one entry. When I tried adding a new entry (switch case: 1) it doesn't work and crashes after this section:
printf("No:");
scanf("%d", &ogrenci[simdikiOgr].no);
At first, I tried I used calloc(ogrenciSayisi*10*sizeof(Ogr)) but it was created only one space. After that, in the debugger (CLion's) after realloc section, ogrenci pointer becomes a null pointer.
Edit: I'm not trying to return a value. As I know (int a) equals (int a[ ]) so KayitEkle(ogrenci) and void KayitEkle (Ogr ogrenci) seems legit to me. And my ogrenci should be empty in the first place so (Ogr *ogrenci=NULL) is correct as you said right?
Edit2: In malloc section 10 is a mistake. I fixed it. I was trying something and I forgot to delete it.
c
without initialization. That means its value is indeterminate and will seem almost random. Don't do that. – Some programmer dudeogrenci
too! – Some programmer dudeogrenci
variable is changed only locally when you realloc.main
has its own copy which is unchanged. That is why you have problems. You need to understand how variables and pointers work or you will have this problem many times. Read stackoverflow.com/questions/5286453/… – Prof. Falkenrealloc
will actually reallocate to a smaller location, which does not appear to be what you intended to do. You have a 10x multiplier in themalloc
for some reason. Additionally, you should always check the return value frommalloc
andrealloc
to confirm that they have actually returned memory to you. – David Hoelzer