cs lecture nov 1

# cs lecture nov 1 - sum an array with a pointer int*a=int a...

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

cs lecture nov 1 Pointers (ch 11/12) 0[ ] 1[ ] 2[ ] 3[ ] 4[ ] .. Pointer Arithmetic C supports 3 operations: -adding an integer to a pointer -subtracting an integer from a pointer -subtracting one pointer from another (of same type) 0[ ] 1[ ] 2[ ] 3[ ] 4[ ] .. 2 p ->P ->p [][][][][][][][][] q int a[8],*p,*q,*I; p=&a[2]; q=p+3; p+=4 subtracting pointers: p=q-3 i=q-p; /* 3 */ i=p-q/*-3*/ comparing pointers p>q p>=q p<q p<=q, p==q, p!=q

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: sum an array with a pointer int*a=int a int sum (int a, int n) { int *p,total=0; for(p=a;p<a+n;p++) total+=*p; return total; } a p Null Pointer-pointer to nothing, value is 0. int *p=(int*)0; “cast” of zero to be a pointer to an int Struct foo *r = (struct foo*)0; if(p!=(int*)0) { .. } or if(p) { .. } Dynamic Storage...
View Full Document

{[ snackBarMessage ]}

### Page1 / 2

cs lecture nov 1 - sum an array with a pointer int*a=int a...

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

View Full Document
Ask a homework question - tutors are online