Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #59132

Re: Variadic functions and const

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.lang.c
Subject Re: Variadic functions and const
Date 2015-03-28 14:11 +0000
Organization Aioe.org NNTP Server
Message-ID <20150328065320.287@kylheku.com> (permalink)
References <8fdc6e6e-6160-4ec8-b910-971c96445e67@googlegroups.com>

Show all headers | View raw


On 2015-03-28, samuelallenalexander@gmail.com <samuelallenalexander@gmail.com> wrote:
> Dear comp.lang.c,
>
> Really surprised I can't find this discussed anywhere.  In a C variadic
> function's prototype, is there any way to specify the const status of the
> variables?

"const" is a type qualifier.  The trailing arguments of a variadic function
do not have a declared type. So there is nothing there that a const could
qualify.

> void nullify_strings( int N, ... )
> {
>   char *s;
>   va_list vargs;
>
>   for ( va_start(vargs, N); N > 0; N-- )
>     *( (s = va_arg(vargs,char*)) ) = '\0';
>
>   va_end(vargs);
> }

You are free to code this as

  const char *s;

  /* ... */

  s = va_arg(vargs, const char *)

(and of course drop the mutating assignment). Then document to the users
that this treats strings as const char *.

> But there's nothing in the prototype warning people (or compilers) that const
> char *'s shouldn't be passed to this function.

Indeed: namely, there is no type information for the trailing arguments.

You can call it as follows, and no diagnostic is required:

  struct foo { int x; } s;

  nullify_strings(42, 4.0E+23, s, &s);

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Variadic functions and const samuelallenalexander@gmail.com - 2015-03-28 05:54 -0700
  Re: Variadic functions and const Lőrinczy Zsigmond <nospam@for.me> - 2015-03-28 14:38 +0100
  Re: Variadic functions and const Richard Damon <Richard@Damon-Family.org> - 2015-03-28 10:02 -0400
  Re: Variadic functions and const Kaz Kylheku <kaz@kylheku.com> - 2015-03-28 14:11 +0000
  Re: Variadic functions and const Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-03-28 15:13 +0000
  Re: Variadic functions and const fir <profesor.fir@gmail.com> - 2015-03-28 08:34 -0700
    Re: Variadic functions and const fir <profesor.fir@gmail.com> - 2015-03-28 08:38 -0700
    Re: Variadic functions and const Lőrinczy Zsigmond <nospam@for.me> - 2015-03-28 17:01 +0100
      Re: Variadic functions and const Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-28 12:49 -0700

csiph-web