Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.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制作我自己的数组:_C - Fatal编程技术网

用c制作我自己的数组:

用c制作我自己的数组:,c,C,我是C语言的初学者。我想制作一个数组,它的大小将从用户的scanf函数中获取,因为数组可能有任何大小,而程序启动时不知道 以及如何输入数组元素: 我希望我的程序输出为: 请输入数组元素数:4 输入元素:12 43 5 6 您输入的元素是:12 43 5 6 有可能这样做吗?我怎样才能使我的输出像这样?是的,这是很有可能的。这称为动态内存分配。 您要做的是创建一个指针,然后分配它 稍后 指针访问的工作原理与静态数组一样,即数组[0]=任意值 编辑: 不要忘记在使用malloc()时应该包括stdl

我是C语言的初学者。我想制作一个数组,它的大小将从用户的scanf函数中获取,因为数组可能有任何大小,而程序启动时不知道

以及如何输入数组元素:

我希望我的程序输出为:

请输入数组元素数:4 输入元素:12 43 5 6 您输入的元素是:12 43 5 6


有可能这样做吗?我怎样才能使我的输出像这样?

是的,这是很有可能的。这称为动态内存分配。 您要做的是创建一个指针,然后分配它 稍后

指针访问的工作原理与静态数组一样,即数组[0]=任意值

编辑:


不要忘记在使用malloc()时应该包括stdlib.h。动态内存可以很好地满足您的需要,但是正如Gopi前面提到的,C99允许您直接使用堆栈

下面是另一个使用堆栈而不是堆内存的解决方案:

    #include <stdio.h>

    int     main(void)
    {
      int   nb;

      scanf("%d", &nb);
      printf("%d\n", nb);
      // declaration
      char  my_tab[nb];
      if  (nb > 2)
        {
          // I use my table as I want...
          my_tab[0] = 'a';
          my_tab[1] = 0;
          printf("%s\n", my_tab);
        }
      return (0);
    }
#包括
内部主(空)
{
int nb;
scanf(“%d”和&nb);
printf(“%d\n”,nb);
//声明
char my_tab[nb];
如果(nb>2)
{
//我用我的桌子,因为我想。。。
我的标签[0]=“a”;
my_tab[1]=0;
printf(“%s\n”,我的\u选项卡);
}
返回(0);
}

我希望这将帮助您更好地理解不同类型的内存分配。

一种简单的方法是,仅使用基本知识就可以将用户输入设置为变量,然后在描述数组大小时使用该变量:

#include <stdio.h>

int main(void)
{
    int arraysize, i;

    printf("Please input an array size.\n");
    scanf("%d", &arraysize); //Will set the user input and use it as array size
    getchar(); //Has user hit enter to continue
    i = arraysize; //This is for sheer convenience in the for() loop
    int array[i]; //This creates an array of the size inputted by the user
    printf("Please input %d numbers to fill the array.\n", i); 
    for(i = 0; i<arraysize; i++) //Has the user put in a number for every space in the array
    {
         scanf("%d", &array[i]); //The i coordinate updates with the i++
         getchar();

    }
    printf("Your numbers were: \n");
    for(i = 0; i<arraysize; i++) //Same thing as the previous for() loop
    {                            //except we are printing the numbers in the table
         printf("| %d |", array[i]);

    }
}

希望有帮助

C99具有可变长度数组!!谷歌和learnit是可能的。如果要获取用户输入并生成输出,请使用标准I/O函数。如果需要动态数组,请使用
malloc()
或VLAs(仅限C99及更高版本)。无论哪种情况,请使用谷歌。谢谢。我对了解更多感兴趣。对了解动态内存分配感到兴奋。非常感谢。标准警告:请输入
malloc()
和family的返回值。@Sourav Ghosh是否愿意解释原因?我被教导总是投输出,但这可能是日期或不正确的信息。我想了解它是否/为什么不正确。@smeezekitty请查看以获取解释。@smeezekitty我的评论中的链接中有高质量的答案。你看过了吗?谢谢你,这就是我要找的。
#include <stdio.h>

int main(void)
{
    int arraysize, i;

    printf("Please input an array size.\n");
    scanf("%d", &arraysize); //Will set the user input and use it as array size
    getchar(); //Has user hit enter to continue
    i = arraysize; //This is for sheer convenience in the for() loop
    int array[i]; //This creates an array of the size inputted by the user
    printf("Please input %d numbers to fill the array.\n", i); 
    for(i = 0; i<arraysize; i++) //Has the user put in a number for every space in the array
    {
         scanf("%d", &array[i]); //The i coordinate updates with the i++
         getchar();

    }
    printf("Your numbers were: \n");
    for(i = 0; i<arraysize; i++) //Same thing as the previous for() loop
    {                            //except we are printing the numbers in the table
         printf("| %d |", array[i]);

    }
}
[PROGRAM BEGINS]
Please input an array size.
5
Please input 5 numbers to fill the array.
1 2 33 4 5
Your numbers were:
| 1 || 2 || 33 || 4 || 5 |
[PROGRAM ENDS]