Thread: Assigning void* to a variable

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    1

    Assigning void* to a variable

    I am working on a testing tool called RTRT. There is a line of testing as,

    FORMAT buffer = void*

    what happens after the above line is executed. Will buffer variable be used to point to the start of a particular size of memory? if yes, what will be the size of the memory?

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I try to steer away from void pointers, but from what I have read, all pointer types can be assigned a pointer to void. In C, a cast operation is not required (though I've read it is required in C++). A pointer to void cannot be dereferenced. The number of bytes a void pointer refers to is not known by the compiler.

    I don't know what FORMAT means, but if this is just an assignment, I don't think it will really do anything. It may just be assigned to a void pointer because the author doesn't like to have pointers that don't point to anything.

    This is just a wild guess, but if you look at the top of wherever you got that code, you may see something like this:
    Code:
    #define FORMAT mystery
    The mystery portion of this might tell you more about what that line of code means.
    Last edited by jack jordan; 11-16-2017 at 03:22 AM.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by boonflies View Post
    I am working on a testing tool called RTRT. There is a line of testing as,

    FORMAT buffer = void*

    what happens after the above line is executed. Will buffer variable be used to point to the start of a particular size of memory? if yes, what will be the size of the memory?
    I don't know what "RTRT" is, or what "FORMAT" is.

    A "void *" is a generic pointer that can be assigned to any pointer type. For example, malloc() returns a void *.
    Code:
    // ...
    char *ptr = NULL;
    ptr = malloc(256);
    // ....
    In previous C Standards malloc() would return a "char *" that would need to be cast to the type of the pointer being assigned. It is also used in functions like qsort()

    @jack jordan
    I try to steer away from void pointers
    Don't "steer clear" of them! Learn what they are and how to use them properly!

  4. #4
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    In understanding void*, I have my own curiosity. From the OP's code, it isn't clear what the line of code is doing because FORMAT is not known, but what can be assumed? I think
    Code:
    FORMAT buffer
    is an lvalue and
    Code:
    void*
    is supposed to be an rvalue. However, I do not believe void* in itself can be an rvalue. At least in my IDE, it highlights it as an error. I think in order for void* to be an rvalue, it needs to call something that returns a value much like malloc. I think I had assumed that void* could just be assigned to a pointer to make it deliberately point to nothing rather than a random something. Because of this, I was a little confused as to what value it might have over NULL. I don't believe this is the case now as I can't find any examples of code such as
    Code:
    int ptr = void*;
    I guess I am now assuming these examples do not exist, and this kind of code is a syntax error.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    The OP's code is not legal C code. It is part of "Rational Test Realtime", an IBM product.

    As for "void *". This is a C data type, not an rvalue or lvalue.
    Code:
    int x = 10;
    int y = 0;
    
    // In the following statement, x is an rvalue, the value stored in x, or the integer 10.
    // y is an lvalue, or basically the location where to store the rvalue, 10.
    
    y = x;
    
    // Now the integer y is equal to the integer x.
    // lvalues MUST be a variable that can be assigned.
    // rvalues can be variables, constants, MACROS, a math expression, or the return value from a function call.

    malloc() does return a "void *" rvalue, and can be assigned to an lvalue object, as I shown in my previous post.

    You need to go back and study pointers as defined in C99 and C11 C Programming Standards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Private Variable assigning
    By Trey Brumley in forum C++ Programming
    Replies: 7
    Last Post: 03-22-2013, 07:31 AM
  2. Assigning a name to a variable
    By PAragonxd in forum C++ Programming
    Replies: 14
    Last Post: 12-13-2007, 05:35 AM
  3. assigning a string to a variable
    By xp5 in forum C Programming
    Replies: 12
    Last Post: 08-30-2007, 01:13 AM
  4. Assigning to Void Pointer
    By coder8137 in forum C Programming
    Replies: 7
    Last Post: 12-06-2006, 01:38 PM
  5. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM

Tags for this Thread