Quote:
Originally Posted by UnixCube
double weight[50]; // variable array weight of type double
double * p_pw; // pointer to weight pw
p_pw = &weight[50]; // pw is assigned the address of the weight array
|
If your comment "pw is assigned the address of the weight array" were correct, you should have:
p_pw = weight;
or equivalent
p_pw = &weight[0];
Your version sets p_pw to point to the address of the 51'st element of a 50 element array (in other words to the next object on the stack after that array).
Quote:
Originally Posted by UnixCube
cout << "input a value for your weight please: " << endl;
cin >> weight[50]; // store input in weight array
* p_pw = weight[50];
|
But the rest of your code makes so little sense, I can't guess what you were trying to do.
Whatever it was should not be done that way.
An array declared with size 50 has elements 0 through 49. It does not have an element 50.
Edit: On review, I think I do have a guess what you may have been trying to do:
Code:
#include <iostream>
using namespace std; // cin and cout
int main()
{
double weight[50]; // variable array weight of type double
double * p_pw; // pointer to weight pw
p_pw = &weight[49]; // pw is assigned the address of the last element of weight array
cout << "input a value for your weight please: " << endl;
cin >> weight[49]; // store input in last element of weight array
cout << " The weight that you inputed is \n" ; //
cout << * p_pw << endl; // should return the input from user
return 0;
} // end main
Notice I did not include your instruction
* p_pw = weight[50];
Even corrected to 49 instead of 50, that would confuse the example. The earlier code makes *p_pw the same object as weight[50]. Changing one of them changes the other. Copying one to the other is redundant.
The important change I made was to use the last element of the array instead of one past the last element.
Since your comments obscured your intention (if I'm now guessing it correctly), I also changed those.