簡體   English   中英

警告:格式“%s”需要類型“char *”但參數 2 的類型為“char (*)[2000]”

[英]warning: format '%s' expects type 'char *' but argument 2 has type '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("\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;
}

如果您能幫我解決問題,我將不勝感激,因為我認為這應該可以正常工作。

你有這個:

scanf("%s", &line);

line的類型為char[2000] (MAX_N)。 通過使用它的地址運算符,您將獲得一種char(*)[2000] 擺脫& ,您將擁有類型char[2000] ,它將衰減為您需要的char*

代碼中有幾個錯誤:

  1. 您試圖通過在scanf() function 中尋址來掃描變量line 如果您從那里刪除&符號,則可以解決此問題。

    盡管已在此問題的第一個答案中提供了解釋性答案。

  2. 使用語句:

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

    完全沒有意義。 您正在嘗試打印不存在的東西 - null。

  3. 語用:

     #pragma omp parallel for schedule(static)

    根據-Wunknown-pragmas標志將被忽略。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM