簡體   English   中英

盡管有參數聲明符,但 Scanf 給出“預期的參數聲明符”錯誤

[英]Scanf giving “expected parameter declarator” error despite having parameter declarator

我正在嘗試使用 scanf 從輸入創建全局變量。 我有這個代碼來做到這一點:

#include <stdio.h>
#include <string.h>

/* innitialize our default global variables */
int height[2] = {0, 0}; /*verticle height will be the first number when splitting, aka the number of rows*/
int barrier = 0;
int fill = 0;
int point[2] = {0, 0}; /*verticle height, aka row number, will be the first number*/

/*set the variables*/
scanf("%d %d", &height[0], &height[1]);
int array[height[0]][height[1]]; /*create array with designated size*/
scanf("%d", &barrier); /*set the barrier*/
scanf("%d", &fill); /*set the fill*/
scanf("%d %d", &point[0], &point[1]); /*set the point*/

但是我在制作時會產生這些錯誤:

prog0.c:11:7: error: expected parameter declarator
scanf("%d %d", &height[0], &height[1]);
      ^
prog0.c:11:7: error: expected ')'
prog0.c:11:6: note: to match this '('
scanf("%d %d", &height[0], &height[1]);
     ^
prog0.c:11:1: error: type specifier missing, defaults to 'int' [-Werror,-Wimplicit-int]
scanf("%d %d", &height[0], &height[1]);
^
prog0.c:11:1: error: conflicting types for 'scanf'
/usr/include/stdio.h:446:24: note: previous declaration is here
extern int __REDIRECT (scanf, (const char *__restrict __format, ...),
                       ^
prog0.c:12:5: error: variable length array declaration not allowed at file scope
int array[height[0]][height[1]]; /*create array with designated size*/
    ^     ~~~~~~~~~
prog0.c:13:7: error: expected parameter declarator
scanf("%d", &barrier); /*set the barrier*/
      ^
prog0.c:13:7: error: expected ')'
prog0.c:13:6: note: to match this '('
scanf("%d", &barrier); /*set the barrier*/
     ^
prog0.c:13:1: error: type specifier missing, defaults to 'int' [-Werror,-Wimplicit-int]
scanf("%d", &barrier); /*set the barrier*/
^
prog0.c:13:1: error: conflicting types for 'scanf'
/usr/include/stdio.h:446:24: note: previous declaration is here
extern int __REDIRECT (scanf, (const char *__restrict __format, ...),
                       ^
prog0.c:14:7: error: expected parameter declarator
scanf("%d", &fill); /*set the fill*/
      ^
prog0.c:14:7: error: expected ')'
prog0.c:14:6: note: to match this '('
scanf("%d", &fill); /*set the fill*/
     ^
prog0.c:14:1: error: type specifier missing, defaults to 'int' [-Werror,-Wimplicit-int]
scanf("%d", &fill); /*set the fill*/
^
prog0.c:14:1: error: conflicting types for 'scanf'
/usr/include/stdio.h:446:24: note: previous declaration is here
extern int __REDIRECT (scanf, (const char *__restrict __format, ...),

並且在因太多錯誤而超時之前,它會繼續為每個 scanf 重復這些錯誤。 研究 scanf 和格式化,據我所知,我做得正確。 我最好的猜測是 scanf 在全局 scope 上不起作用。 這是問題嗎?

你忘了main的 function。 這是初學者的 C 教科書的第一章。 沒有main function 沒有程序代碼可以運行。

你可能想要這個:

#include <stdio.h>
#include <string.h>

/* innitialize our default global variables */
int height[2] = { 0, 0 }; /*verticle height will be the first number when splitting, aka the number of rows*/
int barrier = 0;
int fill = 0;
int point[2] = { 0, 0 }; /*verticle height, aka row number, will be the first number*/


int main()
{
  /*set the variables*/
  scanf("%d %d", &height[0], &height[1]);
  int array[height[0]][height[1]]; /*create array with designated size*/
  scanf("%d", &barrier); /*set the barrier*/
  scanf("%d", &fill); /*set the fill*/
  scanf("%d %d", &point[0], &point[1]); /*set the point*/
}

暫無
暫無

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

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