当函数打印指针数组的内容时,c 程序崩溃

c program crashes when a function prints the contents of array of pointers

我正在学习 C。我编写了我的程序来从文本文件中读取数据并将其打印出来。在构建过程中编译器没有给出错误。但是,当我尝试 运行 时程序崩溃了。我检查了当删除 printTable 函数时,程序 运行s 并在 main() 循环中打印内容。我还尝试删除变量“total”,但程序仍然崩溃。有什么问题?

#include<stdio.h>
#include<stdlib.h>

typedef struct{
char *country;
int females;
int males;
}person_count;

void printTable(person_count *data[]){
    printf("Country  Females  Males  Total\n");
    int i;
    int total[158];
    for(i=0;i<158;i++){

        total[i]=data[i]->females+data[i]->males;

        printf("%15s  %7i  %7i  %8i\n",data[i]->country,data[i]->females,data[i]->males,total[i]);
    }
}


int main(){
    person_count *data[158];

    FILE *eduData=fopen("SecondaryEd2005.txt","r");
    if(eduData==NULL){
        printf("File not found.");
        exit(1);
    }
    printf("File opened successfully.\n");

    int k;
    for(k=0;k<158;k++){
        data[k]=malloc(sizeof(person_count));
        char place[20];
        fscanf(eduData,"%s %i %i\n",place,&data[k]->females,&data[k]->males);
        data[k]->country=place;
        printf("%s %i %i\n",data[k]->country,data[k]->females,data[k]->males);
    }

    printTable(data);

    fclose(eduData);

    return 0;
}

txt 文件内容示例:

比利时 391138 423401 伯利兹 15591 15786 贝宁 154266 281183 百慕大 2494 2262 不丹 19870 22274

您应该更改 person_count 结构:

typedef struct{
    char country[20];
    int females;
    int males;
}person_count;

data[k]->country=place; 应替换为 memcpy(data[k]->country, place, 20*sizeof(char));

WhozCraig 在他的评论中给了你答案。您的代码复制了一个在下一次循环迭代中丢失的指针。

请注意,只有当您所有的国家/地区都少于 19 个字符时,此代码才有效。您也可以坚持使用 char* 指针,但在这种情况下,您将不得不分配内存并在使用后释放它。

我为你修改了你的程序。最重要的是让 country 成为固定长度的字符数组,而不是指针。另外,请确保释放分配给程序的所有内存,否则会产生内存泄漏。

#include <stdio.h>
#include <stdlib.h>

typedef struct{
  char country[20]; //fixed to reflect country field better
  int females;
  int males;
}person_count;

void printTable(person_count *data[],int items){
  items++;
  int i,total[items]; //replaced fixed value with variable to prevent excessive empty rows from printing
  printf("Country  Females  Males  Total\n");
  for(i=0;i<items;i++){
  total[i]=data[i]->females+data[i]->males;
  printf("%15s  %7i  %7i  %8i\n",data[i]->country,data[i]->females,data[i]->males,total[i]);
  }
}


int main(){

  FILE *eduData=fopen("SecondaryEd2005.txt","r");
  if(eduData==NULL){
  printf("File not found.");
  exit(1);
  }
  printf("File opened successfully.\n");

  int k,fscanret;
  person_count* data[159]; //added 1 to prevent out of bounds memory access

  for(k=0;k<158;k++){
data[k]=malloc(sizeof(person_count));
fscanret=fscanf(eduData,"%s %i %i\n",data[k]->country,&data[k]->females,&data[k]->males);
if (fscanret==EOF){ //check return value to avoid going past end of file
  free(data[k]); //discard empty record and free memory if EOF reached
  k--;
  break;
}
printf("%s %i %i\n",data[k]->country,data[k]->females,data[k]->males);
  }
  fclose(eduData);

  printTable(data,k); //print k number of entries.

  while (k>=0){
free(data[k]); //free memory for all entries. DONT FORGET THIS
k--;
  }

  return 0;
}