questions about "void"...

This is a discussion on questions about "void"... within the C++ Programming forums, part of the General Programming Boards category; What does "void" do in a program?...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    30

    questions about "void"...

    What does "void" do in a program?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    24
    void doesnt do anything, thats why its void

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What does "void" do in a program?
    It defines a type that is completely generic (with restrictions), or defines a function that returns no value. In function parameter lists, void means that there are no parameters.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    A type that is completly generic? So, I could have a variable of type void? What could it hold?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So, I could have a variable of type void? What could it hold?
    Note that I said with restrictions. You can't declare a variable of type void, it has to be void *, and that makes it a generic pointer which can be converted into any type. Note that the syntax is a nightmare though.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    How large is type void?

    So... would the following be considered correct syntax?

    void *vVariable;

    vVariable = new char[256];

    -or-

    vVariable = &cVariable;

  7. #7
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    How large is type void?
    all pointers regardless of type store the same data, an address. So all pointers are the same size, that of an int.
    Last edited by C_Coder; 04-13-2002 at 08:24 AM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How large is type void?
    The size of an int.

    >So... would the following be considered correct syntax?
    >void *vVariable;
    >vVariable = new char[256];
    Yes, actually it would. The problem isn't in allocating memory for a void * but in working with one. To allocate memory to a void *, then assign a value to that memory and print requires syntax like this:
    Code:
    /* pseudocode */
    void *p = new int;
    *(int *)p = 27;
    cout<< *(int *)p <<"\n";
    or
    Code:
    /* pseudocode */
    void *p;
    int i = 27;
    p = &i;
    cout<< *(int *)p <<"\n";
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 06:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 08:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 08:22 AM

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