Cin pointer array

Pages: 12
Well, not quite.

Take a look at this changed version:


I added a few lines to show this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
int main()
{
	int *p = new int[5];
	int *start = p;
	for (int i = 0; i < 5; i++)
	{
		std::cout << "Address of p: " << p << std::endl;
		*p++ = i; 
	}

	p = start;

	for (int i = 0; i < 5; i++)
	{
		std::cout << "p[" << i << "]: " << p[i] << std::endl;
		std::cout << "\tAddress: " << &p[i] << std::endl;
	}

	for (int i = 0; i < 5; i++)
	{
		*(p++) = pow(i, 2); //Accomplishes same thing.
	}
	std::cout << "-----------------------------------------------------" << std::endl;
	p = start;

	for (int i = 0; i < 5; i++)
	{
		std::cout << "p[" << i << "]: " << *p << std::endl;
		std::cout << "\tAddress: " << p << std::endl;
		*(p)++;
	}
	p = start;

	for (int i = 0; i < 5; i++)
	{
		(*p)++; //Increment value p is pointing to
		*p++; //Increment p it move along in memory
	}
	std::cout << "------------------------------------------------------" << std::endl;
	p = start;

	for (int i = 0; i < 5; i++)
	{
		std::cout << "p[" << i << "]: " << *p << std::endl;
		std::cout << "\tAddress: " << p << std::endl;
		*(p)++;
	}
delete[] p;
}


It produces this output:

Address of p: 003D9560
Address of p: 003D9564
Address of p: 003D9568
Address of p: 003D956C
Address of p: 003D9570
p[0]: 0
        Address: 003D9560
p[1]: 1
        Address: 003D9564
p[2]: 2
        Address: 003D9568
p[3]: 3
        Address: 003D956C
p[4]: 4
        Address: 003D9570
-----------------------------------------------------
p[0]: 0
        Address: 003D9560
p[1]: 1
        Address: 003D9564
p[2]: 4
        Address: 003D9568
p[3]: 9
        Address: 003D956C
p[4]: 16
        Address: 003D9570
------------------------------------------------------
p[0]: 1
        Address: 003D9560
p[1]: 2
        Address: 003D9564
p[2]: 5
        Address: 003D9568
p[3]: 10
        Address: 003D956C
p[4]: 17
        Address: 003D9570
Press any key to continue . . .


Both *p++ and *(p++) do the same thing. The ++ both times here is applied to the pointer p not the integer that it is pointing to. Additionally, *(p)++ also does the same thing.

Isn't pointer arithmetic wonderful? XD

Edited to insert missing 'delete' and change confusing comment
Last edited on
I think your example left me more unclear. So another example, for what it's worth.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

int main()
{
    {
        char word[] = "ORANGE";
        char *p = word;
        
        cout << *p++;
        cout << *p++;
        cout << *p++;        
        cout << '\n' << word << "\n-----------\n\n";
    }
    
    {
        char word[] = "ORANGE";
        char *p = word;
        
        cout << *(p++);
        cout << *(p++);
        cout << *(p++);        
        cout << '\n' << word << "\n-----------\n\n";
    }
    
    {
        char word[] = "ORANGE";
        char *p = word;
        
        cout << (*p)++;
        cout << (*p)++;
        cout << (*p)++;        
        cout << '\n' << word << "\n-----------\n\n";
    }
    
}

ORA
ORANGE
-----------

ORA
ORANGE
-----------

OPQ
RRANGE
-----------
Last edited on
Did I make it more confusing? Sorry, I can do that sometimes.

Your example is much clearer than mine.
Actually I think I was just reading too fast. But still, different angles of view can help. :)
I'm not convinced that *p++ is the same as *(p)++ and *(p++)... I remember in my earlier courses my teacher told us the difference but I can't seem to remember
Move that '++' to the other side and you'll see a difference.
The POSTFIX ++ increments the pointer after the operation (assignment, printing, etc). PREFIX will do it beforehand.

1
2
3
int a = 5;
int b = a++;
int c = ++a;


What will the values of b and c be? They certainly will not be the same.
Last edited on
I'm not convinced that...

That's fine. There's no need to take anything on trust. Try out some examples of your own, test them and see what the results are. That's the way to understand this stuff.
I'm not convinced that *p++ is the same as *(p)++ and *(p++)...


Lets consult the precedence table here:
http://en.cppreference.com/w/cpp/language/operator_precedence

*p++

++ is at precedence level 2, * is at precedence level 5 3. So, the post-increment happens first and the result of the post increment is dereferenced.. or, if we parenthesized the expression according to precedence we get:

*(p++)

That looks familiar, doesn't it?


*(p)++
() is at precedence level 1, so it is evaluated first leaving us with:

*p++

which also looks awful familiar.
Last edited on
* is at precedence level 3, isn't it?
* is at precedence level 3, isn't it?

So it is. I glanced at the wrong one. That doesn't change the relative order though.
You also shouldn't assume that instructors are 100% correct all the time. I am a teacher at a university (I teach 23 students this semester - not programming, though), and also a student at the same university.
I had an instructor once tell me that static objects would be created after main if they weren't needed yet. I didn't know that was wrong until someone on here told it was.
I've also told my student something incorrect before due to oversight. Reading too fast, making a small mistake. It happens. I quickly fixed the mistake once I realized that I made it.
Topic archived. No new replies allowed.
Pages: 12