0

Some important parameters for functions are pointers, address, and arrays.

void somethingHappens (int *a); //pointers
void somethingHappens (int& a); //address
void somethingHappens (int arg[]); //array

If I was to use(initialize) the functions above, how would it look like? \My answers are below. Correct me if I am wrong.

int i = 9; int j[20] = {1,2,3,4,5};
somethingHappens(&i); //pointers
somethingHappens(&i); //address - Not really sure...
somethingHappens(j); //array

Am I missing any other important parameters for functions?

dalawh
  • 886
  • 8
  • 15
  • 37
  • 2
    Did you try compiling, running, and testing each? – chris Oct 18 '12 at 04:49
  • "*//address*" That's not an address; that is a *reference*. – Nicol Bolas Oct 18 '12 at 04:51
  • *"//array"* That's not an array, that is, unfortunately, due to a poor language design decision, a pointer. – Benjamin Lindley Oct 18 '12 at 04:55
  • arrays are internally converted to pointers. Did you read the chapter on "reference" variables and read WHY they were created? Secondly try compiling each and test. Then compile with two cases and lastly compile with all three cases. – Aniket Inge Oct 18 '12 at 04:57
  • Can you guys tell me what is correct and what is not. If possible provide the correct solution. I want to make sure that I know all the possible parameters that are not "normal". @PrototypeStark What chapter? Can you link me? – dalawh Oct 18 '12 at 05:01
  • 1
    @BenjaminLindley: from 8.3.5.5 'After determining the type of each parameter, any parameter of type “array of T” or “function returning T” is adjusted to be “pointer to T” or “pointer to function returning T,” respectively.' - so clearly it _is_ valid to consider it a parameter of type "array of T", from a pre-adjustment perspective. – Tony Delroy Oct 18 '12 at 05:05
  • @TonyD: Nevermind, I misunderstood what you were arguing. – Benjamin Lindley Oct 18 '12 at 05:09
  • 3
    @dalawh Given that this question is fairly basic, you may benefit from reading an introduction to C++, perhaps one of the books on this list: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – jogojapan Oct 18 '12 at 05:14
  • @dalawh: Correct solution *to what*? You haven't stated a problem, so there's nothing to solve. – Nicol Bolas Oct 18 '12 at 05:25

1 Answers1

3

In the first case (as you have rightly mentioned), the argument is passed through the pointer i.e. the address of the argument is passed to the method. Thereby, the method has direct access to the memory location pointed by the pointer (remember that pointer is also a variable, but it holds a memory address instead).

In the second case, the argument is passed as an alias. It is very similar to the first method, the only different is that you are not using the memory address of the argument to access its value.

In the third case (interestingly), when you pass the array variable, it is passed by reference (in case of an array arr[], arr represents the base address of the array i.e. address of zeroth element or &arr[0]). So manipulating the array in the method will reflect in the caller as well.

In the second case,

void somethingHappens (int& a);

The call to this should be of the form,

somethingHappens(i); 
// somethingHappens(&i); is wrong because &i means the location of i, 
// while the method is expecting an alias and not an address


More information

Community
  • 1
  • 1
Devendra D. Chavan
  • 8,871
  • 4
  • 31
  • 35
  • Thanks for all the help. By any chance, are there any other parameters of a function that I should know that aren't simple? – dalawh Oct 18 '12 at 05:18
  • @dalawh: you've struggled to understand the simplest parameters... I'd wait a while before worrying about how complex they can get, but when you're ready - takes most programmers a few years of full time C++ - check out template expressions.... – Tony Delroy Oct 18 '12 at 07:19