I为记分牌创建了一个简单的组织功能,迭代器正在更改价值而没有分配

问题描述 投票:0回答:1

我假设我存在的问题是主要功能,为了汇编而存在其他功能。重新组织计分板数组时,for-loop的索引的更改值而无需分配。具体来说,我很好奇为什么x在输出中从9变为1,而我从8变为1852797506

#include <stdio.h>
#include <string.h>

struct player
  {
  int score;
  char name[20];
  };

void initializeArray(char *name, int arrayLength)
  {
  int i = 0;
  for(i; i < arrayLength; i++)
    {
    name[i] = '-';
    }
    name[i - 1] = '\0';
  }
  
  void initializeStruct(struct player *players)
  {
    players->score = 0;
    initializeArray(players->name, 20);
  }
  
  void fillStruct(struct player *players)
  {
  char names[9][20] = {"James", "Johnny", "Johnathan", "Ethan", "Robby", "Anthony", "Jeremy", "Scott", "Bronwyn"};
  int x = 9;
  for(int i = 0; i < 9; i++)
    {
    strcpy(players[i].name, names[i]);
    players[i].score = x - i;
    }
  
  }

  int main()
    {
  struct player players[9];
  initializeStruct(players);
  fillStruct(players);
  struct player one;
  one.score = 2;
  initializeArray(one.name, 20);
  strcpy(one.name, "Mathew");


    
    
    for(int i = 0; i < 9; i++)  
      {

      if(one.score > players[i].score)
        {
        for(int x = 8; x >= i; x--)
          {
          printf("x = %d i = %d\n", x, i);
          
          strcpy(players[x].name, players[x  - 1].name);
          
          printf("x = %d i = %d\n", x, i);
          
          (players[x].score) = (players[x - 1].score);
          
          printf("x = %d i = %d\n", x, i);
          }
        strcpy(players[i].name, one.name);
        players[i].score = one.score;
        break;
        }
      }
    }

    output:

    x = 9 i = 8
    x = 9 i = 1852797506
    x = 1 i = 1852797506

索引似乎没有任何作业的变化价值,我觉得我缺少某种隐性作业,我不知道。

i我也使用gdb进行调试,他们只是在玩家[x] .score = players [x -1] .score分配之后更改。

c variables indexing struct variable-assignment
1个回答
0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.