[英]a matrix as an argument of a function in c not working( making a game grid)
我是 c 的新手,我想插入此代码以生成游戏网格,当我运行它而不将其放入 function 时它可以工作,当我在 void function 中声明它时,格栅('0',矩阵 [36 ][36]) 导致问题我不知道为什么!
这是程序的主要 function:
#include "GO_c1.c"
#include "switch_grille.c"
#include <time.h>
#include <string.h>
int main()
{
char matrice[36][36];
grille('0',matrice[36][36]);
return 0;
}
这是function中网格的代码:
#include "Go_1.h"
#define dimension 9
#define spaceleft 2
void grille(char t,char mat[][36])
{
int a=0;
int b=1;
const int ligne_colonne=25;
printf(" "); /*2 spaces*/
for(int j=0;j<dimension;j++)
{
printf("%c", 'A'+a);
a++;
for (int k=0; k<(2*spaceleft)-1;k++)
{
printf(" "); /*5 spaces 5= 2*spaceleft-1 */
}
}
printf("\n");
for (int i=0;i<ligne_colonne;i++) /* ligne_colonne in this case= 25*/
{
if (i%spaceleft==0)
{
printf("%d ",b);
b++;
}
else
printf(" ");
for (int j=0; j<ligne_colonne;j++)
{
if ((i%spaceleft==0)&&(j%spaceleft==0))
{
mat[i][j]= t;
printf("%c ",mat[i][j]));
}
else if ((i%spaceleft==0)&&(j%spaceleft!=0))
printf("* ");
else if ((i%spaceleft!=0)&&(j%spaceleft!=0))
printf(" ");
else if ((i%spaceleft!=0)&&(j%spaceleft==0))
printf("* ");
}
printf("\n");
}
return;
}
虚空中格子的代码 function
#include <stdio.h>
#include <stdlib.h>
#define dimension 9
#define spaceleft 2
int main()
{
char matrice[36][36];
int a=0, b=1;
const int ligne_colonne = (spaceleft-1)*(dimension-1)+ dimension;
printf(" "); /*2 espaces*/
for(int j=0;j<dimension;j++)
{
printf("%c", 'A'+a);
a++;
for (int k=0; k<(2*spaceleft-1);k++)
{
printf(" "); /*5 espaces 5= 2*spaceleft-1 */
}
}
printf("\n");
for (int i=0;i<ligne_colonne;i++) /* ligne_colonne dans ce cas = 25*/
{
if (i%spaceleft==0)
{
printf("%d ",b);
b++;
}
else
printf(" ");
for (int j=0; j<ligne_colonne;j++)
{
if ((i%spaceleft==0)&&(j%spaceleft==0))
{
matrice[i][j]='0';
printf("%c ",matrice[i][j]);
}
else if ((i%spaceleft==0)&&(j%spaceleft!=0))
printf("* ");
else if ((i%spaceleft!=0)&&(j%spaceleft!=0))
printf(" ");
else if ((i%spaceleft!=0)&&(j%spaceleft==0))
printf("* ");
}
printf("\n");
}
return 0;
}
我尝试了一个矩阵形式 [][],然后是一个指针,但是当在 function 中设置网格代码时,所有解决方案都不起作用。
这是一个例子:
/*
* Example 2-D matrix
*
* Compile:
* gcc -g -Wall -pedantic -o xx xx.c
* Example output:
* 1 2 3
* 3 4 5
* NOTES:
* - By convention, symbols you "#define" should be in UPPER CASE
* - Don't specify the array bounds when you call your function.
* - If you don't specify the first array bound in your function prototype/function declaration...
* then be sure to pass it as a parameter to your function.
*/
#include <stdio.h>
#define HEIGHT 2
#define WIDTH 3
void process_matrix(int m[][WIDTH], int h);
int main(int argc, char *argv[])
{
int matrix[HEIGHT][WIDTH] = {
{1, 2, 3},
{3, 4, 5}
};
process_matrix(matrix, HEIGHT);
return 0;
}
void process_matrix(int m[][WIDTH], int h)
{
for (int i=0; i < HEIGHT; i++) {
for (int j=0; j < WIDTH; j++) {
printf ("%d ", m[i][j]);
}
printf ("\n");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.