#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int i, j,
matriz[4][4] = {0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0}, l, c,
matrizcontrolo[4][4] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
ponto = 0, ntentativa = 0;
const char array1[4][4] = {"0000", "0000", "0000", "0000"};
for (i = 0; i < 4; i++) {
do {
printf("Tentativa %d\n", ntentativa + 1);
do {
printf("Linha:\n");
scanf("%d", &l);
} while (l < 1 || l > 4);
do {
printf("Coluna:\n");
scanf("%d", &c);
} while (c < 1 || c > 4);
} while (matrizcontrolo[l][c] == 1);
if (matriz[l - 1][c - 1] == 0) {
printf("Splash!\n");
strcpy(array1[l - 1][c - 1], "x");
} else {
printf("Boom!\n");
ponto = ponto + 2;
i--;
strcpy(array1[l - 1][c - 1], "S");
}
matrizcontrolo[l][c] = 1;
ntentativa++;
}
printf("O campo de batalha foi\n");
for (i = 0; i < 4; i++) {
printf("\n");
printf("%s ", *array1[i]);
}
if (ponto == 8) {
printf("Parabéns, acertou em todos os barcos e a sua pontuação foi de %d",
ponto);
}
return 0;
}我收到一个错误,上面写着:
main.c:31:20:警告:传递‘strcpy’的参数1使指针从整数生成,而不进行强制转换。
/usr/include/string.h.h:129:14:注意:预期的‘char*限制’,但参数的类型是‘char’
main.c:37:20:警告:传递‘strcpy’的参数1使指针从整数生成,而不进行强制转换。
/usr/include/string.h.h:129:14:注意:预期的‘char*限制’,但参数的类型是‘char’
main.c:45:18:警告:格式‘’%s‘’期望类型为‘char*’的参数,但参数2的类型为‘int’-Wformat=
然后简单地说:
分段故障(堆芯倾弃)
当我试图通过在matriz变量中插入值来运行程序时。
有人能帮我吗?
提前感谢!
发布于 2021-02-09 05:20:37
坏消息是你用错了strcpy。好消息是你不用再用它了。
Fyi,strcpy需要一个非const目标缓冲区,足以存储要复制的源字符串,并包括一个用于终止nullchar的额外点。你的目标是一个char,而不是char*
此外,您的字符串缓冲区都比较小。字符串"0000"需要五个字符来存储:其中四个字符串,加上一个用于终止nullchar的字符串。
最后,字符串缓冲区数组不能是const。您将在这些行中更改数据。
这段代码不应该在任何地方使用strcpy。您真正要做的就是将终止字符串中的特定字符设置为特定值。C提供了一种实现这种功能的方法:赋值。程序的修改版本如下所示。我无法证明游戏是否正确,但它解决了您所报告的错误的具体问题,并向您展示了您真正想要的是如何直接的单元格分配。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i, j, l, c, ponto = 0, ntentativa = 0;
int matriz[][4] = {
{0, 0, 1, 1},
{0, 0, 0, 0},
{1, 1, 0, 0},
{0, 0, 0, 0}
};
int matrizcontrolo[][4] = {
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
char array1[][5] = {
"0000",
"0000",
"0000",
"0000"
};
for (i = 0; i < 4; i++)
{
do
{
printf("Tentativa %d\n", ntentativa + 1);
do
{
printf("Linha:\n");
scanf("%d", &l);
} while (l < 1 || l > 4);
do
{
printf("Coluna:\n");
scanf("%d", &c);
} while (c < 1 || c > 4);
} while (matrizcontrolo[l][c] == 1);
if (matriz[l - 1][c - 1] == 0)
{
printf("Splash!\n");
array1[l-1][c-1] = 'x';
}
else
{
printf("Boom!\n");
ponto = ponto + 2;
i--;
array1[l - 1][c - 1]= 'S';
}
matrizcontrolo[l][c] = 1;
ntentativa++;
}
printf("O campo de batalha foi\n");
for (i = 0; i < 4; i++)
{
puts(array1[i]);
}
if (ponto == 8)
{
printf("Parabéns, acertou em todos os barcos e a sua pontuação foi de %d",
ponto);
}
return 0;
}发布于 2021-02-09 04:58:46
老实说,我不知道你的程序背后的逻辑和目标到底是什么。但问题是显而易见的,它们都是可以解决的:
这个语法中的
strcpy(array1l - 1,"x");
当您使用两个下标运算符访问二维数组时,它不再被称为指针,而是类型char。然而,strcpy()向您询问以下参数:
char *strcpy(char *dest,const char *src);
您需要传递array1[] (char*),而不是array1[][] (char)。简单地说,您不需要strcpy()函数:
芳基-1= 'x';
0000提供4,不如将其增加1,即5,并与其他类型的char[]或char[]类似。matriz4 = {0,0,1,1,0,0,0,0,1,1,0,0,0,0,0,0}
你用不正确的方式初始化二维数组.你需要用嵌套的大括号来做:
matriz4 = {{0,0,1,1},{0,0,0,0},{1,1,0,0},{0,0,0,0}
对matrizcontrolo[][].也要这样做
printf(%s,*array1i);^^^___删除星号
https://stackoverflow.com/questions/66113153
复制相似问题