C - 警告:格式 '%s' 需要类型 'char *',但参数 2 的类型为 'char (*)[2000]'



所以我在 Linux 上使用 C 为生命游戏编写这段代码,但我收到了这个警告! 此警告是什么意思,我该如何解决?我写的代码是:

#include <stdio.h>
#include <string.h>
#include <omp.h>
#include <stdlib.h>
#include <assert.h>
#define MAX_N 2000
int plate[2][(MAX_N + 2) * (MAX_N + 2)];
int which = 0;
int n;
int live(int index){
return (plate[which][index - n - 3] 
+ plate[which][index - n - 2]
+ plate[which][index - n - 1]
+ plate[which][index - 1]
+ plate[which][index + 1]
+ plate[which][index + n + 1]
+ plate[which][index + n + 2]
+ plate[which][index + n + 3]);
}
void iteration(){
#pragma omp parallel for schedule(static)
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
int index = i * (n + 2) + j;
int num = live(index);
if(plate[which][index]){
plate[!which][index] =  (num == 2 || num == 3) ?
1 : 0;
}else{
plate[!which][index] = (num == 3);
}
}
}
which = !which;
}
void print_plate(){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
printf("%d", plate[which][i * (n + 2) + j]);
}
printf("n");
}
printf("");
}
int main(){
int M;
char line[MAX_N];
memset(plate[0], 0, sizeof(int) * (n + 2) * (n + 2));
memset(plate[1], 0, sizeof(int) * (n + 2) * (n + 2));
if(scanf("%d %d", &n, &M) == 2){
for(int i = 1; i <= n; i++){
scanf("%s", &line);
for(int j = 0; j < n; j++){
plate[0][i * (n + 2) + j + 1] = line[j] - '0';
}
}
for(int i = 0; i < M; i++){
iteration();
}
print_plate();
}
return 0;
}

如果您能帮助我修复,将不胜感激,因为我认为这应该可以正常工作。

你有这个:

scanf("%s", &line);

line属于类型char[2000](MAX_N(。 通过获取它的地址运算符,您将获得一种char(*)[2000]。 摆脱&,取而代之的是char[2000]型,它将衰减到您需要的char*

代码中存在一些错误:

  1. 您正在尝试通过在scanf()函数中寻址来扫描变量line。如果您从那里删除与号&符号,则可以解决此问题。

    解释答案虽然已经在这个问题的第一个答案中提供了。

  2. 使用该语句:

    printf(""); // format contains a (null)
    

    完全没有意义。您正在尝试打印不存在的内容 - 空值。

  3. 编译指示:

    #pragma omp parallel for schedule(static)
    

    将根据-Wunknown-pragmas标志被忽略。

最新更新