Cin pointer array

Pages: 12
I don't understand why I couldn't use cin>>*p++
It's not working. It outputs memory addresses.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include<iostream>
using namespace std;

int main(){
	int n,*p;;
	cout<<"Input the number of integers required: ";
	cin>>n;
	p = new int[n];
	cout<<"Input these "<<n<<" integers:";
	for(int i=0;i<n;i++) cin>>*p++;
	for(int i=1;i<n;i++){
		if(*(p+i)==*(p+i-1)) *(p+i-1)*=2;
		else if(i>=2 && *(p+i)==*(p+i-2)+1) *(p+i-2)*=2;
		else if(i>=2 && *(p+i)> (*(p+i-1)+ *(p+i-2) ))
			*(p+i-1)=*(p+i)- *(p+i-2); 
	}

	cout<<"the output is: ";
	for(int i=0;i<n;i++)cout<<*p++<<' ';

	return 0;
}
Last edited on
What are you doing inside those two inner loops? Some testing for values? I cannot decipher it.
No matter.

When you increment 'p', you're no longer pointing to where you initially started (the location in memory where the beginning of your allocated array is). You must now go backwards to where you started. You undo the movement.

Try this out for size:

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
#include <iostream>

int main()
{
	int *intPointer = nullptr;
	int sizeOfArray = 0;
	std::cout << "Enter the number of integers: ";
	std::cin >> sizeOfArray;

	std::cout << "\nWill be creating: " << sizeOfArray << " integers in array" << std::endl;

	if (!(sizeOfArray > 0))
		exit(-2);

	intPointer = new int[sizeOfArray];

	//Increment pointer AFTER entering the value and assigning it
	for (auto i = 0; i < sizeOfArray; i++)
	{
		std::cout << "Enter value for value " << i << ": ";
		std::cin >> *(intPointer++);
	}

	//We need to go backwards now to get where we started, so I'll print them as I'm traveling backwards
	for (auto i = 0; i < sizeOfArray; i++)
	{
		std::cout << "Value " << (sizeOfArray - i) << ": " << *(--intPointer) << std::endl;
	}
	delete[] intPointer;
}
cin>>*p++ is probably working just fine. But after you got the input, p will be pointing to a position just after the last element, so you've lost track of the start of where the data was stored.
You need to specify which part of the array's index you'd like to take input, otherwise the compiler doesn't know what your trying to take in.
1
2
3
4
5
6
7
8
9
10
int main(){
	int n,*p;;
	cout<<"Input the number of integers required: ";
	cin>>n;
	p = new int[n];
	cout<<"Input these "<<n<<" integers:";
	for(int i=0;i<n;i++)
        cin >> p[i];

}

I in the for loop will specify which index you are trying to access and put input into. And in order to display the contents of the array you would do the same thing
Or just store an unchanged original pointer - you'll need that for the delete [] in any case.
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
#include<iostream>
using namespace std;

int main()
{
    int n; 
    cout << "Input the number of integers required: ";
    cin >> n;
    
    int *array = new int[n];
    int *p = array;
    
    cout << "Input these " << n << " integers: ";
    
    for (int i=0; i<n; i++)
        cin >> *p++;
    
    cout<<"the output is: ";
    p = array;                 // reset p to start
    for (int i=0; i<n; i++)
        cout << *p++ <<' ';
    
    delete [] array;
    
    return 0;
}
Thanks for the above comments.
This is a school quiz question. The program itself is not so important.
I understand that I could use p[i] or *(p+i) instead.
But I just got confused that why couldn't I use *p++.
When I use that, the output is memory address.
Anyone could explain to me?
@rabster
You can, actually, do it in a different way. Array syntax is really syntactic sugar - it's not the only way to iterate through an array. Using the subscript operator moves you through without modifying where the pointer itself is pointing to (intPointer will still be pointing to intPointer[0]).
I generally avoid using the method OP did, as I find it looks more confusing. Array syntax looks more straight-forward, but not the only way to do it.
Last edited on
1
2
But I just got confused that why couldn't I use *p++.
When I use that, the output is memory address. 

No, the output is some region of memory beyond your allocated block.
If you want a memory address, you can retrieve that using '&'.

&intPointer[i] will output the memory address at some index 'i'.

If you access something outside your allocated block, weird values will print. Nothing, generally, bad will happen if you read from that area (meaning the OS won't stop your program) , but your program will get shut down if you try to write out of bounds. Best to do your best to avoid going out of bounds.
@Chervil @JayhawZombie

How can I amend my code? Why would the memory beyond allocated block?
Thanks!
How can I amend my code:
http://www.cplusplus.com/forum/beginner/180777/#msg887304

Why would the memory beyond allocated block:
When you use p++ it changes the pointer. After entering the last value, p is pointing to one place past the end.

I don't know how to explain it more clearly, I think I'm just repeating myself. Sometimes the best way to understand is to get a pencil and paper and draw some diagrams.
Last edited on
@Chervil
Your explanation is very clear. Thank you so much for your kind help!
The teacher is me doesn't know when to stop, sorry.

http://i.imgur.com/skANUgv.jpg

I hope that will help. I pulled those memory values from a sample program run.
The memory addresses will be different every time you run it.
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
51
52
53
#include <iostream>
#include <cassert>

int main()
{
	int *intPointer = nullptr;
	int sizeOfArray = 0;
	int *originalStartingPoint = nullptr;
	std::cout << "Enter the number of integers: ";
	std::cin >> sizeOfArray;

	std::cout << "\nWill be creating: " << sizeOfArray << " integers in array" << std::endl;

	if (!(sizeOfArray > 0))
		exit(-2);

	intPointer = new int[sizeOfArray];
	originalStartingPoint = intPointer;

	//Increment pointer AFTER entering the value and assigning it
	for (auto i = 0; i < sizeOfArray; i++)
	{
		std::cout << "Enter value for value " << i << ": ";
		std::cin >> *(intPointer++);
	}

	//We need to go backwards now to get where we started, so I'll print them as I'm traveling backwards
	for (auto i = 0; i < sizeOfArray; i++)
	{
		std::cout << "Value " << (sizeOfArray - i - 1) << ": " << *(--intPointer) << std::endl;
	}
	
	std::cout << "By iterating manually *(intPointer + i)" << std::endl;
	for (auto i = 0; i < sizeOfArray; i++)
	{
		std::cout << "Value " << i << ": " << *(intPointer +  i) << std::endl;
	}

	std::cout << "By iterating by incrementing (*intPointer++)" << std::endl;
	for (auto i = 0; i < sizeOfArray; i++)
		std::cout << "Value" << i << ": " << *intPointer++ << std::endl;

	//Now restart
	std::cout << "Restarting by assigning to pointer that holds the original address" << std::endl;
	intPointer = originalStartingPoint;
	for (auto i = 0; i < sizeOfArray; i++)
	{
		std::cout << "Value " << i << ": " << intPointer[i] << std::endl;
		std::cout << "\tAddress: " << &intPointer[i] << std::endl;
	}

	delete[] intPointer;
}


Enter the number of integers: 7

Will be creating: 7 integers in array
Enter value for value 0: 1
Enter value for value 1: 2
Enter value for value 2: 3
Enter value for value 3: 4
Enter value for value 4: 5
Enter value for value 5: 6
Enter value for value 6: 7
Value 6: 7
Value 5: 6
Value 4: 5
Value 3: 4
Value 2: 3
Value 1: 2
Value 0: 1
By iterating manually *(intPointer + i)
Value 0: 1
Value 1: 2
Value 2: 3
Value 3: 4
Value 4: 5
Value 5: 6
Value 6: 7
By iterating by incrementing (*intPointer++)
Value0: 1
Value1: 2
Value2: 3
Value3: 4
Value4: 5
Value5: 6
Value6: 7
Restarting by assigning to pointer that holds the original address
Value 0: 1
        Address: 00E9F420
Value 1: 2
        Address: 00E9F424
Value 2: 3
        Address: 00E9F428
Value 3: 4
        Address: 00E9F42C
Value 4: 5
        Address: 00E9F430
Value 5: 6
        Address: 00E9F434
Value 6: 7
        Address: 00E9F438
Press any key to continue . . .
@JayHawkZombie I never knew that lol, I just started learning c++ 4 and a half months ago. I keep learning new things everyday, alright thanks man.
Just a note:

*p++ is different from *(p++)
^correct, which I failed to mention. They both have the same operator precedence, so if there is nothing telling the compiler that you want to incremenet/de-reference first (like parentheses), then it will just so it in a left-to-right order as it sees them (for the operators, some of them go right-to-left).

http://en.cppreference.com/w/cpp/language/operator_precedence
(*p)++ is certainly different to *p++

On the other hand *(p++) and *p++ are effectively the same.
What does *p++ do?

I know *(p++) is the same as *(p+1) == p[1++]

They do the same thing because of the way the ++ is done.

Another thing I failed to mention. ++ will be performed at different times depending on whether it is prefix (before) or postfix (after).

The incrementing will be done after an assignment is done if it is postfix, like what this code shows

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
#include <iostream>


int main()
{
	int *p = new int[5];
	int *start = p;
	for (int i = 0; i < 5; i++)
	{
		*p++ = i; //De-reference p, assign value as 'i', POST increment
	}

	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. p is incremented AFTER the assignment is done
	}
	std::cout << "-----------------------------------------------------" << std::endl;
	p = start;

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

}
Last edited on
Ah thats right I was thinking of *(p)++ and *p++,

*(p)++ increments the value of the index of p, whilst *p++ increments the index right?
Pages: 12