为什么在这个C code工作变长数组? [英] Why is variable-length array working in this C code?

查看:143
本文介绍了为什么在这个C code工作变长数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "stdio.h"

int main()
    {
        int n;
        printf("Enter n:\n");
        scanf("%d",&n);

        int arr[n][n];
        arr[3][3] = 4;
        printf("%d",arr[3][3]);
        getchar();
        return 0;
    }

没有使用 INT ARR [N] ,其中n是一个变量,在C非法的?我想了解这里发生了什么。显然,code工作在我的铛LLVM编译器 IDEOne 和$ C $的cblock。我猜编译器只是让事情变得简单对我来说,做自动内存分配。但另一个令人震惊的事实是,当我尝试设置 N 1,2或3它仍然有效。

Wasn't using int arr[n], where n is a variable, illegal in C? I am trying to understand what's happening here. Apparently the code works on my clang LLVM compiler and on IDEOne and on Codeblocks. I guess the compiler is just making things easy for me, by doing automatic memory allocation. But another astounding fact is that when I try to set n to 1, 2 or 3 it still works.

推荐答案

变长数组由自C99 C标准允许的。请注意,他们仍然没有C ++允许搜索
标准。结果
需要注意的另外重要的一点是对可变长度数组被实现在扩展的形式支持前C99和大多数C ++编译器版本。

Variable length arrays are allowed by C standard since C99. Note that they are still not allowed by C++
standard.
Also important point to note is for versions before c99 and for most C++ compilers variable length arrays are supported by implementations in the form of extensions.

这篇关于为什么在这个C code工作变长数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆