What is the type of a pointer? 
Author Message
 What is the type of a pointer?

Quote:
> ((char*)ptr)++ is a syntax error in C

Not a syntax error.  Other than its obvious purpose of making sure that
operators have the correc number of operands, the C standard uses
expression syntax mostly as a way of avoiding the concept of operator
precedence.  The syntax requires only that the argument of ++ be a
"postfix expression", which means either a primary expression or an
expression involving only postfix operators.  A primary expression is
an identifier, a constant, a string literal, or any expression in
parentheses.  So the above is valid syntax.

It is, however, a constraint violation.  The operand of ++ is required
(in section 6.3.2.4/3.3.2.4 of the standard) to be a modifiable lvalue,
and ((char *)ptr) is not an lvalue at all.

Quote:
> (i.e. a compiler must spot it!) ...

This is true of constraint violations also.

The rest of the cited article explained *why* it is a constraint violation,
correctly.  This topic is covered in the comp.lang.c FAQ list, which
*really* should be posted to this newsgroup also:

4.5:    I have a char * pointer that happens to point to some ints, and
        I want to step it over them.  Why doesn't

                ((int *)p)++;

        work?

A:      In C, a cast operator does not mean "pretend these bits have a
        different type, and treat them accordingly"; it is a conversion
        operator, and by definition it yields an rvalue, which cannot be
        assigned to, or incremented with ++.  (It is an anomaly in pcc-
        derived compilers, and an extension in gcc, that expressions
        such as the above are ever accepted.)  Say what you mean: use

                p = (char *)((int *)p + 1);

        or (since p is a char *) simply

                p += sizeof(int);

        Whenever possible, you should choose appropriate pointer types
        in the first place, instead of trying to treat one type as
        another.

        References: K&R2 Sec. A7.5 p. 205; ANSI Sec. 3.3.4 (esp.
        footnote 14); ISO Sec. 6.3.4; Rationale Sec. 3.3.2.4; H&S
        Sec. 7.1 pp. 179-80.
--

#define MSB(type)       (~(((unsigned type)-1)>>1))

My text in this article is in the public domain.



Wed, 22 Jul 1998 03:00:00 GMT  
 What is the type of a pointer?

Quote:

> Other than its obvious purpose of making sure that
> operators have the correct number of operands, the C standard uses
> expression syntax mostly as a way of avoiding the concept of operator
> precedence.  The syntax requires only that the argument of ++ be a
> "postfix expression", which means either a primary expression or an
> expression involving only postfix operators.

Huh.  That's what I get for trying to translate a formal grammar into
simple English.  What I *meant* is:

  ... The syntax requires only that the argument of postfix ++ be a
  "postfix expression", which means either a primary expression or an
  expression in which only postfix unary operators are applied to a
  primary expression.

Sorry about that.
--
Mark Brader          "Yet again, I begged him to explain himself in plain
SoftQuad Inc.         English.  This request always surprises him, as he

Toronto               he has done so."       -- Lynn & Jay, "Yes Minister"

My text in this article is in the public domain.



Thu, 23 Jul 1998 03:00:00 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 
 

 
Powered by phpBB® Forum Software