Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zippyfly

macrumors regular
Original poster
Mar 22, 2008
141
0
Hello!

The example on this page

HTML:
http://www.ontko.com/~rayo/cs35/pointers.html

uses pointers. I understand that's to illustrate them, but is there any particular advantage to doing so?

Seems to me that most of these operations can be done using non-pointer variables?

Code:
void hms_to_spm( int hours , int minutes , int seconds , 
                 int *seconds_past_midnight ) ;

void spm_to_hms( int seconds_past_midnight , 
                 int *hours , int *minutes , int *seconds ) ;
main()
{
  int h, m, s, spm ;

  printf("Enter a time (hh:mm:ss)\n" ) ;
  scanf( "%d:%d:%d" , &h, &m, &s) ;

  hms_to_spm( h, m, s, &spm ) ;
  spm += 15 ;
  spm_to_hms( spm , &h, &m, &s ) ;

  printf( "The time, fifteen seconds later, is %02d:%02d:%02d\n" , h , m , s ) ;
}

void hms_to_spm( int hours , int minutes , int seconds , 
                 int *seconds_past_midnight )
{
  *seconds_past_midnight = hours * 3600 + minutes * 60 + seconds ;
}

void spm_to_hms( int seconds_past_midnight , 
                 int *hours , int *minutes , int *seconds )
{
  *hours = ( seconds_past_midnight / 3600 ) % 24 ;
  *minutes = ( seconds_past_midnight / 60 ) % 60 ;
  *seconds = seconds_past_midnight % 60 ;
 
spm_to_hms "returns" a number of values. If things weren't passed using pointers/via reference then you could not do this unless you defined a structure and had this function return an instance of this structure. That's the primary benefit, other than simple academic benefit, that I see from that example.

-Lee
 
The other thing to remember is that, when you're learning a new concept, complex, real-life scenarios aren't the easiest way to go about it.

EDIT: Had I read the FP better, I would have noticed the line "I understand that's to illustrate them..." Oh well.
 
What I would say is that the spm_to_hms function modifies variables "in place", in other words it sends the address of the original variable(s) in order to allow the function to directly operate on them, and they will remain changed in the scope of the calling function when it returns. Passing an int makes a temporary copy of the variable before it gets to the function; you can modify it in the function but the original variable will be unchanged. This is known as pass by value because you are sending the contents of the variable, but which are no longer associated with the original. Passing a pointer to an int (int*) lets you operate on the original variable itself (after dereferencing it), therefore the value of the original will remain modified when the function returns. This is known as pass by reference. In object-oriented programming, all objects are passed by reference. Primitives (non-object types) such as ints, chars, structs, etc. can be passed either way as appropriate.

And as lee mentions, using pointers is the only way to return more than one value from a function as each function can have one and only one return value.
 
In object-oriented programming, all objects are passed by reference. Primitives (non-object types) such as ints, chars, structs, etc. can be passed either way as appropriate.

Not in every language that supports OOP...
Code:
#include <iostream>

void thisGetsAnObjectByValue(std::string);

int main(int argc, char *argv[]) {
  std::string test;
  test = "Test this";
  std::cout << "Before call in main: " << test << std::endl;
  thisGetsAnObjectByValue(test);
  std::cout << "Back in main: " << test << std::endl;
}

void thisGetsAnObjectByValue(std::string valueArg) {
  valueArg += " add this.";
  std::cout << "In function: " << valueArg << std::endl;
}

-Lee
 
spm_to_hms "returns" a number of values. If things weren't passed using pointers/via reference then you could not do this unless you defined a structure and had this function return an instance of this structure. That's the primary benefit, other than simple academic benefit, that I see from that example.

-Lee

Thanks Lee and the others.

So, just so I get this right, the most important point here is that because the function/method returns only a single return value (barring returning a structure or object) that is why we must use pointers to return a series of values, correct?

And in this case, the method/function directly modifies the value stored in the addresses (instead of actually returning them, thus the void return).

Hope I understand this correctly (?)
 
There's a lot of reasons, but being able to return multiple values is the most important one here.

In plain C there are no objects, but if you have large structures this is a good reason to use a pointer, even if you don't need to change the contents. This is because the size of a pointer will be smaller than the size of any structure that has more than a few elements. This means you'll save space on the stack, and save time because you don't have to copy the contents.

Also, when passing arrays you pretty much have to pass a pointer to the base of the array.

There aren't hard and fast rules on when to pass by reference and when to pass by value, but if you need to change a value you have to pass by reference, and if your data size is greater than the size of a pointer, it's probably a good idea.

You can certainly pass information back to the caller using reference arguments AND a return value, but in this case only reference arguments are used, and this is necessary unless you want to define a special struct for every combination of number and type of arguments a function might return. You are also correct that when you modify data passed by reference, you directly manipulate the memory contents the pointers refer to.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.