Thread: Newbie Pointer question - An Arrays & and *

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Newbie Pointer question - An Arrays & and *

    Hi,

    With this code I am trying to cout the contents and memory location of the array's 'containers'.

    Code:
    #include <iostream>
    #include <cstdlib> 
    
    using namespace std;
    
    int main()
    {
    	int arrayz[5];
    	int *pointername;
    	pointername = arrayz;
    
    	for(int t = 0; t<5; t++)
    	{
    		int randomint = rand();
    		arrayz[t] = randomint;
    		cout << "Arrayz location " << t << "is " << &pointername[t] << " and value is " << *pointername << endl;
    	}
    	system("pause");
    }
    During de-bugging, I see random integers being placed into the array as I wanted, but as the screenshot shows below, the cout of the contents are being shown as the same every time.

    This is confusing me, as I don't see why they aren't displayed as different, as I know they indeed are.

    Any help is greatly appreciated.

    Thanks.

    EDIT - I have tried adding [t] to the end of *pointername in the cout line, but it fails to compile, but works with &pointername.
    Last edited by Swerve; 03-06-2008 at 02:26 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your pointername pointer points to the first element of the array - you never update it - so you always print the value of the first element
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    thanks vart!

    I found this, and it does now print the correct memory addresses, but the same technique fails when I add [t] to *pointername.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Code:
    #include <iostream>
    #include <cstdlib> 
    
    using namespace std;
    
    int main()
    {
    	int arrayz[5];
    	int *pointername;
    	pointername = arrayz;
    
    	for(int t = 0; t<5; t++)
    	{
    		int randomint = rand();
    		arrayz[t] = randomint;
    		cout << "Arrayz location " << t << "is " << &pointername[t] << " and value is " << pointername[t] << endl;
    	}
    	system("pause");
    }
    Problem resolved. It doesn't work if you use the * before the arrays name.

    Should anyone see any mistakes i am making in this little program, I would love to hear them.

    Thanks

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What's the point of pointername? You could use arrayz in the same way.

    If you're testing how pointers can be incremented and used on arrays without the [] syntax, then you would want to increment pointername each time through the loop and dereference it to access the element rather than use [] and t.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    OK, I've incremented the pointer, and would welcome any comments on whether I have done this correctly. I am currently unsure on the 'dereferencing' thing mentioned.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cctype>
    #include <cstdio>// I realise not all these libraries are not being used.
    
    using namespace std;
    
    int main()
    {
    	int arrayz[5];
    	int *pointername;
    	pointername = arrayz;
    
    
    	for(int i=0; i<5; i++)
    	{
    		int randomint = rand();
    		*pointername = randomint;
    		pointername++;
    		cout << arrayz[i] << endl;
    	}
    	
    	system("pause");
    }
    Thanks!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    This code looks fine. Plus, you are using the pointer with pointer syntax.

    Whether or not it is "correct" depends on what the point of the exercise is. You aren't outputting the memory location anymore, so you might consider adding that.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks Daved.

    There is no real point to this, I'm just trying to get my head around the subject and the syntax.

    Hearing knowledgeable people like yourself say it looks fine is a great.

    Thanks for all your help!

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I'm just trying to get my head around the subject and the syntax.
    Ok, then how about you try to change your current code so that it outputs:

    "Arrayz location [location] is [address] and value is [value]."

    But don't use the arrayz variable anywhere inside the for loop.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A final note. It's very possible to initialize variables on the line they're defined instead of assigning on the second line:

    Code:
    	int *pointername;
    	pointername = arrayz;
    Is the same as
    Code:
    	int *pointername = arrayz;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie function return question
    By faifas in forum C Programming
    Replies: 2
    Last Post: 06-29-2009, 10:19 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. A pointer question.
    By joenching in forum C++ Programming
    Replies: 7
    Last Post: 03-20-2008, 04:10 PM
  4. Pointer and segfaults question
    By kzar in forum C Programming
    Replies: 5
    Last Post: 09-15-2005, 09:03 AM
  5. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 05:03 PM