c - warning: format '%s' expects type 'char *' but argument 2 has type 'char (*)[2000]'
问题描述
so I was writing this code for the game of life using C on linux but i got this warning! what does this warning mean and how can i fix it ?. The code i wrote is :
#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("\0");
}
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;
}
it would be so much appreciated if you could help me fix cause i think this should have work fine.
解决方案
You have this:
scanf("%s", &line);
line
is of type char[2000]
(MAX_N). By taking the address-of operator of it, you are getting a type of char(*)[2000]
. Get rid of the &
and instead you will have type char[2000]
which will decay to the char*
you need.
推荐阅读
- sql - 选择另一个表的数组或列的内容作为行标题
- mysql - 使用 Replace 函数的更新语句导致所有行的零值“0”
- go - 编码后如何限制JSON字符串的长度?
- javascript - 无法在 Angular 8 的父组件中显示指令组件 Html
- python - 在 Power BI 中编辑用作数据条目的 python 脚本
- python - 无法从父目录导入文件
- c++ - 对象中 vec3 的值
- performance - Spark Structured Streaming - WAL 性能下降
- sql-server - 当列是 XML 数据类型时的 where IN 子句
- regression - 在每个参与者进行多次试验的主题设计之间