Thread: Question on pointers?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    Question Question on pointers?

    Hello everyone, I am in my first semester on C++ and I am wondering if someone could clarify my understanding of pointers.


    Are pointers used sort of the same way as global variables.
    In other words can I use a pointer just like I use a global variable in my program?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    yes, but they can be used like any other variable, global or not. Just make sure you initialize the pointer to something before you attempt to use it. Many people, including old-timers, make that mistake and wonder why their program crashes.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Thanks Dragon for your help on pointers, It's good to have help in understanding programming.
    Much appreciated!

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Check out my "What's Up With Pointers?" tutorial. Quite a few people have found it very helpful.

    Good luck.
    Last edited by LuckY; 12-23-2005 at 12:57 PM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Hey LuckY:
    int kb = 1024;
    int *ptr = kb;// Compile Error!
    You might get a warning.

    If we're posting tutorials . . . .

    Prelude's Brain Vomit: http://eternallyconfuzzled.com/tuts/pointers.html
    cprogramming.com's tutorial: http://www.cprogramming.com/tutorial.html
    first on the list from google: http://cplus.about.com/od/beginnerct.../aa010502a.htm
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    If I am correct dwks would be

    Code:
    int kb = 1024;
    int *ptr_kb = &kb;// correction
    This would set the memory location of kb to *prt_kb


    by the way excellent tutorial lucky!
    Last edited by robasc; 12-23-2005 at 07:14 PM.

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Here are some ways I am learning how pointers work.

    Code:
    #include <iostream.h>
    
    int main()
    {
    	    int count, temp;               // initialize variables
    
    		int *count_ptr, *temp_ptr;     // initialize pointer variables
    
    	count_ptr = &count;                // assign address count to ptr.
    
    	temp_ptr = &temp;                  // assign temp to ptr.
    	
    /*  I noticed in the output below that the address of variable (count) is the same address as 
        variable pointer (count_ptr). */
    		
        
    	
    	cout << "\n\n the address of count is  " << &count << "\n\n";
    	
    	cout <<"\n the address of count_ptr is  " << count_ptr <<"\n\n";
    	
    	
    	{
    		cout <<"_________________________________________________\n\n";
    
    /* Here I am learning the understanding of pointers and the inderection operators
    the inderection operator is (*). In conclusion, this pairing of the pointer variable and inderection
    operator gives us a way to access directly variables that would otherwise be off limits to us.
    (THIS IS IMPORTANT!!!). */
    
    		int count = 4;                    
    
    		int *count_ptr;
    
    		count_ptr = &count;                        // assign address of count to ptr.
    		 
    		int m;
    
    		m = *count_ptr;                            // assign count pointer to m which will equal to 4.
    
             cout << "hello *count_ptr = " << *count_ptr <<"\n\n";  // displays the value of 4.
    
    		 cout << " m = count_ptr,  see!  " << m <<"\n\n";       //  here m also displays the value of 4.
    	}       
    
    	return 0;
    }

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Good to see you are working hard to learn. A couple of things you will want to consider:

    Code:
    #include <iostream.h>
    You will want to use <iostream> in favour of <iostream.h>, something like this:

    Code:
    #include <iostream>
    You should pay attention to namespace:

    Code:
    cout << "hello *....
    You could do a number of things, like this:

    Code:
    std::cout << "hello *....
    or this:

    Code:
    #include <iostream>
    
    using std::cout;
    
    int main()
    {
       // rest of code
    
       return 0;
    
    }
    or even this: (though this last example is not necessarily the best idea, as it defeats the purpose of hiding functions within a namespace) You can read more about the subject here.

    [code]
    #include <iostream>

    using namespace std;

    int main()
    {

    // rest of code

    return 0;
    }


    The other thing is a clarification on your notes:

    Code:
    int *count_ptr, *temp_ptr;     // initialize pointer variables
    At that line you are only declaring the variables. You initialise them a little later when you assign a value to them here:

    Code:
    count_ptr = &count;                // initialise count_ptr with address of count.
    
    temp_ptr = &temp;                  // initialise temp_ptr with address of temp.
    Of course you have no doubt read that it is very important to make sure you initialise your pointer variables before you try to use them anywhere.
    Last edited by kermit; 12-24-2005 at 07:18 AM.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Thanks for the pointers on clarity I see my mistake there.

    I must remember that you declare variables then initialize them.


    I also noticed that you mentioned <iostream>.
    Code:
    #include <iostream>
    
    int main(argc, char, *argv[])
    {
      std :: cout << "hello";
     
     system ("pause");
      return EXIT SUCESS;
    }
    I use this in bloodshed compiler.

    You must be using dev c++....?

    I am using visual c++ on this code. Is it possible to use <iostream> in visual c++ because I tried it and it did'nt work?
    In which I might add bloodshed is much better. This visual c++ just came with my book Titled "C++ Programming Today".

    Thanks for the pointers!

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Since you're using system(), you'll need to include <cstdlib> as well.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by robasc
    If I am correct dwks would be

    Code:
    int kb = 1024;
    int *ptr_kb = &kb;// correction
    This would set the memory location of kb to *prt_kb


    by the way excellent tutorial lucky!
    Yes, that's the corrected code. But my point was simply that LuckY's code might not produce an error -- on my compiler it generates a warning.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    I found it to work this way.
    this is very interesting!

    May I ask the importance of using <iostream> rather than <iostream.h>
    Thanks!
    Code:
    #include <iostream>
    
    using std::cout;
    
    int main()
    
    {
      std :: cout << "hello";
    
      return 0;
     
    }

  13. #13
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    I got namespace to work now.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
      std :: cout << "hello";
    
      return 0;
     
    }
    I read the discussion above about namespace. I am not sure I understood all of it yet but I am going to read it again.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    When you have using std::cout; or (ugh) using namespace std;, you can drop the std:: from couts. That's the whole point. If you're just going to leave the std:: in then you don't need anything special.

    Code:
    #include <iostream>
    
    int main(void) {
        std::cout << "Hello, World!" << std::endl;
        return 0;
    }
    or
    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    
    int main(void) {
        cout << "Hello, World!" << endl;
        return 0;
    }
    or (as I said, ugh)
    Code:
    #include <iostream>
    using namespace std;
    
    int main(void) {
        cout << "Hello, World!" << endl;
        return 0;
    }
    Last edited by dwks; 12-24-2005 at 04:12 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> May I ask the importance of using <iostream> rather than <iostream.h>

    Because <iostream.h> is old and non-standard. That means that new compilers that follow standard C++ don't have to make it work. In fact, some already don't allow code with <iostream.h> to compile (including Visual C++ 7.1 and 8.0).

    The <iostream> version is the newer version that was part of the standard in 1998. All your new code should use it even if your compiler still works with the older version so that if and when you move to a newer compiler, you don't have to change your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM