pointer arithmetic with a pointer to void 
Author Message
 pointer arithmetic with a pointer to void

    Hi all,

    I pretty much get how pointer arithmetics works with pointer to
'real' types (that is not to void). Now I am wondering though : what is
allowed/defined/advised when handling pointers to void. Tests on some
compilers have shown that they handle it as I expected (i.e. exactly as
a char *) as far as arithmetics. I am wondering though if this is mere
luck, or if this is defined is standard C (actually, my doubts arise
because of a recent post of Tobias in one of my threads "two-dimensional
arrays").

    So, thanks in advance for your feedback. Please, emlighten me : )

    Bertrand



Mon, 03 Nov 2003 03:38:39 GMT  
 pointer arithmetic with a pointer to void

Quote:
>     Hi all,
>     I pretty much get how pointer arithmetics works with pointer to
> 'real' types (that is not to void). Now I am wondering though : what is
> allowed/defined/advised when handling pointers to void. Tests on some
> compilers have shown that they handle it as I expected (i.e. exactly as
> a char *) as far as arithmetics. I am wondering though if this is mere
> luck, or if this is defined is standard C (actually, my doubts arise
> because of a recent post of Tobias in one of my threads "two-dimensional
> arrays").
>     So, thanks in advance for your feedback. Please, emlighten me : )

I would figure this is pure luck, and pointer arithmetic with void
pointers is not defined by the C standard.

Here is some results from a simple test I did. Proof by example is not
valid proof, but you can get the general idea.


int main(void) {
  char a;
  void *p=&a;
  p+1;
  return 0;

Quote:
}


hoo.c: In function `main':
hoo.c:4: warning: pointer of type `void *' used in arithmetic
hoo.c:4: warning: statement with no effect

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"I will never display my bum in public again."
   - Homer Simpson



Mon, 03 Nov 2003 03:58:04 GMT  
 pointer arithmetic with a pointer to void

Quote:

> Here is some results from a simple test I did. Proof by example is not
> valid proof, but you can get the general idea.

    Don't worry : proof that something is wrong by counter-example is
perfectly valid.
    Thanks for the demo : )


Mon, 03 Nov 2003 05:06:58 GMT  
 pointer arithmetic with a pointer to void

Quote:

>     Hi all,

>     I pretty much get how pointer arithmetics works with pointer to
> 'real' types (that is not to void). Now I am wondering though : what is
> allowed/defined/advised when handling pointers to void. Tests on some
> compilers have shown that they handle it as I expected (i.e. exactly as
> a char *) as far as arithmetics. I am wondering though if this is mere
> luck, or if this is defined is standard C (actually, my doubts arise
> because of a recent post of Tobias in one of my threads "two-dimensional
> arrays").

>     So, thanks in advance for your feedback. Please, emlighten me : )

Pointer arithmetic cannot be performed portably on void pointers. To do
so violates a constraint...

6.5.6

2 For addition, either both operands shall have arithmetic type, or one
operand shall be a pointer to an object type and the other shall have
integer type.

...because you can't have an object of type void, so void * is not a
pointer to an object type.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Mon, 03 Nov 2003 05:10:25 GMT  
 pointer arithmetic with a pointer to void


Quote:
>    I pretty much get how pointer arithmetics works with pointer to
>'real' types (that is not to void). Now I am wondering though : what is
>allowed/defined/advised when handling pointers to void. Tests on some
>compilers have shown that they handle it as I expected (i.e. exactly as
>a char *) as far as arithmetics. I am wondering though if this is mere
>luck, or if this is defined is standard C (actually, my doubts arise
>because of a recent post of Tobias in one of my threads "two-dimensional
>arrays").

Unless you have a compiler from about 1979, where they essentially
treated void as char as a fake out, then all bets are off on
doing ptr arithmetic on a void *.  That is so say, this is not
Standard C:

   void *p;
   /* Code assigning to p */
   p++;  /* How to scale this? */

You should expect this to fail on any up to date compiler
run in its strict mode.
--
Greg Comeau                 Comeau C/C++ 4.2.45.2
ONLINE COMPILER ==>         http://www.comeaucomputing.com/tryitout
NEW: Try out libcomo!       NEW: Try out our C99 mode!



Mon, 03 Nov 2003 05:13:06 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Pointer arithmetic: char* vs. void*

2. Pointer arithmetic on void*

3. void pointer arithmetic

4. void * and pointer arithmetic

5. Pointer arithmetic with void*

6. arithmetic on void pointers

7. pointer arithmetic and void *

8. Casting function pointer to void pointer

9. problem with pointer to pointer to void

10. pointers to functions and pointers to void.

11. Can void-pointers be casted to function-pointers?

12. Pointer-arithmatic and void pointers

 

 
Powered by phpBB® Forum Software