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.
#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 = newint[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 = newint[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
#include<iostream>
usingnamespace std;
int main()
{
int n;
cout << "Input the number of integers required: ";
cin >> n;
int *array = newint[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.
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.
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.
#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 = newint[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 . . .
^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).
#include <iostream>
int main()
{
int *p = newint[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;
}
}