i wonder what doesld i use & and * means.....and how could i use both of them??

Recommended Answers

All 6 Replies

google "C++ pointers references" and variants thereof

"&" 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);
  printLargest(x,y,z);
  system("pause");
return 0;
} 


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


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


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

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

void printLargest(int a,int b,int c)
{
    int largest;
    largest=a;
    if(b>largest)
    largest=b;
    if(c>largest)
    largest=c;
    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.

cin>>*x>>*y>>*z;

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.

Also , "&" means "the address of", it returns the address of a variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.