Arithmetic on a void * pointer - C

This is a discussion on Arithmetic on a void * pointer - C ; Hi! I know that I cannot perform arithmetic on a void * pointer, but can I do something like: [...] void *base1; void *p1; size_t i, size; p1 = base1 + i * size; [...] is it legal/dangerous/portable ? Thanks ...

+ Reply to Thread
Page 1 of 4 1 2 3 ... LastLast
Results 1 to 10 of 36

Arithmetic on a void * pointer

  1. Default Arithmetic on a void * pointer

    Hi!

    I know that I cannot perform arithmetic on a void * pointer, but can I
    do something like:

    [...]

    void *base1;
    void *p1;
    size_t i, size;

    p1 = base1 + i * size;

    [...]

    is it legal/dangerous/portable ?

    Thanks for your help

    Paolo Montegriffo
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

  2. Default Re: Arithmetic on a void * pointer

    Paolo Montegriffo wrote:
    > I know that I cannot perform arithmetic on a void * pointer, but can I
    > do something like:
    > void *base1;
    > void *p1;
    > size_t i, size;
    > p1 = base1 + i * size;


    I thought you just said you know that arithmetic on a
    void* is not allowed. What would be the width of an
    element of type void, if there were such a thing in
    Standard C? It would have to be 0, so the arithmetic
    would be useless anyway.

    I *think* that what you want could be obtained by
    casting the void* to char* and doing the arithmetic
    on that, then casting backing to void*.
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

  3. Default Re: Arithmetic on a void * pointer

    Paolo Montegriffo <paolo.montegriffo@bo.astro.it> wrote:

    > I know that I cannot perform arithmetic on a void * pointer, but can I
    > do something like:
    >
    > [...]
    >
    > void *base1;
    > void *p1;
    > size_t i, size;
    >
    > p1 = base1 + i * size;


    You cannot perform arithmetic on a void *. You know that. So why do you
    ask whether performing arithmetic on a void * is legal?

    > is it legal/dangerous/portable ?


    It is not C. If you want to do something similar, you can use unsigned
    char *s, possibly temporary ones.

    Richard
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

  4. Default Re: Arithmetic on a void * pointer

    > p1 = base1 + i * size;
    >


    I think it would be better to just cast p1 as a char *.

    p1 = ((char *)p1) + i * size;

    That way you are explicitly giving bytes.

    Jon
    ----
    Learn to program using Linux assembly language
    http://www.cafeshops.com/bartlettpublish.8640017
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

  5. Default Re: Arithmetic on a void * pointer

    Paolo Montegriffo wrote:
    >
    > Hi!
    >
    > I know that I cannot perform arithmetic on a void * pointer, but can I
    > do something like:
    >
    > [...]
    >
    > void *base1;
    > void *p1;
    > size_t i, size;
    >
    > p1 = base1 + i * size;
    >
    > [...]
    >
    > is it legal/dangerous/portable ?


    As you pointed out, you cannot perform arithmetic on a void*, and this is
    exactly what you are tying to do -- add "i*size" to the void* "base1".

    Pointer arithmetic scales to the size of the type being pointed to, so that
    adding 10 to an int* moves the pointer 10 ints. Since void*'s do not point
    to anything, you can't move the pointer by i*size units.

    You could cast to char* if you are trying to adjust the pointer by char
    units:

    p1 = ((char *)base1) + i*size;

    --
    +-------------------------+--------------------+-----------------------------+
    | Kenneth J. Brody | www.hvcomputer.com | |
    | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------------+
    Don't e-mail me at: <mailto:ThisIsASpamTrap@gmail.com>
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

  6. Default Re: Arithmetic on a void * pointer

    Paolo Montegriffo <paolo.montegriffo@bo.astro.it> writes:
    > I know that I cannot perform arithmetic on a void * pointer,


    Right.

    > but can I
    > do something like:
    >
    > [...]
    >
    > void *base1;
    > void *p1;
    > size_t i, size;
    >
    > p1 = base1 + i * size;
    >
    > [...]


    That looks like arithmetic on a void* pointer. It's illegal.

    If you want to perform arithmetic on void* pointers, you can cast them
    to char*, do the arithmetic, and cast back to void*.

    <OT>gcc allows arithetic on void* as an extension (a rather
    ill-advised one IMHO.</OT>

    --
    Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

  7. Default Re: Arithmetic on a void * pointer

    Paolo Montegriffo <paolo.montegriffo@bo.astro.it> wrote:
    > Hi!


    > I know that I cannot perform arithmetic on a void * pointer, but can I
    > do something like:


    No. What you're trying to do is exactly that: arithmetic on a (void
    *) object.

    > is it legal/dangerous/portable ?


    Yes (because there's no law requiring C code to be correct, anywhere)
    Yes (because it'll work on some compilers, even though it's wrong)
    No (because of ^^^^^)

    --
    Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
    Even if all the snow were burnt, ashes would remain.
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

  8. Default Re: Arithmetic on a void * pointer

    "Paolo Montegriffo" <paolo.montegriffo@bo.astro.it> wrote in message
    news:clcm-20050113-0018@plethora.net...
    > Hi!
    >
    > I know that I cannot perform arithmetic on a void * pointer, but can I
    > do something like:
    >
    > [...]
    >
    > void *base1;
    > void *p1;
    > size_t i, size;
    >
    > p1 = base1 + i * size;
    >
    > [...]
    >
    > is it legal/dangerous/portable ?


    No. Doing pointer arithmetic on voids in C means casting it back and forth
    between (char*) and (void*). It makes for obfuscated code, because one
    doesn't actually want to retrieve char's and the underlying data is unlikely
    to be char's. In D, this problem is resolved in that one can do arithmetic
    on void pointers, they just can't be dereferenced. It makes for simpler
    looking, more straightforward code.

    P.S. Implementing memory allocators is one instance where manipulating void*
    pointers into untyped memory blocks is appropriate.

    -Walter
    www.digitalmars.com free C, C++, D compilers
    "code of the nerds"
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

  9. Default Re: Arithmetic on a void * pointer

    On Thu, 13 Jan 2005 10:15:15 -0000, Paolo Montegriffo
    <paolo.montegriffo@bo.astro.it> wrote:

    >Hi!
    >
    >I know that I cannot perform arithmetic on a void * pointer, but can I
    >do something like:
    >
    >[...]
    >
    >void *base1;
    >void *p1;
    >size_t i, size;
    >
    >p1 = base1 + i * size;


    What type is base1? What kind of operator is +? What does the first
    sentence in your post mean? Is it applicable to this statement?
    >
    >[...]
    >
    >is it legal/dangerous/portable ?
    >
    >Thanks for your help
    >
    >Paolo Montegriffo




    <<Remove the del for email>>
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

  10. Default Re: Arithmetic on a void * pointer

    Hi,

    On Thu, 13 Jan 2005 10:15:15 -0000, Paolo Montegriffo said:
    > I know that I cannot perform arithmetic on a void * pointer,


    Correct.

    > but can I do something like:
    >
    > [...]
    >
    > void *base1;
    > void *p1;
    > size_t i, size;
    >
    > p1 = base1 + i * size;
    >
    > [...]


    Looks like arithmetic on a void * to me...

    > is it legal/dangerous/portable ?


    See your first sentence above.

    Cheers,
    Dave.

    --
    David Neary,
    E-Mail: bolsh at gimp dot org
    CV: http://dneary.free.fr/CV/
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

+ Reply to Thread
Page 1 of 4 1 2 3 ... LastLast

Similar Threads

  1. Problem on using a casted void pointer
    By Application Development in forum C
    Replies: 10
    Last Post: 09-24-2007, 04:41 AM
  2. void pointer
    By Application Development in forum C
    Replies: 0
    Last Post: 06-22-2007, 11:15 PM
  3. Pointer Arithmetic confusion
    By Application Development in forum c++
    Replies: 6
    Last Post: 06-22-2007, 09:45 AM
  4. pointer arithmetic with reinterpret_cast
    By Application Development in forum c++
    Replies: 3
    Last Post: 06-04-2007, 09:00 AM
  5. Pointer Difference Arithmetic
    By Application Development in forum c++
    Replies: 12
    Last Post: 08-15-2006, 07:08 PM