可以初始化在C声明之后的数组? [英] Possible to initialize an array after the declaration in C?

查看:114
本文介绍了可以初始化在C声明之后的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时居然出现在初始化之前宣布这样一个变量的方法吗?

  CGFloat的成分[8] = {
        0.0,0.0,0.0,0.0,
        0.0,0.0,0.0,0.15
    };

我想它宣布这样的事情(除了这不起作用):

  CGFloat的部件; [8]
    部件[8] = {
        0.0,0.0,0.0,0.0,
        0.0,0.0,0.0,0.15
    };


解决方案

您不能分配到数组所以基本上你不能做,你提出什么,但在C99中,你可以这样做:

  CGFloat的*组件;
成分=(CGFloat的[8]){
    0.0,0.0,0.0,0.0,
    0.0,0.0,0.0,0.15
};

(){} 运算符称为的复合文字的运营商。它是一个C99的功能。

请注意,在这个例子组件被声明为一个指针,而不是一个数组。

Is there a way to declare a variable like this before actually initializing it?

    CGFloat components[8] = {
        0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.15
    };

I'd like it declared something like this (except this doesn't work):

    CGFloat components[8];
    components[8] = {
        0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.15
    };

解决方案

You cannot assign to arrays so basically you cannot do what you propose but in C99 you can do this:

CGFloat *components;
components = (CGFloat [8]) {
    0.0, 0.0, 0.0, 0.0,
    0.0, 0.0, 0.0, 0.15
};

the ( ){ } operator is called the compound literal operator. It is a C99 feature.

Note that in this example components is declared as a pointer and not as an array.

这篇关于可以初始化在C声明之后的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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