簡體   English   中英

在為2D數組分配空間時,程序卡住了

[英]Programm is stuck while allocating space for a 2d array

嗨,我有一個問題,我需要為2D數組分配空間,但不知何故卡住了。 之后,它應該進入for循環,但永遠不會到達那里。 有人知道為什么嗎?

  int len = read_file("staedte.csv", staedte, laender, bewohner);

  char **resultat;
  int resultatzaehler = 0;
  resultat =(char **) malloc (100 * sizeof(char));
  if(resultat == NULL){
    printf("Malloc failed to allocate space");
    exit(1);
  }
  for(int i = 0; i < 100; i++){
    resultat[i] =(char *) malloc (100);
    if(resultat[i] == NULL){
      printf("Malloc failed to allocate spacce 2");
      exit(1);
    }
  }

您應該通過使用進行分配

resultat = (char**) malloc(100 * sizeof(char *))
for(i = 0; i < 100; i++) {
resultat[i] = (char*) malloc(100 * sizeof(char))
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM