I post this simple code in witch I put on 0 and on 10 the value pointed by a integer pointer.
It compiles without errors on dev c++ 4.9.9.2 and also in borland turbo c++ 2006, but it give a whole system error when it's executed. Can anyone give me a reason for this?
include <iostream>
using namespace std;
int main(){
int dim; char end; dim=0;
cout<< "ora deve scrivere zero: " << dim << '\n'; dim=10;
cout<< "ora deve scrivere 10: " << *dim << '\n';
cin>> end;
return 0;
}
thank you...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
dim is an unitialised pointer, it may have any value. When you dereference it you are attempting to access an unknown and usually invalid address. Pointers must point to something valid before you can dereference them.
include <iostream>
using namespace std;
int main()
{
int x ;
int dim = &x ; // dim points to x
char end; dim=0;
cout<< "ora deve scrivere zero: " << dim << '\n'; dim=10;
cout<< "ora deve scrivere 10: " << *dim << '\n';
cin>> end;
return 0;
}
Note that a pointer is a data type so purely from a stylistic point of view:
int* dim ;
is better than
int *dim ;
The * is part of the data type, not the data name. Moreover it makes teh distinction between * as a pointer type and * as a dereference operator much clearer. Some programmers hedge their bets and write:
int * dim ;
but that just illustrates how confused and unsure they are! ;-)
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I post this simple code in witch I put on 0 and on 10 the value pointed by a integer pointer.
It compiles without errors on dev c++ 4.9.9.2 and also in borland turbo c++ 2006, but it give a whole system error when it's executed. Can anyone give me a reason for this?
include <iostream>
using namespace std;
int main(){
int dim; char end;
dim=0;
cout<< "ora deve scrivere zero: " << dim << '\n';
dim=10;
cout<< "ora deve scrivere 10: " << *dim << '\n';
cin>> end;
return 0;
}
thank you...
dim is an unitialised pointer, it may have any value. When you dereference it you are attempting to access an unknown and usually invalid address. Pointers must point to something valid before you can dereference them.
include <iostream>
using namespace std;
int main()
{
int x ;
int dim = &x ; // dim points to x
char end;
dim=0;
cout<< "ora deve scrivere zero: " << dim << '\n';
dim=10;
cout<< "ora deve scrivere 10: " << *dim << '\n';
cin>> end;
return 0;
}
Note that a pointer is a data type so purely from a stylistic point of view:
int* dim ;
is better than
int *dim ;
The * is part of the data type, not the data name. Moreover it makes teh distinction between * as a pointer type and * as a dereference operator much clearer. Some programmers hedge their bets and write:
int * dim ;
but that just illustrates how confused and unsure they are! ;-)
Clifford