简体   繁体   English

段故障和引用结构数组

[英]Segfaults and referencing struct arrays

I've got a project that involves creating a text game. 我有一个涉及创建文字游戏的项目。 I'm creating a struct for each player and putting them in an array. 我正在为每个玩家创建一个结构,并将它们放置在数组中。 I'm then trying to pass in data and then pass by pointer the array to other functions, however I keep on getting segmentation faults (Although on the odd occasion working fine). 然后,我尝试传递数据,然后通过指针将数组传递给其他函数,但是我不断遇到分段错误(尽管在少数情况下可以正常工作)。 I've summarised below. 我总结如下。

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

typedef struct
{
  char name[9];
  int cardsHeld;
  int hand[8];
} Player;

void printNames(Player** playerArray)
{
  for (int i = 0; i < 3; i++)
  {
    fprintf(stdout, "%s\n", playerArray[i]->name);
  }
}


void gamesetup()
{
  int count;

  fprintf(stdout, "How many players will be partaking in 'The Game'? ( 1 - 5)\n");
  fscanf(stdin, "%d", &count);

  Player** playerArray = (Player**)malloc(sizeof(Player*) * count);

  for(int i = 0; i < count; i++)
  {
    playerArray[i] = (Player*) malloc(sizeof(Player));
    fprintf(stdout, "Please enter the name for player %d.\n\n", i + 1);
    fscanf(stdin, "%s", playerArray[i]->name);
  }

  printNames(playerArray);
}

int main(int argc, char** argv)
{
  gamesetup();

  return 0;
}

My questions are; 我的问题是;

  1. Is the fscanf getting the address of the Player.name member? fscanf是否获取Player.name成员的地址? I'm getting confused whether the -> operator should deference the value of the struct member or since its in an array the address? 我感到困惑的是->运算符是否应该尊重struct成员的值,或者由于其在数组中的地址?

  2. I'm not sure why it works sometimes but not others. 我不确定为什么有时会起作用,但其他人却无法。 If it works sometimes fundamentally it should be ok. 如果有时可以正常工作,那应该没问题。 Is the malloc function allocating memory it should not or is the fscanf putting data in the wrong place. 是malloc函数不应该分配内存,还是fscanf将数据放在错误的位置。

Thank you. 谢谢。

-EDIT- -编辑-

Changed the code so it is in a complete program that appears to work without seg faults. 更改了代码,使其在一个完整的程序中似乎可以正常运行,而没有seg错误。 I think that my issues arise from not freeing the memory before termination is messing it up next time I run it without compiling first. 我认为我的问题是由于终止之前没有释放内存而导致下次我不先编译就运行它时弄乱了内存。 I'm still not sure why fscanf works as in my mind the argument playerArray[i]->name is returning the value, not the address. 我仍然不确定为什么fscanf起作用,因为在我看来,参数playerArray [i]-> name返回值,而不是地址。

I've worked it out where I was confused. 我已经解决了我困惑的地方。 Thank you for all your help in the comments. 感谢您在评论中的所有帮助。

The member I am accessing in my array is a string of chars so the first member is a pointer. 我在数组中访问的成员是一个字符串,所以第一个成员是指针。 By using fscanf(stdin, "%s",playerArray[i]->name); 通过使用fscanf(stdin,“%s”,playerArray [i]-> name); This deferenced the pointer (an address) so it works. 这与指针(地址)有关,因此可以正常工作。 I was getting in a muddle as it was an member of an array of structs. 我陷入了混乱,因为它是一系列结构的成员。 The segfaults were caused by me messing with the code to try and fix what already worked. 这些段错误是由于我弄乱了代码以尝试修复已经起作用的代码而引起的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM