C语言 进程退出,返回值为 255,带指向结构的指针



我有一些函数应该允许我管理动态分配的结构。内存的分配和其中的数据输入不是真正的问题,尽管我的程序在到达某一行代码时停止:(未检测到警告或问题)

if(codeV == p_vendite[ctrl_j].p_venditore[ctrl_i].codVenditore)

此行位于名为 VenditeProdotto(Vendite *p_vendite) 的函数中。

这是代码的重要部分(定义结构)

typedef struct _Venditore {
  int codVenditore;
  int codProdotto;
  int qty;
} Venditore;
typedef struct _Vendite{
  int mmGG;
  Venditore *p_venditore;
} Vendite;
void AggiungiVendita (Vendite *p_vendite);
void VenditeProdotto(Vendite *p_vendite);
void VenditeVenditore(Vendite *p_vendite);
...

这是main()

int main() {
  int check, i, count, flag, choice;
  Vendite *p_Vendite;
  ...
  ...
  p_Vendite = (Vendite*) calloc(numVenditori,sizeof(Vendite));
  ...
  ...
  p_Vendite->p_venditore = (Venditore*)calloc(numVenditori,sizeof(Venditore));
/*menu*/
  flag = TRUE;
  do{
    choice = menu();
    switch (choice) {
      case 1 : AggiungiVendita(p_Vendite); break;
      ...
      case 3 : VenditeProdotto(p_Vendite); break;
      case 4 : VenditeVenditore(p_Vendite); break;
      ...
    }
  } while (flag == TRUE);
  return 0;
}

以下是功能:

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

我在哪里找到调试错误:(在此之后的第 3 行)

  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+ctrl_j)->(p_venditore+ctrl_i)->codProdotto 的东西,因为它是一个指针,但它似乎不起作用。

有几个明显的错误:

Vendite的分配

您正在为 p_Vendite 分配numVenditori元素,但稍后您将在同一指针上迭代 numVendite 次:

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);

Venditore的分配

p_Vendite->p_venditore = (Venditore*)calloc(numVenditori,sizeof(Venditore));

您只是为其中一个Vendite结构分配p_venditore元素。您需要在一个循环中分配所有这些:

for (int j = 0; j < numVendite; j++) {
  p_Vendite[j].p_venditore = (Venditore*)calloc(numVenditori,sizeof(Venditore));
  // And check for allocation errors
}

最新更新