pointer problem.
Hi,
I am confused about pointer issue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#include <iostream>
#include <limits>
using namespace std;
int main()
{
double *Pdb;
Pdb = new double[2];
Pdb[0] = 1;Pdb[1]=3;Pdb[2]= -8.8174;
int tap_length = 3;
for(int i = 0; i <= (tap_length-1); i++)
{
cout<<Pdb[i]<<endl;
};
std::cout <<" Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return(0);
|
The code does not give any error but when I run the program Windows close the console and program.Why does this happen?
/code]
On line you declare
Pdb = new double[2];
but you probably mean
Pdb = new double[3];
In this meaning the square brackets don't count from zero on...
Thanks mordekai
but then why does not this program work(again no compiler error),only diffrence from previous program is 2 dimensional
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
|
#include <iostream>
#include <limits>
using namespace std;
int main()
{
double **PDB;
PDB = new double*[2]; // if N is rows
for (int i = 0; i < 1; ++i )
{
PDB[i] = new double[2];
};
PDB[0][0] = 2;PDB[0][1]=3;PDB[1][0] = 5;
PDB[1][1] = 7;
cout<<PDB[1][0]<<endl;
|
Last edited on
It's the for loop:
for (int i = 0; i < 1; ++i )
has to be
for (int i = 0; i < 2; ++i )
Thanks mordekai
Topic archived. No new replies allowed.