Pointers And Arrays Cont

Pointers And Arrays Cont - Pointers And Arrays Cont. In C,...

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

View Full Document Right Arrow Icon
In C, pointers are arrays and arrays are pointers. For example, int a[10]; - a is an array of 10 elements of type int. - also, a is a pointer to the first element of the array a[0] is the same as *a. a[1] is the same as *(a+1) a = 100 a[0] a = 104 7 a[1] a = 108 ... ... ... ... ... ... ... a = 136 a[9] a + 1 == 104 *(a+1) == 7 - so we have that a[i], which is the same as *(a+i) is the same as *(&a[0]+i) - ((char*)&a[0] + i*sizeof(int)) // a pointer to char by casting, which isn’t the same as the above Pointer Comparison ( < >= >= == != ) - can compare two pointers. - a pointer is an unsigned int that is the address of the value it is pointing to - for example, add the elements of an array of integers #define MAX 100 int a[MAX]; // fill array a // add array using array operator int sum = 0; for ( i = 0 ; i < MAX ; i++) sum += a[i]; // now using pointers and pointer comparisons int *p; int *pend; p = &a[0];
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

Pointers And Arrays Cont - Pointers And Arrays Cont. In C,...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online