Void pointers

This is a discussion on Void pointers within the C Programming forums, part of the General Programming Boards category; Hello, I am using VC++ and I am testing some C code. I am tying to pass a void pointer ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    45

    Void pointers

    Hello,

    I am using VC++ and I am testing some C code. I am tying to pass a void pointer !!! I was wondering if someone can help me with the following code sample. I get an "unhandled exception" error at the following line:

    P->a = 9;

    Here is the full sample code:

    Code:
    #include <stdio.h>
    
    typedef struct tag1{
    int a;
    }t1;
    
    typedef struct tag2{
    int b;
    }t2;
    
    
    void f1(void *i)
    {
    t1 *P;
    P = (t1*)i;         // <<< Type cast!
    P->a = 9;          // <<< error here???
    }
    
    
    int main()
    {
    int x;
    t1 *p = NULL;
    t2 *q = NULL;
    
    f1(p);
    x = p->a;
    }
    Thankyou all in advance for your help!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You are compiling C code as C++ code. In C, void pointers are tremendously useful things. In C++, they are hamstrung by someone's stupid idea of "type safety".

    If you compile that code as C, not C++, it should not produce an error.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    That isn't the problem. You are passing into the function p, which is NULL. And so you try to dereference a null pointer, which is your problem.
    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.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Quote Originally Posted by MK27
    If you compile that code as C, not C++, it should not produce an error.
    Yeah, instead, it would print "Hello world!"

    see the big C: the problem is that p in the main function is a null pointer. You pass it to the f1 function, which then indirectly dereferences it, resulting in undefined behaviour.

    EDIT:
    Ah, so Elysia beat me to it. No matter, it clearly shows that MK27 is hamstrung by his notion of C++ being hamstrung by an allegedly 'stupid idea of "type safety"'
    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
    Jun 2010
    Posts
    45
    Yup, Laserlight and Elysia... you are both right... a minor modification did the trick:

    Code:
    #include <stdio.h>
    
    typedef struct tag1{
    int a;
    }t1;
    
    typedef struct tag2{
    int b;
    }t2;
    
    void f1(void *i)
    {
    t1 *P;
    
    P = (t1*)i;
    P->a = 9;
    }
    
    int main()
    {
    int o;
    t1 x;
    t2 y;
    
    f1(&x);
    o = x.a;
    }
    Which takes me to my next question as to why f1() takes a void* arg. I will start a new thread for that one though.

    Thanks everyone for your help!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    When you post code in your new thread, please indent it properly.
    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

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    No matter, it clearly shows that MK27 is hamstrung by his notion of C++ being hamstrung by an allegedly 'stupid idea of "type safety"'
    Well, it probably did lead me straight to the wrong conclusion there anyway, since this has happened in the past I think with VC++ users.

    All apologies.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

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, 03:14 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM

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