长度为零的数组 [英] Array of zero length

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

问题描述

我正在重构一些旧code和发现含零长度阵列(下图)几个结构。警告德$ P $由编译pssed,当然,但我失败了含有这种结构的新结构(错误2233)创建。阵'byData作为指针,但为什么不改用指针?或者长度为1的阵列?当然,没有加入评论让我享受这个过程...
任何导致使用这样的事情?在重构这些有什么建议?

I am working on refactoring some old code and have found few structs containing zero length arrays (below). Warnings depressed by pragma, of course, but I've failed to create by "new" structures containing such structures (error 2233). Array 'byData' used as pointer, but why not to use pointer instead? or array of length 1? And of course, no comments were added to make me enjoy the process... Any causes to use such thing? Any advice in refactoring those?

struct someData
{
   int nData;
   BYTE byData[0];
}

NB这是C ++,Windows XP中,VS 2003

NB It's C++, Windows XP, VS 2003

推荐答案

是的,这是C-哈克。结果
创建任何长度的数组:

Yes this is a C-Hack.
To create an array of any length:

struct someData* mallocSomeData(int size)
{
    struct someData*  result = (struct someData*)malloc(sizeof(struct someData) + size * sizeof(BYTE));
    if (result)
    {    result->nData = size;
    }
    return result;
}

现在你有someData的对象具有指定长度的数组。

Now you have an object of someData with an array of a specified length.

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

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