问题描述
#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;
}
如您所见,我将malloc()和realloc()用于我的typedef结构,并且我只能输入一个条目.当我尝试添加新条目(开关盒:1)时,它不起作用,并在本节后崩溃:
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);
起初,我尝试使用calloc(ogrenciSayisi * 10 * sizeof(Ogr)),但它仅创建了一个空间.之后,在调试器(CLion的realloc节)之后,ogrenci指针变为空指针.
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.
我不是要返回值.据我所知(int a)等于(int a []),所以KayitEkle(ogrenci)和void KayitEkle(Ogr ogrenci)在我看来是合法的.而且我的ogrenci首先应该是空的,所以(Ogr * ogrenci = NULL)是正确的,如您所说的那样?
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:在malloc中,第10节是一个错误.我修好了它.我正在尝试某些操作,但忘记删除了.
In malloc section 10 is a mistake. I fixed it. I was trying something and I forgot to delete it.
推荐答案
您将 ogrenci
指针按值传递给 KayitEkle()
,您在其中修改了它的值,但是不将其修改后的值返回给 main()
.您需要使用指针传递 ogrenci
值(即 KayitEkle(& ogrenci)
)或将新值返回给被调用的值(即 ogrenci = KayitEkle(ogrenci)
).以下是后者的示例. ogrenci
指针位于while循环内,因此每次循环运行时都会将其重新初始化,可能是您打算将其放在循环外以保留其值.局部变量具有 undefined (读取: any )值,无需初始化,因此,如果需要,您需要将ogrenci显式初始化为NULL.请参见初始化.当 ogrenci == NULL
时,您无需检查 ogrenciSayisi == 0
,因为 realloc(NULL,...)
相等到 malloc(...)
.参见重新分配.
You pass ogrenci
pointer by value to KayitEkle()
, you modify it's value inside it, yet not return it's modified value to main()
. You need to pass ogrenci
value using a pointer (ie. KayitEkle(&ogrenci)
) or return the new value to the called (ie. ogrenci = KayitEkle(ogrenci)
). Example below is with the latter.ogrenci
pointer is inside the while loop, so it will be reinitialized every time the loop runs, probably you meant to put it outside of the loop so it's value is preserved.Local variables have undefined (read: any) value without initialization, so you need to explicitly initialize ogrenci to NULL, if you need. See Initialization.You don't need to check for ogrenciSayisi == 0
when ogrenci == NULL
, because realloc(NULL, ...)
is equal to malloc(...)
. See realloc.
#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;
// or void KayitEkle(Ogr **ogrenci) and then use *ogrenci
Ogr *KayitEkle(Ogr *ogrenci) {
int simdikiOgr = ogrenciSayisi;
ogrenciSayisi++;
ogrenci = 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);
return ogrenci;
}
int main() {
int c = 0;
Ogr *ogrenci = NULL;
while (c != 5) {
printf("\n1-\tYeni Kayit Ekle\n2-\tKayit Sil\n3-\tKayitlari Listele\n4-\tOrtalama Hesapla\n5-\tCikis\n");
scanf(" %d", &c);
switch (c) {
case 1:
ogrenci = KayitEkle(ogrenci);
break;
case 2:
ogrenci = KayitSil(ogrenci);
break;
case 3:
ogrenci = KayitListele(ogrenci);
break;
case 4:
ogrenci = OrtHesapla(ogrenci);
break;
case 5:
printf("Cikiliyor");
break;
default:
printf("Gecerli bir girdi yapiniz\n");
break;
}
}
// it's nice to free things
free(ogrenci);
return 0;
}
这篇关于Malloc和Realloc不起作用(C11-CLion)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!