CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Jun 2011
    Posts
    21

    Question looking for a good pointer info

    hi
    im new to c++ and trying to find a good place to learn more about them i already read wiki pg about them but i want more help,tired of seaching at google for a good place so im asking my geeks friend for some help where i can learn more about pointers a good website to learn.
    thanks i appreciate, loving this new knowledge

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: looking for a good pointer info

    The easiest way to understand pointers is to think of all the variables you use in your program, not just as words or names, but as little blocks of memory in the program. Think of every variable as an object that has physical size. A char is a small box, an int is a bigger box, a std::string is an elaborate box that you can't see inside of.

    A pointer is simply another variable----another box----except that its value type happens to be a location. You can store the location of a different box in there if you wish. You can even store a location in there which doesn't have a normal variable name, which is one of the real powers of pointers.

    The aforementioned std::string box interests you. It tells you it's currently holding a 50-letter sentence, but as you look at it, there's no way you could fit 50 of the char-sized boxes into there. You notice a bit of yarn trailing out of the std::string box....and as you follow it, you find 50 char-sized boxes all lined up at the end of the yarn. These are clearly what the std::string was referring to, even though they weren't stored inside it and you didn't even know they existed until you followed the yarn. The yarn, in this scenario, is a pointer.

  3. #3
    Join Date
    Apr 2010
    Posts
    20

    Re: looking for a good pointer info

    A pointer is just what it sounds like, a pointer to a place in memory.

    If you declare an integer like below.

    Code:
    int value1 = 0;
    You have an integer variable. If you declare a pointer to an integer as below...

    Code:
    int * pValue1 = 0;
    You have a pointer that is pointing at nothing, 0 = NULL. This is what's called a NULL pointer.

    In order to make the pointer valid, you must make it point at some valid memory. There are a couple of different ways to make this pointer point at valid memory. First of all there is allocating new memory.

    Code:
    int * pValue1 = new int;
    Now you have a pointer pointing at a single integer variable.

    To access the memory at this location, you have to dereference the pointer. That is done as follows.

    Code:
    int * pValue1 = new int;
    
    *pValue1 = 0; // now the integer pointed at by pValue1 is zero.
    
    delete pValue1; // we must cleanup the memory we used by dynamically allocating it.
    As I posted in the above code section was the delete operator. You must free the memory allocated by new with delete.

    Another way to use a pointer is by taking the address of a variable using the the prefix & operator. This is the address of operator. You can use it as follows, and the pointer will point at the same variable as the integer I declare.

    Code:
    int nValue1 = 100;
    int * pValue1 = &nValue1;
    
    *pValue1 = 200;
    
    if (nValue1 == 200)
    {
        std::cout << "pValue1 points at the same place in memory as nValue1. It's value is: " << nValue1 << std::endl;
    }
    
    // Since pValue1 points at an integer value on the stack, there's no need to delete pValue1.
    Pointers can also point at dynamic arrays of memory, and to declare a pointer pointing at an array of memory you can do the following. This introduces the new[] and delete[] operators

    Code:
    int * pArray1 = new int[100]; // we have pArray1 pointing at 100 integers in memory. The memory is contiguous.
    
    // initialize the array of memory with the corresponding index into the array.
    for (int x = 0; x < 100; ++x)
    {
        pArray1[x] = x;
    }
    
    // Now pArray1 is filled with 100 integers in a row in ascending order from 0 to and including 99.
    
    // now delete [] the memory used by pArray1.
    
    delete [] pArray1;
    That is for primitive types. I don't know if you are far enough into learning C++ to learn about objects from classes, but you can have pointers to any type in C++, whether it's an integral integer or a complex polymorphic derived class pointed at by a base pointer.

    If you want me to post on how to use polymorphic objects pointed at by pointers reply to this thread and I will give you an example of using polymorphism.

    EDIT...

    Sometimes you need access to a variable in a function from another function. By using a pointer we can access the variable we want to access from another function.

    Code:
    void Function2(int * pValue)
    {
        *pValue = 100;
    }
    
    void Function1()
    {
        int nValue = 0;
    
        Function2(&nValue); // using the address of operator to create a temporary pointer pushed onto the stack.
    
        if (nValue == 100)
        {
            std::cout << "nValue is now 100 set by Function2." << std::endl;
        }
    }
    You can pass an array pointed at by a pointer across function boundaries. You should specify the length of the array as a second parameter.

    Code:
    void InitializeArray(int * pArray,int nLength)
    {
        for (int x = 0; x < nLength; ++x)
        {
            pArray[x] = 100 + x;
        }
    }
    
    void Function1()
    {
        // allocate an array of memory.
        int * pArray = new array[100];
    
        // initialize the array
        InitializeArray(pArray,100);
    
        for (unsigned int x = 0; x < 100; ++x)
        {
            std::cout << "pArray[" << x << "] = " << pArray[x] << std::endl;
        }
    
        // free the memory pointed at by pArray
        delete [] pArray;
    }
    Last edited by CppCoder2010; July 23rd, 2011 at 10:18 AM. Reason: Forgot ending /code tag.

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: looking for a good pointer info

    Quote Originally Posted by ryamjones View Post
    i want more help,tired of seaching at google
    Why don't you get an introductory C++ textbook. In my view it's easier to learn the basics from a single solid source. This is my personal favourite since I learned C++ from it.

    http://www.amazon.com/Absolute-C-3rd...1404166&sr=8-1

    This is not the latest edition but it should be good enougth (I used the first edition) and there are cheap used copies to be had. I don't think you should buy a new expensive C++ book at this time because C++ is somewhat in flux right now due to a new standard.

    There are other alternatives too like this for example,

    http://www.amazon.com/Programming-Pr...1398985&sr=1-9
    Last edited by nuzzle; July 23rd, 2011 at 02:08 AM.

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: looking for a good pointer info

    A pointer is the same as a shortcut. It isn't the actual data, but it points to it.

  6. #6
    Join Date
    Apr 2009
    Posts
    598

    Re: looking for a good pointer info

    To me, the difficulty is not about the theory, it is about concrete situations, and I guess it is the same for you. My solution is to learn examples, without triying to understand them.

    In C and C++ three kinds of symbols are involved: *, &, and [].
    Watch carefully pieces of source code where they are used.

    The main thing to know is:
    Functions can have arguments passed by value or passed by reference (by pointer).
    The latter case is where you have to use pointers.
    If your variable is already a pointer, then pass it as is.
    Else, prefix it with the ampersand &.
    In the function, prefix that variable with a star *.
    This is the C standard way of doing things.
    In C++ you have another possibility with the ampersand inside the function.

    Then you have lots of other situations that occur mainly in exercices provided by teachers, and not in real life. This is a way of selecting students. Good luck to you!

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: looking for a good pointer info

    Quote Originally Posted by olivthill2 View Post
    The main thing to know is:
    Functions can have arguments passed by value or passed by reference (by pointer).
    That's not true. Pass by reference is not the same as passing a pointer. Pointers can be passed by reference but also by value which they often are.

    C supports pass by value only (as does also Java) whereas C++ supports both parameter passing mechanisms.
    Last edited by nuzzle; July 27th, 2011 at 07:29 AM.

  8. #8
    Join Date
    Nov 2003
    Posts
    1,902

  9. #9
    Join Date
    Apr 2009
    Posts
    598

    Re: looking for a good pointer info

    Quote Originally Posted by nuzzle View Post
    That's not true. Pass by reference is not the same as passing a pointer. Pointers can be passed by reference but also by value which they often are.

    C supports pass by value only (as does also Java) whereas C++ supports both parameter passing mechanisms.
    Interesting. I had in mind the following lines of C code where I thought that i was passed by reference:
    Code:
    int plus_one(int *n)
    {
       *n += 1;
       return(0);
    }
    
    int main(int argc, char *argv[])
    {
       int i;
       i = 2;
       plus_one(&i);
       printf("i=%d", i);
       return(0);
    }

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

    Re: looking for a good pointer info

    Quote Originally Posted by olivthill2 View Post
    Interesting. I had in mind the following lines of C code where I thought that i was passed by reference:
    The "&" used in this context is not the reference operator, it is the address-of operator.

    Since addresses are pointers, then you are passing by value (the value being the address-of i).

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Apr 2010
    Posts
    20

    Re: looking for a good pointer info

    Paul is right. If you use the address of operator you are passing a pointer which is by value to the function.

    You pass by reference doing the following.

    Code:
    void Function1(int & x)
    {
        // notice the & after the type of variable before the name of the variable
       x += 100;
    }
    If you were passing a pointer by reference like the below, the address that the pointer points at could be modified.

    Code:
    void Function2(int *& pX)
    {
        pX += 100; // dangerous, adds 100 integer widths to the pointer variable, not the integer it points at.
    }
    Which is essentially doing the same thing as a pointer to a pointer, just the address cannot change.

    Code:
    void Function3(int ** pX)
    {
        *pX += 100; // we dereference the pointer to a pointer to get a reference to the pointer, this is just the same as the above code.
    }
    Last edited by CppCoder2010; July 27th, 2011 at 11:06 AM.

  12. #12
    Join Date
    May 2009
    Posts
    2,413

    Re: looking for a good pointer info

    Quote Originally Posted by olivthill2 View Post
    I had in mind the following lines of C code where I thought that i was passed by reference:
    No you're passing a pointer by value.

    This is why I recommended the OP to invest in a book. It's very important to get the basics right from the beginning.
    Last edited by nuzzle; July 28th, 2011 at 01:43 AM.

  13. #13
    Join Date
    May 2009
    Posts
    2,413

    Re: looking for a good pointer info

    Quote Originally Posted by Skizmo View Post
    A pointer is the same as a shortcut. It isn't the actual data, but it points to it.
    And what if a pointer points to a pointer?

  14. #14
    Join Date
    May 2009
    Posts
    2,413

    Re: looking for a good pointer info

    Quote Originally Posted by Skizmo View Post
    A pointer is the same as a shortcut. It isn't the actual data, but it points to it.
    Wouldn't that rather be a longcut? Indirect access can hardly be considered shorter that direct access.

    And what if a pointer points to a pointer. Does the longcut get even longer or is the pointer considered actual data in that case contradicting your definition?

  15. #15
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: looking for a good pointer info

    Quote Originally Posted by nuzzle
    Wouldn't that rather be a longcut? Indirect access can hardly be considered shorter that direct access.
    I think Skizmo is talking about shortcut icons and symlinks; it is not the shortcut aspect in itself, but the alias aspect of "pointing" to the file/directory/etc that is the analogy.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured