|
| 1 | +// clang-format off |
1 | 2 | /*****************************************************************//** |
2 | 3 | * \file pointer.c |
3 | 4 | * \brief Lab 7 pointers - Xuhua Huang |
4 | 5 | * |
5 | 6 | * \author Xuhua Huang |
6 | 7 | * \date October 2019 |
7 | 8 | *********************************************************************/ |
| 9 | +// clang-format on |
8 | 10 |
|
9 | 11 | #include <stdio.h> |
10 | 12 |
|
11 | | - /* Function Prototype */ |
| 13 | +/* Function Prototype */ |
12 | 14 | int my_math(int, int, int* pProduct); |
13 | 15 |
|
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; |
81 | 80 | } |
82 | 81 |
|
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 |
86 | 84 |
|
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 |
88 | 86 |
|
89 | | - return sum; |
| 87 | + return sum; |
90 | 88 | } |
0 commit comments