2/25/14 ¡ Variable and Address • Variable = Storage in computer Memory memory 0 70 char 1 31 Pointers o Contains some value 2 4 int 3 6 o Must reside at a specific location 4 30 called address 5 1 6 10 o Basic unit – byte 7 4 o Imagine memory as a one- 8 6 Based on slides from K. N. King and Dianna Xu 9 95 dimensional array with addresses as byte indices Bryn Mawr College o A variable consists of one or more 30 201 CS246 Programming Paradigm bytes, depending on its type (size) 31 12 address value Pointer – Reference Address Operations in C • Declaration of pointer variables • A pointer (pointer variable) is a variable that stores an address (like Java reference) o The pointer declarator ‘ * ’ • Use of pointers o value – address of some memory o type – size of that memory o The address of operator ‘ & ’ • Recall in Java, when one declares variables of a o The indirection operator ‘ * ’ – also known as de- referencing a pointer class type, these are automatically references. • In C, pointers have special syntax and much greater flexibility. Pointer Declaration Pointers are NOT integers • Syntax • Although memory addresses are essentially very large integers, pointers and integers are not o destinationType * varName ; interchangeable. • Must be declared with its associated type. • Pointers are not of the same type • Examples • A pointer ’ s type depends on what it points to o int *ptr1; o int *p1; // sizeof(*p1)=sizeof(int) ptr1 o char *p2; //sizeof(*p2)=sizeof(char) A pointer to an int variable • C allows free conversion btw different pointer types o char *ptr2; via casting (dangerous) ptr2 A pointer to a char variable will contain addresses 1 ¡
2/25/14 ¡ Address of Operator Pointer Assignment • Syntax • A pointer p points to x if x ’ s address is stored in p o & expression • Example The expression must have an address. E.g., a o int x = 1; constant such as “ 1 ” does not have an address. int *p; • Example 1 x o int x = 1; p = &x; 1 x address = 567 f(&x); address = 567 The address of x (i.e. where x is stored in p 567 Interpreted as: memory), say, the memory location 567, (not 1) is passed to f . 1 p x Pointer Diagram Pointer Assignment • A pointer p points to x if x ’ s address is stored in p • Example 0012FF88 8 o int x = 1; int *p, *q; 1 x ip i (@0012FF88) p = &x; address = 567 q = p; int i = 8; p 567 Interpreted as: int *ip; q 567 1 p x ip = &i; q Pointer Assignment Indirection Operator • Example Note: ‘ * ’ in a declaration and ‘ * ’ in an • Syntax expression are different. o int x=1, y=2, *p, *q; o * pointerVar int *p; int * p; int* p; o Allows access to value of memory being pointed to p = &x; q = &y; o Also called dereferencing q = p; • Example o int x = 1, *p; 1 2 x y p = &x; address = 567 address = 988 printf("%d\n", *p); *p refers to x ; thus prints 1 p 567 q 988 567 1 p x 2 ¡
2/25/14 ¡ Schematically Assignment Using Indirection Operator • Allows access to a variable indirectly through a pointer pointed to it. • Pointers and integers are not interchangeable x 1 int x = 1; • Example x 1 int *p; p o int x = 1, *p; p = &x; p p = &x; x 2 *p = 2; printf("%d", *p); prints 1 p x 1 printf("%d\n", x); p *p = 2; p x 2 o *p is equivalent to x prints 2 printf("%d", x); Notes • Pointer and integers are not exchangeable • Levels of addressing (i.e. layers of pointers) can be arbitrarily deep • Remember the & that you MUST put in front of scanf variables? • Failing to pass a pointer where one is expected or vise versa always leads to segmentation faults. 3 ¡
Recommend
More recommend