By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,109 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,109 IT Pros & Developers. It's quick & easy.

What is the point of pointers?

P: 7
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.


Enter two integers: 23 57
Both values are now: 57 57

I have the code, which I have already finished here:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  3. int max_value(int* x, int* y);
  5. int main()
  6. {
  7.     int x, y;
  9.     printf("Hello, please enter two integers.\n");
  10.     scanf("%d %d", &x, &y);
  12.     max_value(&x, &y);
  14.     printf("Both values are now: %d %d.\n", x, y);
  16.     return 0;
  17. }
  19. int max_value(int* x, int* y)
  20. {
  21.     int higher;
  23.     if (*x > *y)
  24.         higher = *x;
  25.     else
  26.         higher = *y;
  27.     *x = *y = higher;
  28. }
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?
Jan 5 '17 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 9,197
I remember when I was exactly where you are now. I just couldn't get it. After a while though, I saw the value of a pointer. Generally, speaking a lot of C is loaded with pointers and they are a source of errors,

Here's a scenario:

I am a calling function and I need to know the weather forecast. I realize forecasts vary in length so I can't allocate the correct amount of memory ahead of time. So I will pass you an address of a char pointer. Just allocate memory yourself, copy the forecast to it and return the address of the memory you allocated inside the pointer I gave you.

Expand|Select|Wrap|Line Numbers
  1. void Meteorologist()
  2. {
  3.    char* forecast;
  4.    GetForecast(&forecast);
  5.    printf("The forecast is: %s\n", forecast);   << you will see It's gonna rain
  6. }
  9. void GetForecast(char** result)
  10. {
  11.    *result = malloc(80 * sizeof(char));
  12.    strcpy(*result, "It's gonna rain");
  13. }
Spend some time learning about the address-of and dereference operator.

Post again of you need more info.
Jan 6 '17 #2

P: 3
At a high level, compilers use pointers as a more efficient way of doing their job. Simply stated, a pointer points to a piece of computer storage (memory), just like an address tells you where your home is located.

If the compilers had to move chunks of data around, instead of simply "pointing to them", programs would run much more slowly.
Jan 6 '17 #3

P: 7
Hi weaknessforcats and DGrund! Thank you for the clarification! I have been doing a lot more practice and it is starting to make more and more sense! :-)
Jan 8 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.