10

What is the meaning of * in function parameter when it is in the index of array in C?

int function(int a[*])
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
Sabrina
  • 1,460
  • 11
  • 23
  • 2
    @WeatherVane: It is not. – too honest for this site Feb 22 '17 at 16:21
  • Oh I see (@haccks answer) - I had already tried it as a function prototype before commenting above - but my C does not implement the optional VLA (not that it even did before it became optional). So it remains a syntax error, for me. – Weather Vane Feb 22 '17 at 16:22
  • @WeatherVane: Let me guess ... MSVC - still not up to move forward to modern C ;-) ? – too honest for this site Feb 22 '17 at 16:23
  • @Olaf the VLA is optional, do get up to speed. – Weather Vane Feb 22 '17 at 16:24
  • 2
    @WeatherVane, it is only optional for compilers that implement the C11 standard. – Jens Gustedt Feb 22 '17 at 16:31
  • 1
    @JensGusted isn't the latest standard *the* standard? – Weather Vane Feb 22 '17 at 16:33
  • Yes, of course C11 is presently *the* standard, so yes, VLA support *is* an optional feature, even though it was a required feature of conforming C99 implementations. – John Bollinger Feb 22 '17 at 16:36
  • 5
    @WeatherVane, sure it is. But for those compilers that are conforming to C11 but don't implement VLA (I don't know of any) it is not a syntax error, but a constraint violation.And if you look at a compiler that is not conforming to any C standard, it makes no sense to say that it is optional accodring to that standard. – Jens Gustedt Feb 22 '17 at 16:41
  • @WeatherVane: Modern compilers support C99 **and** C11. I'm not aware of any such compiler which does not support VLAs in C11, but in C99 mode. Even MISRA has advanced and requires C99 since the latest release. For the rest: see Jens Gustedt's comment. Making VLAs optional was completely against the normal practice of backwards-compatibility the committee otherwise follows even if it is nonsense. – too honest for this site Feb 22 '17 at 17:01
  • @Olaf I agree it is nonsense. Although the reasons for it are most likely different, in the case of `gets` MSVC issues a warning and the library header no longer contains the function prototype: however it compiles and links, for that backward compatibility. My original comment was an oversight about the VLA, however I left it in. – Weather Vane Feb 22 '17 at 17:37
  • @WeatherVane: Just call me an optimist: If nudged enough, I hope one day you will use a modern compiler :-) WRT VLAs: honi soit qui mal y pense. – too honest for this site Feb 22 '17 at 17:43
  • 1
    @Olaf there is massive plagiarism "on the internet" for the old French *honi soit qui mal y pense* to mean "shame on those who think badly of it". But as it is the motto of the chivalric Order of the Garter, my preferred version is "Let us honour those who think badly of it" and to me that makes far more sense for those with a chivalric outlook. – Weather Vane Feb 22 '17 at 21:56
  • @WeatherVane: AFAIK it normally is used with a sarcastic connotation, tongue in cheek (I know it also is a motto of the order, but that's obviously not the intended use here). I meant the original french semantics, see https://en.wikipedia.org/wiki/Honi_soit_qui_mal_y_pense (2nd paragraph). Sorry, if that is not the common interpretation in English as I assumed. – too honest for this site Feb 22 '17 at 23:09
  • @Olaf the "common interpretation" in English would probably be "you what?" – Weather Vane Feb 23 '17 at 01:26
  • @WeatherVane: This phrase is not uncommon in German and we are not well known for our French language skills either:-) But agreed, it is not what I asumed from a grocery shop employee. Nevertheless, considering it originates from an English king and is the motto of an english order, I'd assume it can well be seen as part of the English language, much as "ad hominem" arguments. Anyway, google is our friend and extending our horizon is always a good thing, isn't it? – too honest for this site Feb 23 '17 at 01:45

2 Answers2

12

* is used inside [] only to declare function prototypes. Its a valid syntax. It's useful when you omit parameters name from the prototype like example below

int function(int, int [*]);

It tells the compiler that second parameter is a VLA and depends on the value of the first parameter. In the above example it's not worth of doing this. Take a look at a more useful example

int function(int, int [][*]); 

This can't be possible to use VLA as a parameter in a function prototype, having unnamed parameters, without using * inside [].

6.7.6 Declarators

Syntax

 1 declarator:
        [...]
        direct-declarator [ type-qualifier-list static assignment-expression ]
        direct-declarator [ type-qualifier-listopt * ]
        direct-declarator ( parameter-type-list )
        direct-declarator ( identifier-listopt )
Community
  • 1
  • 1
haccks
  • 104,019
  • 25
  • 176
  • 264
6

Paragraph 6.7.6.2/1 of the standard specifies that in an array-like declaration:

In addition to optional type qualifiers and the keyword static, the [ and ] may delimit an expression or *.

(emphasis added). Paragraph 4 of the same section explains:

If the size is * instead of being an expression, the array type is a variable length array type of unspecified size, which can only be used in declarations or type names with function prototype scope

Provided that your implementation supports VLAs, that's what you have. The function is declared to accept an argument that is a (pointer to the first element of) a variable-length array of unspecified length.

As @WeatherVane observes in comments, however, C2011 makes VLA support optional (whereas it was mandatory in C99). For an implementation that does not support VLAs, the code is syntactically incorrect.

You can check for VLA support via the preprocessor, somewhat:

#if __STDC__
#if __STDC_VERSION__ == 199901L || ( __STDC_VERSION__ >= 201112L && ! __STDC_NO_VLA__ )
// VLAs are supported (or the implementation is non-conforming)
#else
// VLAs are not supported
// (unless the implementation provides VLAs as an extension, which could,
// under some circumstances, make it non-conforming)
#endif
#else
// a C89 or non-conforming implementation; no standard way to check VLA support
#endif
John Bollinger
  • 160,171
  • 8
  • 81
  • 157