Skip to content

C and C++ array-to-pointer decay #9

@hainest

Description

@hainest

@kupsch Notes

I looked up C function array parameter little used functionality. Here are the details.

When declaring an array parameters to a function in all versions of C and C++ for can use the normal common syntax:

f(T a[])
f(T a[N])

where T is a const/volatile qualified type (also restrict qualified for C). These parameter types decay to the type T*.

C99 and later versions of C allow the additional syntax

f(T a[OPT_KEYWORDS N])
f(T a[OPT_KEYWORDS *]) same as f(T a[OPT_KEYWORDS])

where OPT_KEYWORDS are used to require a array size and to add qualifiers to the pointer itself as opposed to the type of the pointer where the qualifiers are part of T. They can be one or more of the following:

static - hint: a is not NULL with at least N elements
const - type of a inside the function is Tconst
volatile - type of a inside the function is T
volatile
restrict - type of a inside the function is T*restrict

The * indicates unknown size.

For example, the declaration

f(volatile int a[static const restrict 10])

would treat the type of a in the function f as if it were declared:

volatile int * const restrict a;

with the value of a being not null and containing at least 10 elements.


There is a lot more to mention about this, but I wanted to record Jim's notes first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions