Array, pointer relationship.

This is a discussion on Array, pointer relationship. within the C++ Programming forums, part of the General Programming Boards category; I wonder why in the following code example 1 is written like it is and not like I wrote down ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    62

    Array, pointer relationship.

    I wonder why in the following code example 1 is written like it is and not like I wrote down in example 2. What are the differences between them and is there a reason for having the * outside of the ()?

    Code:
    #include <iostream>
    
    const int ARRAY_LENGHT = 5;
    
    int main()
    {
        using namespace std;
    
        // an array of 5 integers initialized to 5 values
        int Numbers [ARRAY_LENGHT] = {0, 100, 200, 300, 400};
    
        // pInt points to the first element
        const int *pInt = Numbers;
    
        cout << "Using a pointer to print the contents of the array: " << endl;
    
        for (int nIndex = 0; nIndex < ARRAY_LENGHT; ++ nIndex)
        {
            cout << "Element [" << nIndex << "] = " << *(pInt + nIndex) << endl;
        }
    
        return 0;
    }
    Example 1:
    Code:
    cout << "Element [" << nIndex << "] = " << *(pInt + nIndex) << endl;
    Example 2:
    Code:
    cout << "Element [" << nIndex << "] = " << (*pInt + nIndex) << endl;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The first moves the pointer pInt along in memory, then dereferences. The second dereferences the pointer to get the value of Numbers[0], then adds the index to that value.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    62
    I am a bit confused now. Could you explain that with an example and what the output would be from that example?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    18
    It's the difference between changing the address in memory that you are looking at, and changing the VALUE of the thing at the pointer address.
    E.g. For your first example, say, your pointer pInt is pointing to the address 100, and nIndex has the value 5, your result will be whatever is at the address 105.

    In the second, You are getting the value of whatever is at the address 100 and adding 5 to that.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    62
    Ok, so in the first example it first does the calculation between the () and then stores that value in the pointer?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,254
    Quote Originally Posted by Kitt3n View Post
    Ok, so in the first example it first does the calculation between the () and then stores that value in the pointer?
    No. It is not storing any value, it is actually reading the value from the nIndex of the array.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Kitt3n View Post
    I am a bit confused now. Could you explain that with an example and what the output would be from that example?
    The first would give you 0, 100, 200, 300, 400 (moving the pointer forward in memory).

    The second would give you 0, 1,2 ,3 ,4 (always using Numbers[0], then adding the index afterwards).

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    * is used to dereference pointers, i.e., get the data they are pointing at.

    *(pInt + nIndex) is the same as saying 'give me the data stored at memory location nIndex bytes from the location of pInt'.

    *pInt + nIndex is only derefencing pInt so it's the same as saying 'give me the data pInt is pointing to and add nIndex to that value'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning a pointer to a struct array
    By osiris^ in forum C Programming
    Replies: 14
    Last Post: 09-13-2009, 11:21 AM
  2. Replies: 0
    Last Post: 05-29-2009, 06:48 AM
  3. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 02:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21