Skip to content

Commit deeffcc

Browse files
author
XuhuaHuang
committed
Update notes on void pointer
1 parent 6cf9bf4 commit deeffcc

File tree

4 files changed

+166
-109
lines changed

4 files changed

+166
-109
lines changed

RawPointer/CMakeLists.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
cmake_minimum_required(VERSION 3.00)
1+
cmake_minimum_required(VERSION 3.20)
22

3-
project("raw_pointer")
3+
project("raw_pointer" LANGUAGES C CXX)
44

5-
set(CMAKE_C_STANDARD 11)
5+
set(CMAKE_C_STANDARD 17)
66
set(CMAKE_C_STANDARD_REQUIRED OFF)
77

8+
set(CMAKE_CXX_STANDARD 23)
9+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
10+
811
add_executable(raw_pointer_c "pointer.c")
912
add_executable(void_pointer_c "void_ptr.c")
13+
add_executable(void_pointer_cpp "void_ptr.cpp")
14+
15+
target_compile_features(raw_pointer_c PRIVATE c_std_17)
16+
target_compile_features(void_pointer_c PRIVATE c_std_17)
17+
target_compile_features(void_pointer_cpp PRIVATE cxx_std_23)

RawPointer/pointer.c

Lines changed: 71 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,88 @@
1+
// clang-format off
12
/*****************************************************************//**
23
* \file pointer.c
34
* \brief Lab 7 pointers - Xuhua Huang
45
*
56
* \author Xuhua Huang
67
* \date October 2019
78
*********************************************************************/
9+
// clang-format on
810

911
#include <stdio.h>
1012

11-
/* Function Prototype */
13+
/* Function Prototype */
1214
int my_math(int, int, int* pProduct);
1315

14-
int main(void)
15-
{
16-
/* Q1 Pointing to integers */
17-
int x = 5;
18-
int y = 8;
19-
20-
int* ptrToInt = &x; // initialize the pointer to the location of int x
21-
printf("The value of variable x is %d.\n", *ptrToInt);
22-
23-
ptrToInt = &y; // change the pointer to the location of int y
24-
printf("\nThe value of variable y is %d.\n", *ptrToInt);
25-
26-
*ptrToInt = 7;
27-
printf("\nThe value of the pointer has been changed to %d\n", *ptrToInt);
28-
printf("\nThe corresponding value of variable y is %d.\n", y); // note: printing the value of y should give us 7
29-
30-
y = 2; // overwriting value of y
31-
printf("The pointer contains the address of variable of y has the value of %d.\n\n", *ptrToInt);
32-
33-
/* Q2 Pointing to an array */
34-
int array[10] = { 0,1,2,3,4,5,6,7,8,9 }; // declaring an array
35-
36-
int index = 0;
37-
//ptrToInt = &array[index]; I was trying to do something fancier
38-
printf("The program lits the array below using a for loop.\n");
39-
for (index = 0; index < 10; index++)
40-
{
41-
printf("%d\n", array[index]);
42-
} // this for loop prints the int array
43-
44-
int* ptrToArr = &array[0]; // declaring a pointer and initializing it to the first element in the array indexed 0
45-
// right side of the equal sign is equivalent to `= array`
46-
ptrToArr++; // this should increment the pointer ptrToArr to array [1]
47-
printf("\nThe element in the array after the pointer has been incremented is %d.\n\n", *ptrToArr);
48-
49-
ptrToArr = array; // ptrToArr was incremented to array[1]; this brings it back to array[0]
50-
// the pointer need to increment in the for loop
51-
printf("The program lists the array by incrementing the value of pointer.\n");
52-
for (int i = 0; i < 10; i++)
53-
{
54-
printf("%d\n", *ptrToArr);
55-
ptrToArr++;
56-
} // this for loop prints the int array using pointer
57-
58-
/* Q3 Functions and pointers */
59-
int userIn1; // declaring two int type variable to store user's input
60-
int userIn2;
61-
62-
int product;
63-
64-
// gather user input with scanf() function
65-
printf("\nPlease enter the first integer that you want to add\n");
66-
scanf_s("%d", &userIn1); // asking inputs from the user
67-
printf("\nPlease enter the second integer that you want to add\n");
68-
scanf_s("%d", &userIn2);
69-
70-
int result = my_math(userIn1, userIn2, &product); // calling the function
71-
// &product is the address of variable product
72-
printf("\nThe summation of these two integers is %d.\n", result);
73-
printf("The product of these two integers is %d\n", product);
74-
75-
/*
76-
The following line is the original one to call the function without the pointer
77-
int result = my_math (userIn1, userIn2);
78-
*/
79-
80-
return 0;
16+
int main(void) {
17+
/* Q1 Pointing to integers */
18+
int x = 5;
19+
int y = 8;
20+
21+
int* ptrToInt = &x; // initialize the pointer to the location of int x
22+
printf("The value of variable x is %d.\n", *ptrToInt);
23+
24+
ptrToInt = &y; // change the pointer to the location of int y
25+
printf("\nThe value of variable y is %d.\n", *ptrToInt);
26+
27+
*ptrToInt = 7;
28+
printf("\nThe value of the pointer has been changed to %d\n", *ptrToInt);
29+
printf("\nThe corresponding value of variable y is %d.\n", y); // note: printing the value of y should give us 7
30+
31+
y = 2; // overwriting value of y
32+
printf("The pointer contains the address of variable of y has the value of %d.\n\n", *ptrToInt);
33+
34+
/* Q2 Pointing to an array */
35+
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // declaring an array
36+
37+
int index = 0;
38+
// ptrToInt = &array[index]; I was trying to do something fancier
39+
printf("The program lits the array below using a for loop.\n");
40+
for (index = 0; index < 10; index++) {
41+
printf("%d\n", array[index]);
42+
} // this for loop prints the int array
43+
44+
int* ptrToArr = &array[0]; // declaring a pointer and initializing it to the first element in the array indexed 0
45+
// right side of the equal sign is equivalent to `= array`
46+
ptrToArr++; // this should increment the pointer ptrToArr to array [1]
47+
printf("\nThe element in the array after the pointer has been incremented is %d.\n\n", *ptrToArr);
48+
49+
ptrToArr = array; // ptrToArr was incremented to array[1]; this brings it back to array[0]
50+
// the pointer need to increment in the for loop
51+
printf("The program lists the array by incrementing the value of pointer.\n");
52+
for (int i = 0; i < 10; i++) {
53+
printf("%d\n", *ptrToArr);
54+
ptrToArr++;
55+
} // this for loop prints the int array using pointer
56+
57+
/* Q3 Functions and pointers */
58+
int userIn1; // declaring two int type variable to store user's input
59+
int userIn2;
60+
61+
int product;
62+
63+
// gather user input with scanf() function
64+
printf("\nPlease enter the first integer that you want to add\n");
65+
scanf("%d", &userIn1); // asking inputs from the user
66+
printf("\nPlease enter the second integer that you want to add\n");
67+
scanf("%d", &userIn2);
68+
69+
int result = my_math(userIn1, userIn2, &product); // calling the function
70+
// &product is the address of variable product
71+
printf("\nThe summation of these two integers is %d.\n", result);
72+
printf("The product of these two integers is %d\n", product);
73+
74+
/*
75+
The following line is the original one to call the function without the pointer
76+
int result = my_math (userIn1, userIn2);
77+
*/
78+
79+
return 0;
8180
}
8281

83-
int my_math(int x, int y, int* pProduct)
84-
{
85-
int sum = x + y; // calculating the sum of two integers
82+
int my_math(int x, int y, int* pProduct) {
83+
int sum = x + y; // calculating the sum of two integers
8684

87-
*pProduct = x * y; // the product of x and y is written to the address of pProduct
85+
*pProduct = x * y; // the product of x and y is written to the address of pProduct
8886

89-
return sum;
87+
return sum;
9088
}

RawPointer/void_ptr.c

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,37 @@
1717
#include <stdlib.h>
1818

1919
int main(void) {
20-
void* vp;
21-
int* ip;
22-
23-
/* print out the size of pointers */
24-
printf("sizeof(void*) -> %zu\n", sizeof(vp));
25-
printf("sizeof(int*) -> %zu\n", sizeof(ip));
26-
27-
int x = 0xFEEDBEEF;
28-
ip = &x;
29-
vp = ip;
30-
31-
/* print out the address of both p and ip */
32-
printf("vp -> %p\n", vp);
33-
printf("ip -> %p\n", ip);
34-
35-
/* dereference the int* and assign it a new value */
36-
*ip = 0x00C0FFEE;
37-
//*p = 0xDEADBEEF; // void is not assignable. no type information
38-
*(int*)vp = 0xDEADBEEF; // this assignment will overwrite the 0xCOFFEE assignment
39-
// because both *p and *ip point to the same address
40-
// therefore, they both read the latest assignment
41-
42-
/* print out hexadecimal value */
43-
printf("vp -> %p, with value of %x\n", vp, *(int*)vp);
44-
printf("ip -> %p with value of %x\n", ip, *ip);
45-
46-
/* c and cpp differ with the following lines */
47-
// int* ip2 = malloc(sizeof(int)); // error in cpp, requires explicit conversion
48-
// prefer the explicit conversion for portability and readability
49-
int* ip2 = (int*)malloc(sizeof(int));
50-
free(ip2);
51-
52-
return EXIT_SUCCESS;
20+
void* vp;
21+
int* ip;
22+
23+
/* print out the size of pointers */
24+
printf("sizeof(void*) -> %zu\n", sizeof(vp));
25+
printf("sizeof(int*) -> %zu\n", sizeof(ip));
26+
27+
int x = 0xFEEDBEEF;
28+
ip = &x;
29+
vp = ip;
30+
31+
/* print out the address of both p and ip */
32+
printf("vp -> %p\n", vp);
33+
printf("ip -> %p\n", ip);
34+
35+
/* dereference the int* and assign it a new value */
36+
*ip = 0x00C0FFEE;
37+
//*p = 0xDEADBEEF; // void is not assignable. no type information
38+
*(int*)vp = 0xDEADBEEF; // this assignment will overwrite the 0xCOFFEE assignment
39+
// because both *p and *ip point to the same address
40+
// therefore, they both read the latest assignment
41+
42+
/* print out hexadecimal value */
43+
printf("vp -> %p, with value of %x\n", vp, *(int*)vp);
44+
printf("ip -> %p with value of %x\n", ip, *ip);
45+
46+
/* c and cpp differ with the following lines */
47+
// int* ip2 = malloc(sizeof(int)); // error in cpp, requires explicit conversion
48+
// prefer the explicit conversion for portability and readability
49+
int* ip2 = (int*)malloc(sizeof(int));
50+
free(ip2);
51+
52+
return EXIT_SUCCESS;
5353
}

RawPointer/void_ptr.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* @file void_ptr.cpp
3+
* @author Xuhua Huang
4+
* @brief
5+
* @version 0.1
6+
* @date 2026-01-18
7+
*
8+
* @copyright Copyright (c) 2026
9+
*
10+
*/
11+
12+
#include <cstddef>
13+
#include <cstdlib>
14+
#include <iostream>
15+
16+
int main() {
17+
std::nullptr_t np = nullptr;
18+
void* vp = nullptr;
19+
int* ip = nullptr;
20+
21+
// print out the size of pointers
22+
std::cout << "sizeof(void*) -> " << sizeof(vp) << std::endl;
23+
std::cout << "sizeof(int*) -> " << sizeof(ip) << std::endl;
24+
25+
int x = 0xFEEDBEEF;
26+
ip = &x;
27+
vp = ip;
28+
29+
// print out the address of both p and ip
30+
std::cout << "vp -> " << vp << std::endl;
31+
std::cout << "ip -> " << ip << std::endl;
32+
33+
// dereference the int* and assign it a new value
34+
*ip = 0x00C0FFEE;
35+
// *p = 0xDEADBEEF; // void is not assignable. no type information
36+
*(static_cast<int*>(vp)) = 0xDEADBEEF; // this assignment will overwrite the 0xCOFFEE assignment
37+
// because both *p and *ip point to the same address
38+
// therefore, they both read the latest assignment
39+
40+
// print out hexadecimal value
41+
std::cout << "vp -> " << vp << ", with value of " << std::hex << *(static_cast<int*>(vp)) << std::endl;
42+
std::cout << "ip -> " << ip << " with value of " << std::hex << *ip << std::endl;
43+
44+
// c and cpp differ with the following lines
45+
// int* ip2 = malloc(sizeof(int)); // error in cpp, requires explicit conversion
46+
// prefer the explicit conversion for portability and readability
47+
int* ip2 = static_cast<int*>(std::malloc(sizeof(int)));
48+
std::free(ip2);
49+
50+
return 0;
51+
}

0 commit comments

Comments
 (0)