public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* "restrict" in brackets - what is this?
@ 2023-08-10 14:39 Ian Pilcher
  2023-08-10 14:45 ` Xi Ruoyao
  2023-08-10 14:45 ` John Scott
  0 siblings, 2 replies; 4+ messages in thread
From: Ian Pilcher @ 2023-08-10 14:39 UTC (permalink / raw)
  To: gcc-help

I am looking at the man page for inet_ntop(3) on my Fedora 38 system,
and it shows the following:

  const char *inet_ntop(int af, const void *restrict src,
                        char dst[restrict .size], socklen_t size);
                                 ^^^^^^^^^^^^^^

I have never seen the underlined syntax used before.  Is this a GCC
extension, something from one of the newer C standards, or something
else?

Sorry for the potentially stupid question, but none of my searches have
turned up anything.

-- 
========================================================================
Google                                      Where SkyNet meets Idiocracy
========================================================================


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: "restrict" in brackets - what is this?
  2023-08-10 14:39 "restrict" in brackets - what is this? Ian Pilcher
@ 2023-08-10 14:45 ` Xi Ruoyao
  2023-08-10 14:45 ` John Scott
  1 sibling, 0 replies; 4+ messages in thread
From: Xi Ruoyao @ 2023-08-10 14:45 UTC (permalink / raw)
  To: Ian Pilcher; +Cc: gcc-help

On Thu, 2023-08-10 at 09:39 -0500, Ian Pilcher via Gcc-help wrote:
> I am looking at the man page for inet_ntop(3) on my Fedora 38 system,
> and it shows the following:
> 
>   const char *inet_ntop(int af, const void *restrict src,
>                         char dst[restrict .size], socklen_t size);
>                                  ^^^^^^^^^^^^^^
> 
> I have never seen the underlined syntax used before.  Is this a GCC
> extension, something from one of the newer C standards, or something
> else?
> 
> Sorry for the potentially stupid question, but none of my searches have
> turned up anything.

man pages are not a part of GCC.  Please ask via
linux-man@vger.kernel.org.

-- 
Xi Ruoyao <xry111@xry111.site>
School of Aerospace Science and Technology, Xidian University

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: "restrict" in brackets - what is this?
  2023-08-10 14:39 "restrict" in brackets - what is this? Ian Pilcher
  2023-08-10 14:45 ` Xi Ruoyao
@ 2023-08-10 14:45 ` John Scott
  2023-08-10 15:14   ` Ian Pilcher
  1 sibling, 1 reply; 4+ messages in thread
From: John Scott @ 2023-08-10 14:45 UTC (permalink / raw)
  To: Ian Pilcher, gcc-help


[-- Attachment #1.1: Type: text/plain, Size: 817 bytes --]

This is a C99 feature. Essentially, since arrays are implicitly converted to pointers when passed to functions, this:
void foo(char s[restrict]);
is equivalent to this:
void foo(char *restrict s);

Note that the [.size] notation is neither a C feature nor a GCC extension; it's a notational convenience used in the Linux man pages. The proper way to declare a function that takes an array of a specified size is to use the static qualifier like so:
void foo(size_t array_size, char s[static array_size]);
which specifies that s must point to at least array_size elements when the function is called.

If the size argument needs to come after the array parameter, then that's what the GCC extension of being able to forward declare parameters is for:
void foo(size_t size; char [static size], size_t size);

[-- Attachment #1.2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5880 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: "restrict" in brackets - what is this?
  2023-08-10 14:45 ` John Scott
@ 2023-08-10 15:14   ` Ian Pilcher
  0 siblings, 0 replies; 4+ messages in thread
From: Ian Pilcher @ 2023-08-10 15:14 UTC (permalink / raw)
  To: gcc-help

On 8/10/23 09:45, John Scott via Gcc-help wrote:
> This is a C99 feature. Essentially, since arrays are implicitly
> converted to pointers when passed to functions, this: void foo(char
> s[restrict]); is equivalent to this: void foo(char *restrict s);

Thank you.  I wasn't familiar with the "array" version of the syntax.

> Note that the [.size] notation is neither a C feature nor a GCC
> extension; it's a notational convenience used in the Linux man pages.
> The proper way to declare a function that takes an array of a
> specified size is to use the static qualifier like so: void
> foo(size_t array_size, char s[static array_size]); which specifies
> that s must point to at least array_size elements when the function
> is called.

I can't believe that I didn't know about this before now.

> If the size argument needs to come after the array parameter, then
> that's what the GCC extension of being able to forward declare
> parameters is for: void foo(size_t size; char [static size], size_t
> size);

Cool!

Thanks again!

-- 
========================================================================
Google                                      Where SkyNet meets Idiocracy
========================================================================



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-08-10 15:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-10 14:39 "restrict" in brackets - what is this? Ian Pilcher
2023-08-10 14:45 ` Xi Ruoyao
2023-08-10 14:45 ` John Scott
2023-08-10 15:14   ` Ian Pilcher

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).