Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中,char foo[foo]是不允许的,但是为什么呢?以及如何使用输入作为此[foo]的声明_C_Char_Declare - Fatal编程技术网

在C语言中,char foo[foo]是不允许的,但是为什么呢?以及如何使用输入作为此[foo]的声明

在C语言中,char foo[foo]是不允许的,但是为什么呢?以及如何使用输入作为此[foo]的声明,c,char,declare,C,Char,Declare,我是C语言的新手,我已经习惯了zx basic和z80 asm。我想使用输入的值来创建字符数组,但是 int amount_of_dice_sides ; printf("Give number of sides of dice:\n") ; scanf("%d", &amount_of_dice_sides) ; char all[amount_of_dice_sides] ; 现在还不允许 int amount_of_dice_si

我是C语言的新手,我已经习惯了zx basic和z80 asm。我想使用输入的值来创建字符数组,但是

int   amount_of_dice_sides ;
printf("Give number of sides of dice:\n") ;
scanf("%d", &amount_of_dice_sides) ;
char  all[amount_of_dice_sides] ;
现在还不允许

int   amount_of_dice_sides=6 ;
char  all[6] ;
很好用。。。。我怎么知道 char all[骰子边的数量]; 工作 这是一个非常初级的问题,非常“基本”,但我似乎找不到一个不以“const char*foo之间的差异”开头的答案 这与使用整数设置字符无关,只是作为实值而不是变量。 我发现这些可能的“char foo”,但没有一个是我想要实现的

编辑 我使用另一个编译器,然后是gcc

#include <stdio.h>
void main ()
    {
int   amount_of_dice_sides ;
printf("Give number of sides of dice:\n") ;
scanf("%d", &amount_of_dice_sides) ;
char  all[amount_of_dice_sides] ;
return ;
    }
#包括
空干管()
{
骰子边的整数数量;
printf(“给出骰子的边数:\n”);
scanf(“%d”和骰子边的数量);
char all[骰子边的数量];
返回;
}
工作正常,但与zcc无关。 所以我不得不去另一个地方,我真的还在学习。
再见,谢谢你的回答。chris

C仅允许在某些情况下使用可变长度堆栈数组。特别是当您有一个函数时,可以使用基于参数的计算来分配堆栈数组

void func(int param){
    int array[param] // call it here
    // do some work with array as a scratch space
}   // when the final curly bracket is called, the array is destroyed

然而,这种技术很容易让你陷入麻烦。如果您没有指定数组的长度,并且用户输入了一个非常大的数字,那么就会出现堆栈溢出。在早期的C编译器和平台(如Intel-8051)上,这种情况非常普遍,而且调用了太多嵌套函数

解决方案是在堆上分配可变长度数组。有些平台也允许分配到堆栈,但并非所有平台都允许

// In sumfunc we roll a number of dice, print, and return the sum
// Malloc allocates to the heap
int sumFunc_malloc(int num_dice){
    int * dice = (int*)malloc(sizeof(int) * num_dice); // our allocation
    int sum = 0;
    for (int i = 0; i < num_dice; i++){
        dice[i] = (rand() % 6) + 1;
        printf("Dice %d rolled a %d", i, dice[i];
        sum += dice[i];
    }
    free(dice); // REQUIRED or memory will leak, and eventually program will fail
    return sum;
}

// In sumfunc we roll a number of dice, print, and return the sum
// Alloca is not supported by all platforms, but is on the stack
int sumFunc_alloca(int num_dice){
    int * dice = (int*)alloca(sizeof(int) * num_dice); // our allocation on the stack
    int sum = 0;
    for (int i = 0; i < num_dice; i++){
        dice[i] = (rand() % 6) + 1;
        printf("Dice %d rolled a %d", i, dice[i];
        sum += dice[i];
    }
    return sum;
}
在本宣言中

int   amount_of_dice_sides ;
printf("Give number of sides of dice:\n") ;
scanf("%d", &amount_of_dice_sides) ;
char  all[amount_of_dice_sides] ;
所有的[amount\u of_dice\u sides]都是一个静态数组&编译器在构建代码之前需要知道它的大小

在您的例子中发生的是,在编译之后,数组的大小作为用户的输入被读取


始终建议明确提及数组的大小,以避免以后在代码中出现内存问题。

它不起作用的原因是您使用的是侏罗纪时期的编译器和MCU。可变长度数组(VLA)于1999年在C语言中引入。Z80可能是学习汇编语言的一个很好的平台,但它是学习C语言的一个可怕的平台

但无论如何,在低端微控制器上永远不应该使用VLA分配或堆分配,因为它们通常内存非常有限,特别是堆栈内存。(而且堆分配也很简单。)


出于同样的原因,您也不应该使用
stdio.h
,它会占用大量可用内存。相反,您应该使用原始UART驱动程序或自己编写一个驱动程序-假设stdio.h函数到达PC上的终端。首先查找链接器映射文件,研究它,看看这些LIB实际占用多少内存,然后你自己会明白为什么它们不合适。

最好的注释是解释为什么部分:编译将把内存设置在一个永久位置,如果我以后更改这个大小,C将在分配变量时遇到麻烦

同时,我确实在一个叫迪诺萨的游戏中学习了C语言。我花了一段时间才使用它。z88dk正在缓慢地从“经典”改写为SDCC和其他版本

这一个在zcc中工作,但在构建z88dk本身(!!!)并使用zx频谱的特殊库时,BUILD_SCC=1:

 /* C source start */
 /* by chris born ad 2020 as homework for z88dk, farkle */
 /* zcc +zx -vn -startup=0 -clib=sdcc_iy dice_026.c -o dice026 -create-app */

  #include <arch/zx.h>
  #include <stdio.h>
  #include <stdlib.h>

  int dice(int aods)
          {
  char d ;
    /*srand (seedrandom); */
      d =+ rand()%aods ;
      return d;
           }
    
  int main()
   {

   int x  , amount_of_dice_sides , hand_of_dice ;
   char number , hand[256] ;

   zx_cls(PAPER_WHITE) ;

   printf("Give number of sides of dice:\n") ;
   scanf("%d", &amount_of_dice_sides) ;
 
   printf("how many dice do you want:\n") ;
   scanf("%d", &hand_of_dice) ;
   printf(" amount of dice %d ",hand_of_dice );
   if (hand_of_dice > 255) 
          {
          hand_of_dice = 255 ;
          printf(" is now %d", hand_of_dice) ;
          }
   printf("\n") ;

   scanf("press a key") ;

 /* char   hand[&hand_of_dice];     */ /* int is max 65536 gives a huge hand     filled with stones */

   printf("You threw: \n");

   for (x=1 ; x<=hand_of_dice ;x++) 
        {
        number=dice(amount_of_dice_sides);
        printf("stone %d : %d  \n",x , number);
        hand[x]=number;
        }
        printf("\n");
   return 0;
   }

   /* C source end */
/*C源代码启动*/
/*克里斯·伯恩(chris born)于2020年为法克尔z88dk做家庭作业*/
/*zcc+zx-vn-startup=0-clib=sdcc_iy dice_026.c-o dice026-创建应用程序*/
#包括
#包括
#包括
整数骰子(整数aods)
{
chard;
/*srand(seedrandom)*/
d=+rand()%aods;
返回d;
}
int main()
{
整数x,骰子的边数,骰子的手数;
字符数,手[256];
zx_cls(纸白色);
printf(“给出骰子的边数:\n”);
scanf(“%d”和骰子边的数量);
printf(“您想要多少个骰子:\n”);
scanf(“%d”和手掷骰子);
printf(“骰子数量%d”,手上的骰子数量);
如果(骰子之手>255)
{
掷骰子的手=255;
printf(“现在是%d”,骰子的手);
}
printf(“\n”);
scanf(“按键”);
/*char hand[&hand_of_dice];*/*int的最大值为65536,表示一只装满石头的大手*/
printf(“您抛出:\n”);

对于(x=1;x
char all[amount of dice\u sides];
正在定义一个可变长度数组,它(现在)是C的可选功能,并非所有编译器都支持。另一种方法是动态分配内存,例如
char*all=malloc(amount of dice\u sizeof*all);
没有
char foo[foo]
在您的代码中。
char all[骰子边的数量]
非常不同,被称为VLA(可变长度数组),是C99中的一项新功能。与动态分配内存相比,这不是一个好主意。另一种方法是定义一个数组,允许最大数量的骰子边,例如
char all[20];
并将输入限制为
20
@phuclv char foo[foo]当然是递归的,这不是我要说的,所以标题确实不正确。当有人输入-1时,你期望会发生什么?在微控制器上,正确的解决方案是分配静态所需的内存。例如,你可以使用静态内存而不是堆分配来创建简单的内存池指定平台。他说他以前习惯在Zilog上编程。如果他在Zilog上运行C,那么他肯定不应该分配内存。但是,如果他在Intel上?了解分配是成为一名更好的程序员的一项要求。我是说你的“抓挠”示例应该根据需要分配内存,而不是使用多个functi之间共享的一些奇怪的通用缓冲区
 /* C source start */
 /* by chris born ad 2020 as homework for z88dk, farkle */
 /* zcc +zx -vn -startup=0 -clib=sdcc_iy dice_026.c -o dice026 -create-app */

  #include <arch/zx.h>
  #include <stdio.h>
  #include <stdlib.h>

  int dice(int aods)
          {
  char d ;
    /*srand (seedrandom); */
      d =+ rand()%aods ;
      return d;
           }
    
  int main()
   {

   int x  , amount_of_dice_sides , hand_of_dice ;
   char number , hand[256] ;

   zx_cls(PAPER_WHITE) ;

   printf("Give number of sides of dice:\n") ;
   scanf("%d", &amount_of_dice_sides) ;
 
   printf("how many dice do you want:\n") ;
   scanf("%d", &hand_of_dice) ;
   printf(" amount of dice %d ",hand_of_dice );
   if (hand_of_dice > 255) 
          {
          hand_of_dice = 255 ;
          printf(" is now %d", hand_of_dice) ;
          }
   printf("\n") ;

   scanf("press a key") ;

 /* char   hand[&hand_of_dice];     */ /* int is max 65536 gives a huge hand     filled with stones */

   printf("You threw: \n");

   for (x=1 ; x<=hand_of_dice ;x++) 
        {
        number=dice(amount_of_dice_sides);
        printf("stone %d : %d  \n",x , number);
        hand[x]=number;
        }
        printf("\n");
   return 0;
   }

   /* C source end */