CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: void pointer

  1. #1
    Join Date
    Jun 2014
    Posts
    7

    void pointer

    I am working through the C++ guide from cplusplus.com. I do not really understand the following code (esepcially the emboldened code). What exactly is the emboldened section doing?

    please could someone walk me through what the int main () is doing with the function and what the function does.

    This is what I understand:
    1) variable a is defined as a char and assigned value x
    2) vaiable b is defined as int and assigned value 1602
    3) increase (&a, sizeof(a)) assigns &a to data and 1 to psize (as char = 1)
    4) the function defines a pointer variable pchar
    5) pchar = (char*)data - this line is the most confusing. Firstly what is it doing. second, why could it not read char*data?
    6)++(*pchar) increases the location that pchar points to by 1

    Code:
     #include <iostream> 
    using namespace std; 
     
    void increase (void* data, int psize) 
    { 
     if ( psize == sizeof(char) ) 
     { 
         char* pchar; 
         pchar= (char*)data; 
         ++(*pchar); 
     } 
     else if (psize == sizeof(int) ) 
     { 
         int* pint; 
         pint=(int*)data; 
         ++(*pint); 
     } 
    } 
     
    int main () 
    { 
     char a = 'x'; 
     int b = 1602; 
     increase (&a,sizeof(a)); 
     increase (&b,sizeof(b)); 
     cout << a << ", " << b << endl; 
     return 0; 
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: void pointer

    Quote Originally Posted by jsmith613
    5) pchar = (char*)data - this line is the most confusing. Firstly what is it doing. second, why could it not read char*data?
    The pointer to void named data points to something. That cast interprets that something as being a char, making pchar point to a char. The reason why data is not a pointer to void is demonstrated by this line in main:
    Code:
    increase (&b,sizeof(b));
    &b is a pointer to int, which is convertible to a pointer to void, but not to a pointer to char (unless you use a cast).

    The reason why pchar and the cast was used is that a pointer to void cannot be dereferenced and, in C++, requires a cast to be converted to a pointer to some object type.

    Quote Originally Posted by jsmith613
    6)++(*pchar) increases the location that pchar points to by 1
    This statement could be a little misleading. I would say that it increases the value of what pchar points to by 1.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: void pointer

    Code:
    increase (&a,sizeof(a));
    this passes the address of the variable a as the first parameter.

    Code:
    void increase (void* data, int psize)
    data is defined as being a pointer to a type void and requires an address to be passed (as from &a - address of a). As the type of data is pointer to void in the function definition, a pointer to any type can be passed.

    Code:
    char* pchar;
    pchar is of type pointer to a char (eg a memory address).
    Code:
         pchar= (char*)data;
    data is of type pointer to void which is of a different type to pointer to char. Therefore a cast is used to 'convert' from pointer to void to pointer to char. pchar is set to the memory address passed in as data and will treat the memory to which it points as holding chars. These two lines could have been combined to give
    Code:
         char* pchar = (char*)data;
    Code:
         ++(*pchar);
    *pchar dereferences the memory pointer to by pchar to give the char to which the memory pchar points. This char is then incremented.

    The brackets are not required and this statement could have been written
    Code:
         ++*pchar;
    but this is probably not as 'readable' as with the brackets. Incidentially, if you move the ++ from the front to before the ; in both versions do you get the same or different results? Why?

    As a point re layout style, I would recommend
    Code:
    char *pchar;
    rather than
    Code:
    char* pchar;
    both are syntactially correct, but the basic type here is char - not char*. If you write
    Code:
    char* pch1, pch2;
    only pch1 is a pointer to a char. pch2 is just a char. To have both pch1 and pch2 pointers to char you would write
    Code:
    char *pch1, *pch2;
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Jun 2014
    Posts
    7

    Re: void pointer

    Quote Originally Posted by 2kaud View Post
    [code]

    Code:
         pchar= (char*)data;
    data is of type pointer to void which is of a different type to pointer to char. Therefore a cast is used to 'convert' from pointer to void to pointer to char. pchar is set to the memory address passed in as data and will treat the memory to which it points as holding chars. These two lines could have been combined to give
    Code:
         char* pchar = (char*)data;

    thanks...the only thing I don't get is why we NEED brackets for (char*)data
    thanks

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: void pointer

    Quote Originally Posted by jsmith613 View Post
    thanks...the only thing I don't get is why we NEED brackets for (char*)data
    thanks
    That's called a cast. It tells the compiler to treat the variable as a different type. data was declared as a void pointer, but you're telling the compiler to pretend it's a char pointer in that statement.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: void pointer

    Quote Originally Posted by 2kaud
    As a point re layout style, I would recommend
    Code:
    char *pchar;
    rather than
    Code:
    char* pchar;
    both are syntactially correct, but the basic type here is char - not char*.
    The type of pchar is char*, not char, so the notion of "basic type" here is irrelevant. What you probably meant to say is that the * binds to the name pchar in the grammar. Nonetheless, because of type, it makes sense to write:
    Code:
    char* pchar;
    instead of:
    Code:
    char *pchar;
    and this is what I recommend for style in C++.

    Quote Originally Posted by 2kaud
    If you write
    Code:
    char* pch1, pch2;
    only pch1 is a pointer to a char. pch2 is just a char. To have both pch1 and pch2 pointers to char you would write
    Code:
    char *pch1, *pch2;
    Yes, but because of the increased likelihood of a typo error or confusion in the event that it is not a typo error, I recommend separating the declarations:
    Code:
    char* pch1;
    char* pch2;
    Refer to Stroustrup's answer to the FAQ Is ``int* p;'' right or is ``int *p;'' right? for further reading. (Incidentally, for style in C, I use your recommendation precisely because I agree with Stroustrup's assessment of the situation.)

    Quote Originally Posted by jsmith613
    the only thing I don't get is why we NEED brackets for (char*)data
    As GCDEF stated, that is a C-style cast. Alternatively, you could use a C++-style cast, e.g.,
    Code:
    pchar = static_cast<char*>(data);
    Which has the benefit of being easier to spot and more descriptive.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jun 2014
    Posts
    7

    Re: void pointer

    thanks all for sticking with me.

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: void pointer

    because of type, it makes sense to write:
    Code:
    char* pchar;
    instead of:
    Code:
    char *pchar;
    and this is what I recommend for style in C++.
    I've come across too many times code like this
    Code:
    char* pch1, pch2, pch3;
    where the programmer has 'assumed' that pch1, pch2 and pch3 are all pointers to a char whereas only pch1 is a pointer to a char. IMO that is why I always suggest putting the '*' before the variable name - not withstanding the comments made by Stroustrup. The above then becomes
    Code:
    char *pch1, *pch2, *pch3;
    Personally I don't like multiple variable definitons on a single line for this reason. Either
    Code:
    char *pch1,
         *pch2,
         *pch3;
    or
    Code:
    char *pch1;
    char *pch2;
    char *pch3;
    or even
    Code:
    typedef char* Pchar;
    
    Pchar pch1, pch2, pch3;
    but this is really all about preference.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: void pointer

    Quote Originally Posted by jsmith613 View Post
    thanks...the only thing I don't get is why we NEED brackets for (char*)data
    thanks
    Of course, if you want to do it the C++ way then you would use

    reinterpret_cast<char*>(data);
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: void pointer

    Quote Originally Posted by JohnW@Wessex
    Of course, if you want to do it the C++ way then you would use

    reinterpret_cast<char*>(data);
    As I noted earlier, static_cast will suffice.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured