-
June 10th, 2014, 01:54 PM
#1
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;
}
-
June 10th, 2014, 02:17 PM
#2
Re: void pointer
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.
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.
-
June 10th, 2014, 02:58 PM
#3
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.
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;
*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
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
rather than
both are syntactially correct, but the basic type here is char - not char*. If you write
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
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)
-
June 10th, 2014, 04:40 PM
#4
Re: void pointer
Originally Posted by 2kaud
[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
-
June 10th, 2014, 07:46 PM
#5
Re: void pointer
Originally Posted by jsmith613
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.
-
June 10th, 2014, 10:45 PM
#6
Re: void pointer
Originally Posted by 2kaud
As a point re layout style, I would recommend
rather than
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:
instead of:
and this is what I recommend for style in C++.
Originally Posted by 2kaud
If you write
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
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.)
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.
-
June 11th, 2014, 04:23 AM
#7
Re: void pointer
thanks all for sticking with me.
-
June 11th, 2014, 04:34 AM
#8
Re: void pointer
because of type, it makes sense to write:
instead of:
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)
-
June 12th, 2014, 04:44 AM
#9
Re: void pointer
Originally Posted by jsmith613
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
-
June 12th, 2014, 05:12 AM
#10
Re: void pointer
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|