void* warnings

This is a discussion on void* warnings within the C Programming forums, part of the General Programming Boards category; Is there any way to get rid of warnings that say "pointer of type void * used in arithmetic?' my ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Location
    CA
    Posts
    19

    void* warnings

    Is there any way to get rid of warnings that say "pointer of type void * used in arithmetic?' my program still runs fine....i just want to know if there is a way to tell the terminal i['m using that i'm ok with doing arithmetic on void*'s (i can't cast them as any type because I am implementing a generic function..

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    How are you doing arithmetic with void*?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    CA
    Posts
    19
    In the code it's lines such as this:

    v is a vector struct that has {
    void* elems;
    int elemsize;
    int alloclength;
    int loclength;
    }

    The void* lines are stuff like this:
    void* vpt = v-> elems + 3 * v-> elemsize;

    ....etc

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    hmm... for one thing, multiplication is not defined where pointers are involved. Then, what does (v->elems + 3) mean when v->elems is a void*?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    CA
    Posts
    19
    oh i think i figured it out ...you cast everything to char* because what i am doing is counting bytes

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Always remember, the void pointer doesn’t have any scalar value. In other words the offset value. In order to dereference or to do any arithmetic operation on the void pointer u need a scalar or the offset value which the compiler doesn’t have it when you declare any pointer as void. Hence u need to cast it to what ever type you want to be. So when you cast it char * and apply a arithmetic operation; it would move the pointer a byte forward.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    When I think about it, is it defined to use arithmetic functions on void* pointers? Since void has no size, there's no given offset the compiler would add...

    But yes, you can cast all your void* to char* and do calculations and then cast back if necessary.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    When I think about it, is it defined to use arithmetic functions on void* pointers? Since void has no size, there's no given offset the compiler would add...

    But yes, you can cast all your void* to char* and do calculations and then cast back if necessary.
    It is not valid to use arithmetic with void pointers, hence the compiler errors.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    I saw "warning," not error, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I saw "warning," not error, though.
    Yes, I meant warnings - it is implementation dependant ("UB") what happens here.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    You could try to use void**, I think..

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    sizeof(void*) == probably 4 or 8, so no, it's a bad idea.
    @matsp: Aha, makes sense.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,628
    Well, it's valid, but useful is another story.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Warnings when using vector of vector
    By Boksha in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2008, 01:54 PM
  2. Compilers and warnings
    By rogster001 in forum C Programming
    Replies: 6
    Last Post: 03-26-2008, 05:16 AM
  3. Replies: 9
    Last Post: 03-14-2008, 09:55 AM
  4. Warnings from String manipulation functions.
    By Arker in forum C Programming
    Replies: 4
    Last Post: 10-14-2002, 11:59 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21