dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Pointers ..... Please Help

  1. #1
    Join Date
    May 2000
    Location
    England
    Posts
    574

    Pointers ..... Please Help

    hi
    i understand the syntax of pointers i just dont seem to get why you would actually use them ?

    can someone please explain why and when i should use them ?

    thanks


  2. #2
    Join Date
    Oct 2001
    Location
    Pune, India
    Posts
    102

    Wink Life is impossible without pointers

    U have to use pointers when u want to refer the address and not the value stored at the address. U must be finding this definition PRETTY VAGUE. However if u pick any book on C, C++ then u can get a indepth view of what pointers are all about.
    In short without pointers a c/c++/vc++ programmer will not be able to survive.

    Sumedh

  3. #3
    Join Date
    Nov 2001
    Posts
    92
    Hi,

    What Sumedh said is right but at the same time I think we need not have to go for pointers all times. It all depends on how you define data structures
    The best book I remember is "Pointers in C" by Kanetkar
    However I am sure that you find many useful posts in codeguru

    Ram

  4. #4
    Join Date
    Feb 2000
    Location
    Rennes, France
    Posts
    624
    One of the reason for the use of pointer is to reduce the use of the memory in a program.
    If you construct a class with a lot of attributes, or a big structure, you might not want to copy it to every fonction that will use it. It could be memory-saving (just inventing the word, sorry), to reuse the same data through its place in memory. So you pass the address of the object, stored in a pointer.
    A pointer being an address, it takes a lot less space than an object to store, and duplicate.

    Marina

  5. #5
    Join Date
    Jun 2002
    Location
    Boston, MA
    Posts
    139
    Another use for pointers is to solve the old "pass by value, pass by reference" chestnut.

    eg;

    Code:
    void byRef(int *p)
    {
      *p +=1;
      cout << *p << endl; // prints 6
    }
    
    void byVal(int q)
    {
      q +=1;
      cout << q<< endl; // prints 7
    }
    
    int main()
    {
      int x = 5;
      int *px = &x;// px is a pointer to x
      byRef(px); //passes a pointer to x to function
      cout << x << endl; //x now has the value 6
      byVal(x);
      cout << x << endl;//x still has the value 6 since a copy of x was 
                                      passed to the function
    
      return 0;
    }

  6. #6
    Join Date
    Feb 2000
    Location
    Rennes, France
    Posts
    624
    Isn't that the same???
    Dupond and Dupont conversation .... (whoever gets that).

    Marina

  7. #7
    Join Date
    Jun 2002
    Location
    Boston, MA
    Posts
    139
    Is what the same?

    If you mean passing by value and passing by reference, they are definitely not the same.

  8. #8
    Join Date
    Nov 2001
    Posts
    92

    Re: pointers

    before making use of pointers, I think one should know about stack memory and heap memory. There is some useful information on this in MSDN.

  9. #9
    Join Date
    Jul 2002
    Location
    St. Louis, MO
    Posts
    484
    When you call a function that has parameters, a copy of the actual parameter is created, and used by the function....

    For instance:

    Code:
    void func(int a);
    
    void main(void)
    {
       int b = 0;
    
       func(b);
    }
    
    void func(int a)
    {
       //a is a copy of the integer b in main
       a += 1;
    }
    We can see how this can be useful, by allowing us to manipulate data, and still keep the value intact...

    Lets now think of how this could be a problem...

    Code:
    //Now this is a big structure...
    struct ABigStruct
    {
       __int64 whoa[65536];
    };
    
    void func(ABigStruct b);
    
    void main(void)
    {
       ABigStruct a;
    
       func(a);
    }
    
    //This function has to make a copy of this huge structure!
    void func(ABigStruct b)
    {
       //do something...
    }
    If we did this:

    Code:
    void func(ABigStruct* b);
    
    void main(void)
    {
       ABigStruct a;
    
       func(&a);
    }
    
    //This function only makes a copy of the pointer to allow it
    //to manipulate the data
    void func(ABigStruct* b)
    {
       //do something...
    }

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Pointers ..... Please Help

    Originally posted by posty68
    hi
    i understand the syntax of pointers i just dont seem to get why you would actually use them ?

    can someone please explain why and when i should use them ?

    thanks

    For C++, the biggest reason to learn to use pointers is polymorphism (also known as "virtual functions").
    Code:
    #include <iostream>
    class A
    {
       public:
          virtual void foo() { std::cout << "A" << std::endl; }
    };
    
    class B : public A
    {
      public:
          void foo() { std::cout << "B" << std::endl; }
    };
    
    void CallFunction(A* SomePointer)
    {
        SomePointer->foo();
    }
    
    int main()
    {
       A SomeA;
       B SomeB;
    
    // Note that both of these are A* 
       A* pB = &SomeB;
       A* pA = &SomeA;
    
    // Call function
       CallFunction(pA);
       CallFunction(pB);
    }
    Both pointers, pB and pA, are pointers to an A object. The CallFunction() function takes only A*, however, the foo() function for B will be called when passed pB, and the foo() function for 'A' will be called when passed pA.

    Therefore the CallFunction() didn't know or care what you passed to it, just as long it is an A*. The magic of "virtual" makes the correct version of foo() get called in CallFunction. In CallFunction(), you didn't need to do "if it's an A, call A::foo(), if it's a B, call B::foo".

    This is polymorphism, and it's the only reason why C++ is an object-oriented language. If you want to learn to do this, you have to use pointers. All other reasons to use pointers are important, but not as important as above.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)