Thread: Void Pointers

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Void Pointers

    As per my knowledge , arithmetic operations on void pointers is not allowed unless properly typecasted.

    Code:
    int main(){
        float i = 10;
        void *k;
        k = &i;
        printf("%u \n" , k);
        k = k-4;
        printf("%u\n" ,k);
        getch();
        return 0;
    }
    However the above code always increments , decrements the void *k properly without any error or warning.
    For a single increment , it takes the address value from, example 227315 to 227316.
    Hows this thing working ?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Using gcc? It's non-standard gcc extension. don't use it.
    GCC will do pointer arith on void pointer.(as char pointer).
    Try to compile with -Wall -pedantic-errors -std=c89 # or -std=c99

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Take note that it only incremented by 1 byte (the default) not the size of a float.

    A void pointer has no explicit type.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Turn up the warning levels of your compiler. For example, to trigger the relevant warning when compiling with the MinGW port of gcc 3.4.5, I had to use the -pedantic option.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix operations using objects
    By circuitbreaker in forum C++ Programming
    Replies: 7
    Last Post: 05-04-2010, 08:53 AM
  2. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 04:14 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM