Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Different Between the operator '*' and '&'

Different Between the operator '*' and '&'

Scheduled Pinned Locked Moved C / C++ / MFC
15 Posts 7 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jeansea
    wrote on last edited by
    #1

    the two Different Code create the same result i want to know the different between the operator '*' and '&' the code is below: first: #include<iostream> using namespace std; int Add(int &P1,int &P2) { P1=5; P2=10; return P1+P2; } int main() { int P1=2,P2=3; cout<<"As Parameter Before The P1 is:"<<P1<<" P2 is:"<<P2<<endl; int Sum=Add(P1,P2); cout<<"P1 Parameter After The P1 is: "<<P1<<" P2 is:"<<P2<<endl; } Second: #include<iostream> using namespace std; int Add(int *P1,int *P2) { *P1=5; *P2=10; return *P1+*P2; } int main() { int *P1,*P2; int PA=2,PB=3; P1=&PA; P2=&PB; cout<<"As Parameter Before The P1 is:"<<*P1<<" P2 is:"<<*P2<<endl; int Sum=Add(P1,P2); cout<<"P1 Parameter After The P1 is: "<<*P1<<" P2 is:"<<*P2<<endl; } end the variable P1 and P2 's value are all change . could you tell me the difference between them thank you :laugh:

    C P J K M 5 Replies Last reply
    0
    • J jeansea

      the two Different Code create the same result i want to know the different between the operator '*' and '&' the code is below: first: #include<iostream> using namespace std; int Add(int &P1,int &P2) { P1=5; P2=10; return P1+P2; } int main() { int P1=2,P2=3; cout<<"As Parameter Before The P1 is:"<<P1<<" P2 is:"<<P2<<endl; int Sum=Add(P1,P2); cout<<"P1 Parameter After The P1 is: "<<P1<<" P2 is:"<<P2<<endl; } Second: #include<iostream> using namespace std; int Add(int *P1,int *P2) { *P1=5; *P2=10; return *P1+*P2; } int main() { int *P1,*P2; int PA=2,PB=3; P1=&PA; P2=&PB; cout<<"As Parameter Before The P1 is:"<<*P1<<" P2 is:"<<*P2<<endl; int Sum=Add(P1,P2); cout<<"P1 Parameter After The P1 is: "<<*P1<<" P2 is:"<<*P2<<endl; } end the variable P1 and P2 's value are all change . could you tell me the difference between them thank you :laugh:

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      The difference is in how you will access the variable in your function: if you pass the variables by reference (using &), you can access the variables directly. If you pass the pointer to the variables (using *), then in your function you will receive the pointer.

      Cédric Moonen Software developer
      Charting control [v1.5] OpenGL game tutorial in C++

      _ 1 Reply Last reply
      0
      • J jeansea

        the two Different Code create the same result i want to know the different between the operator '*' and '&' the code is below: first: #include<iostream> using namespace std; int Add(int &P1,int &P2) { P1=5; P2=10; return P1+P2; } int main() { int P1=2,P2=3; cout<<"As Parameter Before The P1 is:"<<P1<<" P2 is:"<<P2<<endl; int Sum=Add(P1,P2); cout<<"P1 Parameter After The P1 is: "<<P1<<" P2 is:"<<P2<<endl; } Second: #include<iostream> using namespace std; int Add(int *P1,int *P2) { *P1=5; *P2=10; return *P1+*P2; } int main() { int *P1,*P2; int PA=2,PB=3; P1=&PA; P2=&PB; cout<<"As Parameter Before The P1 is:"<<*P1<<" P2 is:"<<*P2<<endl; int Sum=Add(P1,P2); cout<<"P1 Parameter After The P1 is: "<<*P1<<" P2 is:"<<*P2<<endl; } end the variable P1 and P2 's value are all change . could you tell me the difference between them thank you :laugh:

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        * is backward compatible with C. :-D Edit: I was off by one character, I meant to indicate that C doesn't support references.

        modified on Thursday, March 5, 2009 9:56 AM

        C 1 Reply Last reply
        0
        • P PIEBALDconsult

          * is backward compatible with C. :-D Edit: I was off by one character, I meant to indicate that C doesn't support references.

          modified on Thursday, March 5, 2009 9:56 AM

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          You can use pointers in C too (thus using the '*' is valid in C).

          Cédric Moonen Software developer
          Charting control [v1.5] OpenGL game tutorial in C++

          P 1 Reply Last reply
          0
          • C Cedric Moonen

            You can use pointers in C too (thus using the '*' is valid in C).

            Cédric Moonen Software developer
            Charting control [v1.5] OpenGL game tutorial in C++

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Ah, I'm not awake yet, coffee just finished dripping. I must have meant it the other way around. I only dabbled in C++ a little, and not recently.

            B 1 Reply Last reply
            0
            • J jeansea

              the two Different Code create the same result i want to know the different between the operator '*' and '&' the code is below: first: #include<iostream> using namespace std; int Add(int &P1,int &P2) { P1=5; P2=10; return P1+P2; } int main() { int P1=2,P2=3; cout<<"As Parameter Before The P1 is:"<<P1<<" P2 is:"<<P2<<endl; int Sum=Add(P1,P2); cout<<"P1 Parameter After The P1 is: "<<P1<<" P2 is:"<<P2<<endl; } Second: #include<iostream> using namespace std; int Add(int *P1,int *P2) { *P1=5; *P2=10; return *P1+*P2; } int main() { int *P1,*P2; int PA=2,PB=3; P1=&PA; P2=&PB; cout<<"As Parameter Before The P1 is:"<<*P1<<" P2 is:"<<*P2<<endl; int Sum=Add(P1,P2); cout<<"P1 Parameter After The P1 is: "<<*P1<<" P2 is:"<<*P2<<endl; } end the variable P1 and P2 's value are all change . could you tell me the difference between them thank you :laugh:

              J Offline
              J Offline
              jeansea
              wrote on last edited by
              #6

              Is all the position the '*' use can replace by '&'? :doh:

              1 Reply Last reply
              0
              • J jeansea

                the two Different Code create the same result i want to know the different between the operator '*' and '&' the code is below: first: #include<iostream> using namespace std; int Add(int &P1,int &P2) { P1=5; P2=10; return P1+P2; } int main() { int P1=2,P2=3; cout<<"As Parameter Before The P1 is:"<<P1<<" P2 is:"<<P2<<endl; int Sum=Add(P1,P2); cout<<"P1 Parameter After The P1 is: "<<P1<<" P2 is:"<<P2<<endl; } Second: #include<iostream> using namespace std; int Add(int *P1,int *P2) { *P1=5; *P2=10; return *P1+*P2; } int main() { int *P1,*P2; int PA=2,PB=3; P1=&PA; P2=&PB; cout<<"As Parameter Before The P1 is:"<<*P1<<" P2 is:"<<*P2<<endl; int Sum=Add(P1,P2); cout<<"P1 Parameter After The P1 is: "<<*P1<<" P2 is:"<<*P2<<endl; } end the variable P1 and P2 's value are all change . could you tell me the difference between them thank you :laugh:

                K Offline
                K Offline
                ky_rerun
                wrote on last edited by
                #7

                in a declaration ie int *X means a a pointer to x named *; You cannot define a reference with initializing it so int &X //doesn't work however --- int Y; int &x = Y; does Y and X now refer to the same place in memory changing the value of one will change the value of the other. When you use a & in a function prototype we are saying I want my parameter to access memory at the location of the variable the caller specified; When you declare a pointer in the prototype you are going to explicitly pass in a pointer that pointer can however be reassigned but once reassinged the caller will no longer be able to read changes to what was passed in I wrote a little example. // CppTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" void NoSideEffect(int *T) { int G = 5; T = &G; } void SideEffect(int &T) { int G = 5; T = G; } void PointerSideEffect(int *T) { int G = 10; *T = G; } int _tmain(int argc, _TCHAR* argv[]) { char buffer[2]; int X = 2; NoSideEffect(&X); printf("X is Now %d \n",X); SideEffect(X); printf("X is Now %d \n",X); PointerSideEffect(&X); printf("X is Now %d \n",X); gets(buffer); } this will print out X is Now 2 X is Now 5 X is Now 10


                a programmer traped in a thugs body

                J 1 Reply Last reply
                0
                • J jeansea

                  the two Different Code create the same result i want to know the different between the operator '*' and '&' the code is below: first: #include<iostream> using namespace std; int Add(int &P1,int &P2) { P1=5; P2=10; return P1+P2; } int main() { int P1=2,P2=3; cout<<"As Parameter Before The P1 is:"<<P1<<" P2 is:"<<P2<<endl; int Sum=Add(P1,P2); cout<<"P1 Parameter After The P1 is: "<<P1<<" P2 is:"<<P2<<endl; } Second: #include<iostream> using namespace std; int Add(int *P1,int *P2) { *P1=5; *P2=10; return *P1+*P2; } int main() { int *P1,*P2; int PA=2,PB=3; P1=&PA; P2=&PB; cout<<"As Parameter Before The P1 is:"<<*P1<<" P2 is:"<<*P2<<endl; int Sum=Add(P1,P2); cout<<"P1 Parameter After The P1 is: "<<*P1<<" P2 is:"<<*P2<<endl; } end the variable P1 and P2 's value are all change . could you tell me the difference between them thank you :laugh:

                  M Offline
                  M Offline
                  Maximilien
                  wrote on last edited by
                  #8

                  using references is safer, you do not have to check for NULL pointers.

                  This signature was proudly tested on animals.

                  P 1 Reply Last reply
                  0
                  • K ky_rerun

                    in a declaration ie int *X means a a pointer to x named *; You cannot define a reference with initializing it so int &X //doesn't work however --- int Y; int &x = Y; does Y and X now refer to the same place in memory changing the value of one will change the value of the other. When you use a & in a function prototype we are saying I want my parameter to access memory at the location of the variable the caller specified; When you declare a pointer in the prototype you are going to explicitly pass in a pointer that pointer can however be reassigned but once reassinged the caller will no longer be able to read changes to what was passed in I wrote a little example. // CppTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" void NoSideEffect(int *T) { int G = 5; T = &G; } void SideEffect(int &T) { int G = 5; T = G; } void PointerSideEffect(int *T) { int G = 10; *T = G; } int _tmain(int argc, _TCHAR* argv[]) { char buffer[2]; int X = 2; NoSideEffect(&X); printf("X is Now %d \n",X); SideEffect(X); printf("X is Now %d \n",X); PointerSideEffect(&X); printf("X is Now %d \n",X); gets(buffer); } this will print out X is Now 2 X is Now 5 X is Now 10


                    a programmer traped in a thugs body

                    J Offline
                    J Offline
                    jeansea
                    wrote on last edited by
                    #9

                    void NoSideEffect(int *T) { int G = 5; T = &amp;G; } int X = 2; NoSideEffect(&X); printf("X is Now %d \n",X); X is Now 2 don't change why?could you explain more detail ;P

                    K 1 Reply Last reply
                    0
                    • J jeansea

                      void NoSideEffect(int *T) { int G = 5; T = &amp;G; } int X = 2; NoSideEffect(&X); printf("X is Now %d \n",X); X is Now 2 don't change why?could you explain more detail ;P

                      K Offline
                      K Offline
                      ky_rerun
                      wrote on last edited by
                      #10

                      This is because c++ accurately always passes by value When you pass a pointer you are passing the value of the pointer which is a memory address. when the compiler creates a stack frame the value of the memory address is put on the stack as a local variable when you change the value of that variable you point it to a new memory location and no longer maps to memory address of the variable in the caller. So any change will not affect the caller since you are changing the location associated with another variable in this case another local variable which is allso on the stack.


                      a programmer traped in a thugs body

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Ah, I'm not awake yet, coffee just finished dripping. I must have meant it the other way around. I only dabbled in C++ a little, and not recently.

                        B Offline
                        B Offline
                        bulg
                        wrote on last edited by
                        #11

                        I can't believe there're people who know C and not C++ still! :)

                        P 1 Reply Last reply
                        0
                        • M Maximilien

                          using references is safer, you do not have to check for NULL pointers.

                          This signature was proudly tested on animals.

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #12

                          No?

                          _ 1 Reply Last reply
                          0
                          • B bulg

                            I can't believe there're people who know C and not C++ still! :)

                            P Offline
                            P Offline
                            PIEBALDconsult
                            wrote on last edited by
                            #13

                            C++ is all hype; it's not necessary for most real work. :-D

                            1 Reply Last reply
                            0
                            • P PIEBALDconsult

                              No?

                              _ Offline
                              _ Offline
                              _Superman_
                              wrote on last edited by
                              #14

                              That's right. You cannot pass a null to a reference like you can for a pointer.

                              «_Superman_» I love work. It gives me something to do between weekends.

                              1 Reply Last reply
                              0
                              • C Cedric Moonen

                                The difference is in how you will access the variable in your function: if you pass the variables by reference (using &), you can access the variables directly. If you pass the pointer to the variables (using *), then in your function you will receive the pointer.

                                Cédric Moonen Software developer
                                Charting control [v1.5] OpenGL game tutorial in C++

                                _ Offline
                                _ Offline
                                _Superman_
                                wrote on last edited by
                                #15

                                It is more neat sometimes to use &. Take this simple example of a multiply function. Using *

                                double multiply(double *x, double *y)
                                {
                                return *x * *y;
                                }

                                double d = multiply(&a, &b);

                                Using &

                                double multiply(double &x, double &y)
                                {
                                return x * y;
                                }

                                double d = multiply(a, b);

                                «_Superman_» I love work. It gives me something to do between weekends.

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups