Void Pointers

This is a discussion on Void Pointers within the C Programming forums, part of the General Programming Boards category; As per my knowledge , arithmetic operations on void pointers is not allowed unless properly typecasted. Code: int main(){ float ...

  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
    22,159
    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.
    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

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, 09: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, 12:29 PM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04: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

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