Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

static array size: int foo[static 2]

2 views
Skip to first unread message

ShaunJ

unread,
Dec 10, 2009, 2:05:09 PM12/10/09
to
I saw the following declaration in the source code of xzutils:

uint16_t integer_read_16(const uint8_t buf[static 2]);

I've never seen the keyword static used in this way before. I can't
seem to find any reference to it online. GCC does accept it as legal.
Has this syntax always existed? If not, when was it added to the C
standard? What does it mean?

Thanks,
Shaun
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Alain Ketterlin

unread,
Dec 11, 2009, 3:46:28 PM12/11/09
to
ShaunJ <sjac...@gmail.com> writes:

> I saw the following declaration in the source code of xzutils:
>
> uint16_t integer_read_16(const uint8_t buf[static 2]);
>
> I've never seen the keyword static used in this way before. I can't
> seem to find any reference to it online. GCC does accept it as legal.
> Has this syntax always existed? If not, when was it added to the C
> standard? What does it mean?

It appeared in C99, and specifies the minimal number of data the array
contains. You can put "const" there also.

Like you I couldn't find much on this feature. Here is a short
explanation:
http://publib.boulder.ibm.com/infocenter/zos/v1r9/index.jsp?topic=/com.ibm.zos.r9.cbclx01/static_array_index.htm

HTH,

-- Alain.

ShaunJ

unread,
Dec 14, 2009, 1:09:31 PM12/14/09
to
On Dec 11, 12:46 pm, Alain Ketterlin <al...@dpt-info.u-strasbg.fr>
wrote:

> ShaunJ <sjack...@gmail.com> writes:
> > I saw the following declaration in the source code of xzutils:
>
> > uint16_t integer_read_16(const uint8_t buf[static 2]);
>
> > I've never seen the keyword static used in this way before. I can't
> > seem to find any reference to it online. GCC does accept it as legal.
> > Has this syntax always existed? If not, when was it added to the C
> > standard? What does it mean?
>
> It appeared in C99, and specifies the minimal number of data the array
> contains. You can put "const" there also.
>
> Like you I couldn't find much on this feature. Here is a short
> explanation:http://publib.boulder.ibm.com/infocenter/zos/v1r9/index.jsp?topic=/co...

Thanks for the link. That clears up my question. I found this usage
interesting:

void foo(int arr [static const i]); /* arr points to at least i ints;
i is computed at run
time. */

I tried the following use case:

int read(int fd, const char buf[static count], unsigned count)
{
return count;
}

but GCC didn't like it.

foo.c:1: error: 'count' undeclared here (not in a function)

However, the following declaration was acceptable:

int read(int fd, unsigned count, const char buf[static count])
{
return count;
}

They're going to have to redefine the order of arguments of read. ;)

Cheers,
Shaun

0 new messages