char()表示数组

时间:2016-10-11 18:57:29

标签: c arrays char

我有一个程序,我必须创建一个用户输入1到99之间的奇数,并创建了一个魔术广场,我已成功完成。

#include <stdio.h>

int main()
{
  int n;

  printf("\nThis programs creates a magic squares of a specified size.\n");
  printf("The size must be an odd number between 1 and 99.\n");
  printf("Enter the size of magic square: ");
  scanf("%d", &n);


  int magicsq[99][99];

  int row = 0;   
  int col = (n - 1) / 2;
  magicsq[row][col] = 1;

  int i;     
  for(i = 2; i <= n * n; i++)
    {
      row = (row + n - 1) % n;

      /*  printf("i = %d\n", i);
      printf("row %d\n", row);

       col = (col % n); */
      col = (col + 1) % n;

      /* printf("col %d\n\n", col); */

      if(magicsq[row][col] != 0)
    { 
      row = (n + row + 2) % n;
      col = (n + col - 1) % n;
      /* printf("n = %d ; row = %d ; col = %d\n", n, row, col); */
    }

      magicsq[row][col] = i;
    }




  printf("\n");
  int j;    
  for (i = 0; i < n; i++)
    {                                                     
      for (j = 0; j < n; j++)
    {                                                 
      printf("%5d", magicsq[i][j]);                                        
    }                                                                       
      printf("\n");                                                             
    }                                                                           
  return 0;                                                                     
}  

我遇到了另一个问题,它说我要介绍两个函数,void create_magic_square(int n, char magic_square[99][99])void print_magic_square(int n, char magic_square[99][99])

#include <stdio.h>


void create_magic_square(int n, char magic_square[99][99]);
void print_magic_square(int n, char magic_square[99][99]);

int main()
{
  int n;
  char **magic_square;

  printf("\nThis programs creates a magic squares of a specified size.\n");
  printf("The size must be an odd number between 1 and 99.\n");
  printf("Enter the size of magic square: ");
  scanf("%d", &n);

  create_magic_square(n, magic_square[99][99]);
  print_magic_square(n,  magic_square[99][99]);

  return 0;
}

void create_magic_square(int n, char magic_square[99][99])
{

  int *magicsq[][];
  magic_square[99][99] = magicsq[][];
  int row = 0;   
  int col = (n - 1) / 2;
  magicsq[row][col] = 1;

  int i;     
  for(i = 2; i <= n * n; i++)
    {
      row = (row + n - 1) % n;

      printf("i = %d\n", i);
      printf("row %d\n", row);

      /* col = (col % n); */
      col = (col + 1) % n;

      printf("col %d\n\n", col);

      if(magicsq[row][col] != 0)
    { 
      row = (n + row + 2) % n;
      col = (n + col - 1) % n;
      printf("n = %d ; row = %d ; col = %d\n", n, row, col);
    }

      magicsq[row][col] = i;
    }
}



void print_magic_square(int n, char magic_square[99][99])
{     
  printf("\n");
  int j;    
  for (i = 0; i < n; i++)
    {                                                     
      for (j = 0; j < n; j++)
    {                                                 
      printf("%5d", magicsq[i][j]);                                        
    }                                                                       
      printf("\n");                                                             
    }
}

编译完成后,我对char类型数组的声明和参数的使用充满了大量的错误。我用googled char()参数和类型,但我无法合并到我的程序中。

我是新手,所以如果我做错了什么,那么建设性的批评会受到赞赏并帮助我更好地学习。

语言:c99;编译器:gcc

1 个答案:

答案 0 :(得分:0)

当然,您的代码中存在很多与指针和数组相关的错误,而这些错误在单个答案中无法完全解释。 我建议你正确研究这些指针。指针和数组之间的关系,在尝试做你想做的事情之前,都需要彻底理解多维数组。