关于在C数组 [英] about the array in C

查看:91
本文介绍了关于在C数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了下面的code(请参阅问题code评论)

 #包括LT&;&stdio.h中GT;
诠释的main()
{
    INT大小;
    scanf函数(%d个,&安培;大小);
    INT ARR【规格】; / *是一个有效的声明?* /
    的for(int i = 1; I< =大小;我++)
    {
        scanf函数(%d个,&安培;常用3 [I]);
        的printf(%d个,编曲[I]);
    }
    返回0;
}


解决方案

使用非常量数组的大小是有效的C99但不C90。有一个较旧的GCC扩展允许它。

请注意,服用这种优势使其难以检查内存分配成功。由用户提供的值使用它可能并不明智。

I have written a following code (see code comments for the question),

#include<stdio.h>
int main()
{
    int size;
    scanf("%d",&size);
    int arr[size];    /*is it a valid statement?*/
    for(int i=1;i<=size;i++)
    {
        scanf("%d",&arr[i]);
        printf("%d",arr[i]);
    }
    return 0;
}

解决方案

The use of a non constant array size is valid C99 but not C90. There is an older gcc extension allowing it.

Note that taking advantage of this make it harder to check that memory allocation succeed. Using it with values provided by the user is probably not wise.

这篇关于关于在C数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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