【问题标题】:Problem either in allocation or handling array of pointer to struct分配或处理指向结构的指针数组时出现问题
【发布时间】:2021-12-28 09:35:08
【问题描述】:

我有一个问题(或者可能有几个)
我正在努力解决指向 struct 的指针的问题,但我不明白是内存分配问题,还是将正确的东西传递给我的函数的问题。
这是(部分)代码

typedef struct Corsa {
  char codice[15];
  char partenza[15];
  char destinazione[15];
  int giorno, mese, anno, oraP, minP, oraA, minA, ritardo;
} corsa;

corsa** allocaStructArray(int numStruct);
void leggiEStampaStruct(FILE *fp, corsa **pntToCorsa, int numR);

// ..main and other functs..

corsa** allocaStructArray(int numStruct) //numStruct is the number of 'rows'
{
  corsa **pointerToStructCorsa;
  pointerToStructCorsa = malloc(numStruct * sizeof(corsa));

  if (pointerToStructCorsa == NULL)
    printf("\nErrore di allocazione.\n");

  return pointerToStructCorsa;
}

void leggiEStampaStruct(FILE *fp, corsa **pntToCorsa, int numR) {
  int i;
  fclose(fp);
  fp = fopen("file.txt", "r");
  if (fp == NULL)
    exit(232);

  for (i = 0; i < numR; i++) {
    fscanf(fp, "%s %s %s %d/%d/%d %d.%d %d.%d %d", pntToCorsa[i]->codice,
        pntToCorsa[i]->partenza, pntToCorsa[i]->destinazione,
        &pntToCorsa[i]->giorno, &pntToCorsa[i]->mese, &pntToCorsa[i]->anno,
        &pntToCorsa[i]->oraP, &pntToCorsa[i]->minP, &pntToCorsa[i]->oraA,
        &pntToCorsa[i]->minA, &pntToCorsa[i]->ritardo);

  }

  for (i = 0; i < numR; i++) {
    printf("\n%s %s %s %d/%d/%d %d.%d %d.%d %d\n", pntToCorsa[i]->codice,
        pntToCorsa[i]->partenza, pntToCorsa[i]->destinazione,
        pntToCorsa[i]->giorno, pntToCorsa[i]->mese, pntToCorsa[i]->anno,
        pntToCorsa[i]->oraP, pntToCorsa[i]->minP, pntToCorsa[i]->oraA,
        pntToCorsa[i]->minA, pntToCorsa[i]->ritardo);
  }

  return;
}

我不喜欢的另一件事是我无法手动检查是否所有东西都在原位。

就像它是一个普通矩阵一样,我可以检查值是否放置在正确的位置,但使用指针我确实不能,这无助于发现问题。

你们对这个其他问题有什么建议吗?

【问题讨论】:

  • 我认为allocaStructArray 应该只返回corsa* 而不是corsa**
  • 您的标题说指向结构的指针数组,但您只为结构数组分配内存,没有指向结构的指针。如果你没有指针,kiner_shah 的注释是正确的。只需从该返回类型中删除一个*。也在那个函数里面。
  • 在打印过程中您遇到了同样的问题。 pntToCorsa[i] 被分配为结构,而不是指针。您也没有显示任何会为这样的指针分配一些内存的代码。因此它应该是pntToCorsa[i].codice 而不是pntToCorsa[i]-&gt;codice
  • 您根本没有告诉我们您的问题是什么。使用错误的类型进行内存分配会导致内存浪费,但不会造成任何麻烦,因为您的结构大于单个指针。因此,“这无助于发现问题”解决了什么问题?
  • 您是否在代码中未显示的任何位置为pntToCorsa[i] 分配内存?

标签: arrays c pointers struct


【解决方案1】:

至少这些问题

fscanf() 中没有宽度

不要在fscanf() 中使用没有宽度限制的"%s"。使用 "%14s %14s %14s ... 作为每个指向 15 的数组。

检查输入结果

检查返回值。添加一个测试,看看这 11 个说明符是否都被解析了。

 if (fscanf(....) != 11) Handle_Error();

分配错误

sizeof 输入错误。

通过调整被引用对象的大小而不是而不是类型来避免这个问题。更容易正确编码、审查和维护。另外,不清楚numStruct &lt; 0时OP想做什么,所以加了个测试。

// Wrong size                                v-----------v 
// pointerToStructCorsa = malloc(numStruct * sizeof(corsa));
if (numStruct >= 0) {
  //                            v--------------------------v Right size for referenced object. 
  pointerToStructCorsa = malloc(sizeof *pointerToStructCorsa * numStruct);
  ...

【讨论】:

猜你喜欢
  • 2013-03-02
  • 2021-12-14
  • 2016-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
相关资源
最近更新 更多