473,408 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

What is the point of pointers?

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:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. int max_value(int* x, int* y);
  4.  
  5. int main()
  6. {
  7.     int x, y;
  8.  
  9.     printf("Hello, please enter two integers.\n");
  10.     scanf("%d %d", &x, &y);
  11.  
  12.     max_value(&x, &y);
  13.  
  14.     printf("Both values are now: %d %d.\n", x, y);
  15.  
  16.     return 0;
  17. }
  18.  
  19. int max_value(int* x, int* y)
  20. {
  21.     int higher;
  22.  
  23.     if (*x > *y)
  24.         higher = *x;
  25.     else
  26.         higher = *y;
  27.     *x = *y = higher;
  28. }
  29.  
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
3 1434
weaknessforcats
9,208 Expert Mod 8TB
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. }
  7.  
  8.  
  9. void GetForecast(char** result)
  10. {
  11.    *result = malloc(80 * sizeof(char));
  12.    strcpy(*result, "It's gonna rain");
  13. }
  14.  
Spend some time learning about the address-of and dereference operator.

Post again of you need more info.
Jan 6 '17 #2
DGrund
22 16bit
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
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

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

Similar topics

3
by: Will | last post by:
I don't know if I can explain what is happening but I will give it a try. I have 2 pages that display a set of thumbnail and they are almost identical. The only difference is the text file they...
1
by: Evan | last post by:
Chandra Shekhar Kumar <chandra.kumar@oracle.com> wrote in message news:<3EF64860.3FCC6E7F@oracle.com>... > donot use dude pointers ...use smart pointer instead There's a problem with this...
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
3
by: jimfortune | last post by:
At what point is a Form added to the Forms collection or a Report added to the Report collection? I.e., listed as currently open. The reason I ask is that I have a subreport for an exclusive...
0
by: danny.agle | last post by:
I've been trying to resolve an issue with a user control not maintaining its state on a web page after a postback. I reviewed http://msdn2.microsoft.com/en-us/library/1whwt1k7.aspx and am...
5
by: Yvad | last post by:
Hi all, I am reading a reference software of H.264 video encoder (http://iphome.hhi.de/suehring/tml/download/). And I found a lot of variable are declared like below: imgpel **imgY =...
1
by: NITISH SHARMA | last post by:
I Am Not Able To Understand !!! What Is Pointer?? Thaat Why We Use It, What Are Its Funcations Also How To Use It?????????
3
by: Phil Latio | last post by:
I am designing a relatively simple CMS with the usual Item, Category and User scenario. My question here relates to how I implement the User registration on this application. Currently I have the...
12
by: Michael Bell | last post by:
I am trying to put my learning effort into the most useful things. What do pointers do that other things can't? Michael Bell --
0
by: Joseph Geretz | last post by:
At what point in the application lifecycle should a filter be applied to the Request / Response objects? Should this be done at the start of each transaction? This would make sense if a new Request...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.