C 进程已退出,返回值为255,带指向结构的指针
我有一些功能可以让我管理动态分配的结构。内存的分配和数据的输入并不是真正的问题,尽管我的程序在到达某一行代码时停止:(未检测到警告或问题) 此行位于名为C 进程已退出,返回值为255,带指向结构的指针,c,pointers,structure,C,Pointers,Structure,我有一些功能可以让我管理动态分配的结构。内存的分配和数据的输入并不是真正的问题,尽管我的程序在到达某一行代码时停止:(未检测到警告或问题) 此行位于名为VenditeProdotto(Vendite*p\u Vendite)的函数中 下面是代码的重要部分(定义结构) 下面是main(): 以下是功能: void AggiungiVendita (Vendite *p_vendite) { int flag, check, answer; i = 0; do{ /*input
VenditeProdotto(Vendite*p\u Vendite)
的函数中
下面是代码的重要部分(定义结构)
下面是main()
:
以下是功能:
void AggiungiVendita (Vendite *p_vendite) {
int flag, check, answer;
i = 0;
do{
/*input of struct - codVenditore,codProdotto,qty*/
...
check = scanf("%d", &(p_vendite[j].p_venditore[i].codVenditore));
...
/*input*/
check = scanf("%d", &(p_vendite[j].p_venditore[i].codProdotto) );
...
/*controllo sull'input*/
check = scanf("%d", &(p_vendite[j].p_venditore[i].qty) );
...
...
//asking to redo or quit
} while(flag == TRUE && i < numVenditori);
return;
}
int menu() {
//just a standard menu, no problem here
...
return choice;
}
void VenditeProdotto(Vendite *p_vendite) {
int check = 0, codeP = 0, ctrl_i = 0, ctrl_j = 0; //ctrl_i,ctrl_j are increasing variables and I use them to search among the structures
...//input, continues after
void aggiungingivendita(Vendite*p_Vendite){
int标志、检查、应答;
i=0;
做{
/*结构的输入-CODvenditor、codProdotto、数量*/
...
check=scanf(“%d”和(p_vendite[j].p_venditor[i].codvenditor));
...
/*输入*/
check=scanf(“%d”和(p_vendite[j].p_venditor[i].codProdotto));
...
/*控制输入*/
check=scanf(“%d”和(p_vendite[j].p_venditor[i].qty));
...
...
//要求重做或退出
}while(flag==TRUE&&i
我发现调试错误的地方:(后面的第3行)
(ctrl_j=0;ctrl_j->
,是否真的合法,但如果我尝试更改语法,就会发现错误。有什么想法吗
起初我想到了类似于code>(p_vendite+ctrl_j)->(p_venditor+ctrl_I)->codProdotto,因为它是一个指针,但似乎不起作用。有几个明显的错误:
Vendite的分配
您正在为p_Vendite
分配numVenditori
元素,但稍后您将在同一指针上迭代numVendite
次:
或者我更喜欢:
p_Vendite = calloc (numVendite, sizeof *p_Vendite);
卖方的分配
您只为Vendite
结构的一个分配p\u venditor
元素。您需要在一个循环中分配所有资源:
for (int j = 0; j < numVendite; j++) {
p_Vendite[j].p_venditore = (Venditore*)calloc(numVenditori,sizeof(Venditore));
// And check for allocation errors
}
for(int j=0;j
void AggiungiVendita (Vendite *p_vendite) {
int flag, check, answer;
i = 0;
do{
/*input of struct - codVenditore,codProdotto,qty*/
...
check = scanf("%d", &(p_vendite[j].p_venditore[i].codVenditore));
...
/*input*/
check = scanf("%d", &(p_vendite[j].p_venditore[i].codProdotto) );
...
/*controllo sull'input*/
check = scanf("%d", &(p_vendite[j].p_venditore[i].qty) );
...
...
//asking to redo or quit
} while(flag == TRUE && i < numVenditori);
return;
}
int menu() {
//just a standard menu, no problem here
...
return choice;
}
void VenditeProdotto(Vendite *p_vendite) {
int check = 0, codeP = 0, ctrl_i = 0, ctrl_j = 0; //ctrl_i,ctrl_j are increasing variables and I use them to search among the structures
...//input, continues after
for(ctrl_j = 0; ctrl_j < numVendite; ctrl_j++) {
for(ctrl_i = 0; ctrl_i < numVenditori; ctrl_i++) {
if (codeP == p_vendite[ctrl_j].p_venditore[ctrl_i].codProdotto)
printf("\nSeller %d, quantity sold: %d in day %d", p_vendite[ctrl_j].p_venditore[ctrl_i].codVenditore, p_vendite[ctrl_j].p_venditore[ctrl_i].qty, ctrl_j+1);
else
continue;
}
}
return;
}
p_Vendite = (Vendite*) calloc(numVenditori,sizeof(Vendite));
...
for(ctrl_j = 0; ctrl_j < numVendite; ctrl_j++) {
for(ctrl_i = 0; ctrl_i < numVenditori; ctrl_i++) {
if (codeP == p_vendite[ctrl_j].p_venditore[ctrl_i].codProdotto)
p_Vendite = (Vendite*) calloc(numVendite,sizeof(Vendite));
p_Vendite = calloc (numVendite, sizeof *p_Vendite);
p_Vendite->p_venditore = (Venditore*)calloc(numVenditori,sizeof(Venditore));
for (int j = 0; j < numVendite; j++) {
p_Vendite[j].p_venditore = (Venditore*)calloc(numVenditori,sizeof(Venditore));
// And check for allocation errors
}