c - 打印指向结构体的指针的二维数组

标签 c arrays pointers struct

我昨天一直在做作业,我已经完成了大部分,但没能做主要的事情。我不知道为什么它不起作用我问过其他学生,但没有人知道问题出在哪里。基本上这个节目是一个小游戏,有18名选手,每队9人。程序随机给玩家坐标和方向,然后他们开始移动。我基本上已经完成了程序,但是我的字段有问题,它根本没有显示玩家。 我尝试了很多事情,在测试时发现它甚至不打印我编写的 if 语句中的测试字符串。当我写这部分时 field[i][j] = &players[k][0];我已经检查过 field[i][j] 是否真的获得了 x 和 y 坐标,是的,确实如此。但在 print_field 类中,它将 field[][] 视为 null 并且该字段为空。 Players 是一个结构体数组。 field 是指向玩家的指针数组或 NULL。

我已经尝试了所有的知识,但无法做得更好。 这段代码有什么问题?为什么没有显示场上的球员?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h> 
#define LENGTH 25 
#define WIDTH 25 
enum direction {Right, Left, Up, Down};             /*Right = 0, Left = 1, Up = 2, Down = 3*/

void print_field();
void random_positions();
void playerdirection();
void motion();
void game();

struct player
{
 char *dora;      
 char *team;
 char *name;       //string?
 int x,y;          //coordinates       
 int direction;      
};
typedef struct player Player;
struct player *field[WIDTH][LENGTH];
Player players[8][1];
int main()
{   
    srand (time(NULL));
    int i;              //players 9 in each team  team1 = 0  team2 = 1
    players[0][0].name =  "A1";
    players[1][0].name =  "A2";
    players[2][0].name =  "A3";
    players[3][0].name =  "A4";
    players[4][0].name =  "A5";
    players[5][0].name =  "A6";
    players[6][0].name =  "A7";
    players[7][0].name =  "A8";
    players[8][0].name =  "A9";
    players[0][1].name =  "B1";
    players[1][1].name =  "B2";
    players[2][1].name =  "B3";
    players[3][1].name =  "B4";
    players[4][1].name =  "B5";
    players[5][1].name =  "B6";
    players[6][1].name =  "B7";
    players[7][1].name =  "B8";
    players[8][1].name =  "B9";
    for(i = 0; i < 9 ; i++)
    {
          players[i][0].team = "Team A";
          players[i][1].team = "Team B";
          players[i][0].dora = "Alive";
          players[i][1].dora = "Alive";     
    }
    random_positions();
    playerdirection();
    print_field();
    motion (Player player);
    print_field();
    game();       
    return 0;
}

void random_positions()
{
     int i,j,k;
     int xs[17],ys[17];
     for(i= 0; i<9 ; i++)
     {
      players[i][0].x = rand() % 25;
      players[i][0].y = rand() % 25;
      players[i][1].x = rand() % 25;
      players[i][1].y = rand() % 25;
      printf("A%d x = %d y = %d \n",i+1,players[i][0].x,players[i][0].y);
      printf("B%d x = %d y = %d \n",i+1,players[i][1].x,players[i][1].y);          
     }
     for(i = 0; i < 9 ; i++)
     {
           xs[i] = players[i][0].x; 
           xs[i+8] = players[i][1].x;
           ys[i] = players[i][0].y; 
           ys[i+8] = players[i][1].y;
           for(j = 0; j <= i ; j++)
           {
                 //printf("j%d start\n",j);
                 if(i != j && xs[i] == xs[j])
                 {
                      //printf("i%d start\n",j);
                      if(ys[i] == ys[j])
                      {
                               return random_positions();                                    
                      }
                      //("j%d done\n",j);
                 }
                 //printf("j%d done\n",j);
           }  
     }
     for(i = 0; i < 25; i++)
         {
              for(j = 0; j < 25; j++)
              {
                    for(k = 0; k < 9; k++)
                    {
                          if(i == players[k][0].x && j == players[k][0].y)
                          {
                               field[i][j] = &players[k][0];
                          }
                          if(i == players[k][1].x && j == players[k][1].y)
                          {
                               field[i][j] = &players[k][1];
                          }
                          else field[i][j] = NULL;                             //I da J sheidzleba shesacvleli iyos
                    }           
              }      
         }    
}     

/*this function prints out the given state of the field*/
void print_field(){
int i,j;
printf("\n");
printf("|0 1 2 3 4 5 6 7 8 9 101112131415161718192021222324|\n"); /*just to show easier the allignment*/
for(j=0; j<WIDTH+2; j++)        /*This first loop goes through row and creates them each by each*/
{
         if(j == 0 || j == WIDTH +1)         /*creates the upper and lower part of the field*/
              for(i=0; i<LENGTH+2; i++)       /*there should be space for frame so I added 2 to LENGTH in the loop*/
              {        
                  if(i==0) 
                  printf("-");
                  else if(i == LENGTH+1)
                  printf("-\n");
                  else printf("--");        /*3 decimals*/
              }
         else 
              for(i=0; i<LENGTH+2; i++)     /*Goes through the columns in this row and creates either frame or puts the nodeid*/
              {
                  if(i==0)printf("|");      /*frame*/
                  else if(i == LENGTH+1) printf("| %d\n",(j-1));  /*frame*/
                  else if(field[j-1][i-1] != NULL) 
                  {
                       printf("aaa");
                       printf("%-*s",2,(*field[j-1][i-1]).name);       /*putting nodeid 3 decimals*/
                  }
                  else printf("  "); 
              }         
}
printf("\n");
} 

最佳答案

您需要 Player[9][2] 而不是 Player[8][1]。您应该使用数组的长度来初始化数组,尽管您只能访问 length - 1 以内的索引,因为数组是从 0 开始索引的。

关于c - 打印指向结构体的指针的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21532962/

相关文章:

java - 将值从公共(public)方法传递到私有(private)数组

c++ - 双指针和它们之间的括号

c++ - 为什么内存地址是偶数?

c - 动态内存分配的流行用法

c - 两个随机数组

c - 链接问题 : i386:x86-64 architecture of input file *. o 与 i386 输出不兼容

arrays - 排序数组项后 knockout js强制重新绑定(bind)

c - 指向 NULL 的结构初始化指针

c - 结构中的内存分配

c - C 中返回函数指针的函数指针的语法