首页 > 解决方案 > 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.

标签: clinuxunixparallel-processingopenmp

解决方案


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.


推荐阅读