# POINTERS - double g = PI double*d1*d2 d1=&q...

This preview shows pages 1–2. Sign up to view the full content.

POINTERS What is a pointer? -a memory address of some variable -most often stored in variables named “pointer variables” -pointer variables are declared as follows: <type> *<identifier>; int *p1; //pointer to an integer variable double *p2; //pointer to a double variable -store the address of a variable in a pointer int x=42; p1=&x; To use a pointer to access a variable, we must “dereference” it *p1=43 cout<<x<<endl; //43, * - dereferencing operator cout<<*p1<<endl; -when you dereference a pointer, you can use it exactly like a variable of that type -assign to it -evaluate it //int z = 3 * *p1; -pass it to a function Important!!!! You must initialize a pointer before you attempt to dereference it!!! can (but not always) cause a segmentation fault

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: double g = PI; double *d1, *d2; d1= &q; d2=d1; *d2=*d2+1; (*d1)--; can do cout<<p1<<endl; //prints out memory address p1 is currently pointing at You can pass pointers to functions Char response; Cin>>respose; Foo(&response); Void foo(char *c) { if(*c==’q’) cout<<”quit”; else *c=’c’; } foo(response); void foo(char &c) { if(c==’q’) cout<<”quit”; else c=’c’; } double hfrexp(double x, int &exp) { return frexp(x, &exp); } double frexp(double x, int *exponent); typedef-allows you to define your own data types-for example unsigned long int *r; //pointer to an unsigned long int or typedef unsigned long int *ulp; ulp r; //a pointer to an unsigned long integer-another nice feature: ulp r1, r2; //now r1 and r2 are both pointers to unsigned long int...
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

POINTERS - double g = PI double*d1*d2 d1=&q...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online