- 1). Load the C++ IDE by clicking on its program icon. When it opens, select "File/New/Project"and choose "C++ Project" to create a new C++ project. A blank source code file appears in the text editor portion of the IDE.
- 2). Create a main function, which is where your program will begin. Write the following at the top of the source code file:
int main()
{} - 3). Declare an "int" variable by writing the following within the curly brackets of the main function:
int x = 500; - 4). Declare a "void" pointer by writing the following below the statement written in the previous step:
void *voidPtr; - 5). Assign the address of the "int" data type "x" to the "void" pointer. Write the following after the "void" pointer declaration:
voidPtr = &x; - 6). Declare an "int" pointer that stores the address held by the "void" pointer. This will allow you to treat the address of the "void" pointer as an "int" data type. You can accomplish this by writing the following statement below the one written in the previous step:
int *intPtr = (int*)voidPtr; - 7). Print out the value located at the address pointed to by the pointer "intPtr" by writing the following:
cout << *intPtr << endl; - 8). Execute the program by clicking the green "Play" button located on the top row of buttons. The program stores the address of the "int" variable "x" inside the "void" pointer. The "void" pointer is then accessed through the use of the "int" pointer "intPtr," and its value of "500" is printed out to the output console.
previous post