So I have a question about pointers. In my homework assignment (I AM NOT ASKING FOR HELP WITH THE ASSIGNMENT, JUST AN EXPLANATION ABOUT SOMETHING) my teachers specifies that I must use pointer variables. I am not sure if this is just because he wants to know that I can use it, or if the program would not work without pointers.
The assignment wants me to create a program that prompts the user to enter in two integers. Then, I needed to create a function that would essentially change the values of both variables to the one with the higher value.
example:
Enter two integers:
23 57
Both values are now:
57 57
I have the code, which I have already finished here:
-
#include <stdio.h>
-
-
int max_value(int* x, int* y);
-
-
int main()
-
{
-
int x, y;
-
-
printf("Hello, please enter two integers.\n");
-
scanf("%d %d", &x, &y);
-
-
max_value(&x, &y);
-
-
printf("Both values are now: %d %d.\n", x, y);
-
-
return 0;
-
}
-
-
int max_value(int* x, int* y)
-
{
-
int higher;
-
-
if (*x > *y)
-
higher = *x;
-
else
-
higher = *y;
-
*x = *y = higher;
-
}
-
Could you explain exactly where a pointer is needed, if any place at all? Where would the program get the variables' values mixed up and need pointers? If there isn't any place that requires pointers then what is the point of pointers? I understand that it takes up less space and it more accurate, but as far as google says, this is mostly for large, complicated programs. So where would it come in handy for student that is just learning programming?