va_arg(ap, const char *) = char*? 
Author Message
 va_arg(ap, const char *) = char*?

Can va_arg(ap, const char *) fail if the argument is plain 'char *',
or  va_arg(ap, char *) fail if the argument is 'const char *'?

--
Hallvard



Sun, 04 Sep 2005 03:03:11 GMT  
 va_arg(ap, const char *) = char*?

# Can va_arg(ap, const char *) fail if the argument is plain 'char *',
# or  va_arg(ap, char *) fail if the argument is 'const char *'?

With var-args you should not assume the original types of the arguments
are in anyway saved. Or even the number of arguments or the types. All you
are given is a bit string of unknown length; it is up to you to partition that
into typed values with successive va_arg() calls. It's entirely your
responsibility not to assign a bit string an incompatiable type (such
as odd integer made (int*) on some cpus). If that happens, the results
are undefined and the compiler will probably not include enough information
for an automated detection and analysis of the failure.

--
Derk Gwen http://www.*-*-*.com/
Death is the worry of the living. The dead, like myself,
only worry about decay and {*filter*}cs.



Sun, 04 Sep 2005 10:13:45 GMT  
 va_arg(ap, const char *) = char*?

Quote:


> # Can va_arg(ap, const char *) fail if the argument is plain 'char *',
> # or  va_arg(ap, char *) fail if the argument is 'const char *'?

> With var-args you should not assume the original types of the
> arguments are in anyway saved. (...) It's entirely your responsibility
> not to assign a bit string an incompatiable type (such as odd integer
> made (int*) on some cpus).

I know.  I just wondered if treaing char* as const char* could cause
trouble.  Anyway, I found the answer myself in ANSI 3.1.2.5 (Types):

   A pointer to void shall have the same representation and alignment
   requirements as a pointer to a character type.  Similarly, pointers
   to qualified or unqualified versions of compatible types shall have
   the same representation and alignment requirements.  (...)

Which means I can also pass NULL to a char*, BTW.  That's nice.

--
Hallvard



Sun, 04 Sep 2005 18:46:06 GMT  
 va_arg(ap, const char *) = char*?

Quote:

> Which means I can also pass NULL to a char*, BTW.  

You can't portably pass NULL to an unprototyped or variadic function
that expects a pointer to char.  NULL may have integer type.

Jeremy.



Sun, 04 Sep 2005 19:01:57 GMT  
 va_arg(ap, const char *) = char*?

Quote:
>I know.  I just wondered if treaing char* as const char* could cause
>trouble.  Anyway, I found the answer myself in ANSI 3.1.2.5 (Types):

>   A pointer to void shall have the same representation and alignment
>   requirements as a pointer to a character type.  Similarly, pointers
>   to qualified or unqualified versions of compatible types shall have
>   the same representation and alignment requirements.  (...)

>Which means I can also pass NULL to a char*, BTW.  That's nice.

If the char * is in the variable parameter list, you still have to cast
NULL to char *.   Even the following code is broken:

    printf("%p\n", NULL);

The correct version being

    printf("%p\n", (void *)NULL);

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Sun, 04 Sep 2005 21:56:29 GMT  
 va_arg(ap, const char *) = char*?


Wed, 18 Jun 1902 08:00:00 GMT  
 va_arg(ap, const char *) = char*?
[Followup-To: comp.std.c]

Quote:

> You can't portably pass NULL to an unprototyped or variadic function
> that expects a pointer to char.  NULL may have integer type.

Oops, you are right.  All the standard says is that it 'expands to an
implementation-defined null pointer constant'.  Which means it could
even be (int*)0, which cannot be assigned to e.g. a char* without
casting it.  (Though the library section makes a few more restrictions
since it uses NULL a few places without casting it.)

It seems to me that NULL as currently defined is completely useless:
Since we always must cast it, we could just as well cast 0.

A lot of code uses NULL without casting it.
I've had the impression that NULL must have the same size, alignment and
representation as (void*)0, and that's true on the systems I have seen.
Would it be possible to mandate this in a future version of the
standard?

--
Hallvard



Sun, 04 Sep 2005 23:45:27 GMT  
 va_arg(ap, const char *) = char*?

Quote:

> [Followup-To: comp.std.c]

I shouldn't have done that so quickly.  comp.lang.c readers, you can
stop telling me I was wrong now:-)

--
Hallvard



Mon, 05 Sep 2005 16:58:07 GMT  
 va_arg(ap, const char *) = char*?

Quote:


> > [Followup-To: comp.std.c]

> I shouldn't have done that so quickly.  comp.lang.c readers, you can
> stop telling me I was wrong now:-)

Your "future version of the standard" is the prefered topic,
on comp.std.c

--
pete



Mon, 05 Sep 2005 19:55:54 GMT  
 va_arg(ap, const char *) = char*?


Wed, 18 Jun 1902 08:00:00 GMT  
 va_arg(ap, const char *) = char*?


Quote:
> Can va_arg(ap, const char *) fail if the argument is plain 'char *',
> or  va_arg(ap, char *) fail if the argument is 'const char *'?

Let's see.

7.15.1.1 The va_arg macro

2 ... If there is no actual next argument, or if type is not
  compatible with the type of the actual next argument (as
  promoted according to the default argument promotions), the
  behavior is undefined, except for the following cases:

  - one type is a signed integer type, the other type
  is the corresponding unsigned integer type, and the value is
  representable in both types;
  - one type is pointer to void and the other
  is a pointer to a character type.

so the question is are the two types const char * and char *
compatible? And I think they are not, because:

6.7.3 Type qualifiers

9 ... For two qualified types to be compatible, both shall have the
  identically qualified version of a compatible type; ...

and

6.7.5.1 Pointer declarators

2 For two pointer types to be compatible, both shall be
  identically qualified and both shall be pointers to compatible
  types.

So that pointing to a char isn't compatible to pointing to a const
char.

But I may be interpreting the standard in a wrong way.
--

profound enlightenment experience you will have when you finally get
it; that experience will make you a better programmer for the rest of
your days."  -- Eric S. Raymond



Tue, 06 Sep 2005 02:01:08 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Help: inline char const* const& max(char const* const &a, char const* const &b)

2. const char * vs char const *

3. const char *p == char const *p ?

4. char** and const char** ...

5. const char* to char[64]

6. char^ to const char* casting

7. const char ** incompatible with char ** (and vice versa)?

8. passing const char* to char* fails

9. const char* vs. char *

10. converting const char* to unsigned char[1024]

11. char *'s and const char *'s

12. const char* to char* ?

 

 
Powered by phpBB® Forum Software