Thread: Problem with this pointer

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    67

    Problem with this pointer

    I have the following code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
        int a[10];
        int *p;
        p = a;
        
        for (int i = 0; i < 10; i++) {
            cout << "Geef een getal: ";
            cin >> *p;
            p++;
        }    
        
        for (int i = 0; i < 10; i++) {
            cout << *p << endl;
            p++;
        }    
        cout << endl << a[0] << endl;
        cin.ignore();
        cin.get();
        return 0;
    }
    When I run it and i enter a number 10 times (say the number 1) this is my output:

    Geef een getal: 1
    Geef een getal: 1
    Geef een getal: 1
    Geef een getal: 1
    Geef een getal: 1
    Geef een getal: 1
    Geef een getal: 1
    Geef een getal: 1
    Geef een getal: 1
    Geef een getal: 1
    2293664 // this list of numbers should all be 1
    4198592
    4370432
    4370436
    2293664
    4198768
    1
    4011880
    4008304
    4198736

    but when I let a[0] cout to the screen it displays the proper value... What;s my problem?

    I know the program is useless but i need to learn pointers for school purposes..

    Thanx in advance!

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You never set p = a again after the first loop and before the second.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    67
    nevermind... i had to reinitialise the pointer to a

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    67
    whaha allright

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM