Thread: void datatype

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    void datatype

    ok ... the trouble started with sizeof(void) = 1

    on similar terms sizeof empty class/structure in C++ is also 1

    Bjarne Stroustrup's FAQ reasons: "To ensure that the addresses of two different objects will be different. " taken from here

    but I cannot use the same reasoning for void ... since I cannot create variables of void type

    void x ; <does not hold>

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > the trouble started with sizeof(void) = 1
    Says who?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( ) {
        printf("%d\n", sizeof(void) );
        return 0;
    }
    gcc reports
    foo.c:5: warning: invalid application of `sizeof' to a void type

    vc6 reports either
    foo.c(5) : warning C4034: sizeof returns 0
    foo.c(5) : error C2070: illegal sizeof operand

    You can't have a variable with void type, even if your compiler apparently gives it a non-zero size.

    > on similar terms sizeof empty class/structure in C++ is also 1
    But C can't have an empty struct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    thank you! , solves all the troubles

    btw... what is the difference between -pedantic and -Wall arguments in gcc ?

    I have been using -Wall instead of -pedantic ... so the warnings were bypassed

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    The gcc manual page explains all.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. game window rejected painting !
    By black in forum Windows Programming
    Replies: 4
    Last Post: 03-27-2007, 01:10 AM
  5. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM