Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004.

Similar presentations


Presentation on theme: "CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004."— Presentation transcript:

1 CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004

2 Pointers Introduction Contain memory addresses as their values Normal variables contain a specific value (direct reference) Pointers contain address of a variable that has a specific value (indirect reference) Indirection – referencing a pointer value count 7 7 count_ptr

3 Pointer Declarations Pointer declaration: int *my_ptr; Declares a pointer to an int (pointer of type int *) Multiple pointers require using a * before each variable declaration int *my_ptr1, *my_ptr2; Can declare pointers to any data type. Multiple pointers can point to same thing.

4 The & and * operators The & (address of) operator returns address of operand, the * (de-referencing) operator gives back the variable pointed to. int y = 5, *y_ptr; y_ptr = &y;/* y_ptr set to address of y */ (*y_ptr)++;/* increment y */ Note that * and & are inverses: printf("y is %d\n", **&&y); /* silly example */ You can initialize pointers to 0 (or NULL) or an address: char c *char_ptr = NULL;

5 Pointers Example The following program has a pointer variable, ptr_to_number, declared: #include void main(){ int number = 3; int *ptr_to_number; ptr_to_number = &number; printf(“Value of number is %d\n”, *ptr_to_number); }

6 Pointers Example (2) Two variables are declared, number and ptr_to_number. number is set equal to 3 and ptr_to_number is not initialised. The assignment statement sets the variable ptr_to_number to “point” to numbe r. This is achieved by means of the address-of operator, &. The output to the screen will be. Value of number is 3 You can also change the value of a variable by accessing it via a pointer. *ptr_to_number = 10; printf(“Value of number is %d\n”, *ptr_to_number); The output is now: Value of number is 10

7 Pointer Types A pointer to one type cannot be assigned to the address of another type, e.g.: int i, *ptr_to_int; char c; &ptr_to_int = c;/* error */ In C it is conventional to assign pointers to NULL, which has the value 0. char *c_ptr = NULL; The dereferencing operator (*) has the same high precedence as the other unary operators such as ++, and right-to-left associativity.

8 Arrays and Pointers Arrays and pointers are closely related, for example pointers can perform array subscripting. Declares an array b[5] and a pointer b_ptr and sets them equal to one another: char b[5], *b_ptr; b_ptr = b; The array name (b) is actually the address of first element of the array, &b[0].

9 Pointer Arithmetic Element b[3] can be accessed with *(b_ptr + 3) where n is the offset. This is called pointer/offset notation. Note that b_ptr[3] is also the same as b[3]. Elements can also be accessed by doing pointer arithmetic on the array itself *(b + 3) There are differences between arrays and pointers, of course. You cannot assign one array to another, e.g. int a[10], b[10]; a = b;/* illegal */

10 Pointer Comparison You can also compare pointers for equality and inequality: two pointers are equal if they point to the same variable or to the same cell in an array, and are unequal if they don't. One common use of pointer comparisons is when copying arrays using pointers. int array1[10], array2[10]; int *ip1, *ip2 = array2; int *end_ptr = &array1[10]; for(ip1 = array1; ip1 != end_ptr; ) *ip2++ = *ip1++;

11 Pass by reference (1) Parameter passing so far has been pass-by-value. We will illustrate this with an example function, add_VAT, which accepts the price of an item as an argument and computes the value-added tax (VAT) for the item, returning the price plus VAT. #define VATRATE 0.2 float price_item; add_VAT(price_item); printf(“Price plus VAT is %f\n”, price_item); void add_VAT(float local_price_item){ local_price_item*=(1 + VATRATE); }

12 Pass by reference (2) The code above will not work properly because local _price_item is a copy of price_item and this value is not copied back to price_item. We need to pass- by-reference and the correct way to achieve this would be to use pointers. add_VAT(&price_item); printf(“Price plus VAT is %d\n”, price_item); void add_VAT(float *ptr_price_item){ *ptr_price_item*=(1 + VATRATE); } Note that the address-of operator ( & ) is required before the argument to add_VAT. The parameter is declared as a pointer to float.


Download ppt "CS1061 C Programming Lecture 13: Pointers A. O’Riordan, 2004."

Similar presentations


Ads by Google