7 Years
Discussion Span
Last Post by firstPerson

"&" is the bitwise operator when used in proper context.
"&" is reference operator when used in proper context.
"*" is a multiplication operator when used in proper context.
"*" is a de-referencing operator when used in proper context.
"&*" is a reference to a pointer type.


i wonder why the final answer for this program is 3??
when i calculate it i could only get the answer 2...
when did the value for a is changing???

thank you for helping....


but how does the below program works with the * and &???

#include <iostream>

using namespace std;

/* Prototype Declarations */
void getData (int *x,int *y,int *z);
void printForward (int first,int second,int third);
void printReversed (int first,int second,int third);
void printLargest(int first,int second,int third);
int main ()
/* Local Definitions */
  int x;
  int y;
  int z;
/* Statements */
  getData (&x,&y,&z);
  printForward ( x, y, z);
  printReversed ( x, y, z);
return 0;

/* =================== getData ===================*/
void getData (int *x, int *y, int *z)
cout<<"Enter three integers: ";

/* ================= printForward ==================*/
void printForward (int a, int b, int c)
cout<<"\nNumbers in order:"<<a<<""<< b<<" "<<c<<endl;

/* ================== printReversed ================*/

void printReversed (int a, int b, int c)
cout<<"\nNumbers reversed:"<<c<<""<<b<<" and "<<a<<endl;
/* ================== printLargest ================*/

void printLargest(int a,int b,int c)
    int largest;
    cout<<"The largest number is:"<<largest<<endl;

but how does the below program works with the * and &???

In the code below, the first line, which is a function declaration, the parameters are pointers, which is indicated with the * symbol.
By typing a * between the class and its instance-name, you create a pointer. A pointer simply enough points to a variable. So instead of having an int with it contents, you point to an other int, and if that other int changes, then you can read that through the pointer aswell. Pointers can be changed to point at something else, while references cant.

The second line we create a reference to our variables and send them into the function.
A reference means the actual path to whereever in the memory your integer (or other class) is located.
When defining a pointer, you give it a reference to whatever it must point to.

void getData (int *x,int *y,int *z);
getData (&x,&y,&z);

In this code, we have to put a * in front of the pointers. That is to dereference them. Because a pointer does not exactly equal what it is pointing to. If you printed x, y and z directly you would just get the "location-id" of the referenced item.

So putting the * in front of the pointer, dereferences them, so we get the content they are pointing to instead.


I hope you understand this.

Pointers and references are powerful and a large player in performance. It is very benefitable to learn how to use them, and... use them! :)

If you still are not sure, you are welcome to ask.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.