Professional Documents
Culture Documents
Programming in Artificial Intelligence: Ansgar Fehnker
Programming in Artificial Intelligence: Ansgar Fehnker
in Artificial Intelligence
Ansgar Fehnker
Primitive Type vs Data Structures in
Processing
Output Output
Paul's money: 200.0 Paul's balance: 200.0
Mary's money: 100.0 Mary's balance: 200.0
Pointers
Careful:
§ Writing in C or C++ is like running a chain saw with all the
safety guards removed. Bob Gray
§ In C++ it's harder to shoot yourself in the foot, but when
you do, you blow off your whole leg. Bjarne Stroustrup.
Pointers and Memory
Example
std::cout<< &count << std::endl;
§ This means:
§ count is a variable with value 5
§ ptr is a pointer with value
0x241FF54
Dereferencing Pointers
§ Normal variable
§ Contains a data value
int count = 5;
§ Use & to get its address:
cout << count; //prints 5
cout << &count;//prints 0x241FF54
§ Pointers:
§ Contain an address
int *ptr = &count;
§ Use * to get the value at that address
cout << ptr; //prints 0x241FF54
cout << *ptr;// prints 5
Exercise
If we have
char *mychar; // char needs one byte
short *myshort; // short takes 2 bytes
long *mylong; // long takes 4 bytes
§ http://tinyurl.com/createpointers