Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.

Similar presentations


Presentation on theme: "Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer."— Presentation transcript:

1 Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer to a bit field or a reference. A pointer is classified as a scalar type, which means that it can hold only one value at a time. Some common uses for pointers are: To access dynamic data structures such as linked lists, trees, and queues. To access elements of an array or members of a structure or C++ class.

2 To access an array of characters as a string. To pass the address of a variable to a function. (In C++, you can also use a reference to do this.) By referencing a variable through its address, a function can change the contents of that variable.

3 Declaring Pointers The following example declares pcoat as a pointer to an object having type long: long *pcoat; The following example declares pnut as a pointer to an int object : extern int *pnut; The following example defines psoup as a pointer to an object having type float: float * psoup;

4 The following example defines pfowl as a pointer to an enumeration object of type bird: enum bird *pfowl; The next example declares pvish as a pointer to a function that takes no parameters and returns a char object: char (*pvish)(void);

5 Assigning Pointers When you use pointers in an assignment operation, you must ensure that the types of the pointers in the operation are compatible. The following example shows compatible declarations for the assignment operation: float subtotal; float *sub_ptr; /*... */ sub_ptr = &subtotal; printf("The subtotal is %f\n", *sub_ptr);

6 The next example shows incompatible declarations for the assignment operation: double league; int *minor; /*... */ minor = &league; /* error */

7 Initializing Pointers The initializer is an = (equal sign) followed by the expression that represents the address that the pointer is to contain. The following example defines the variables time and speed as having type double and amount as having type pointer to a double. The pointer amount is initialized to point to total: double total, speed, *amount = &total;

8 The compiler converts an unsubscripted array name to a pointer to the first element in the array. You can assign the address of the first element of an array to a pointer by specifying the name of the array. The following two sets of definitions are equivalent. Both define the pointer student and initialize student to the address of the first element in section: int section[80]; int *student = section; is equivalent to: int section[80]; int *student = &section[0];

9 You can assign the address of the first character in a string constant to a pointer by specifying the string constant in the initializer. The following example defines the pointer variable string and the string constant "abcd". The pointer string is initialized to point to the character a in the string "abcd". char *string = "abcd";

10 The following example defines weekdays as an array of pointers to string constants. Each element points to a different string. The pointer weekdays[2], for example, points to the string "Tuesday". static char *weekdays[ ] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; A pointer can also be initialized to null using any integer constant expression that evaluates to 0, for example char * a=0;. Such a pointer is a null pointer. It does not point to any object.

11 Using Pointers Two operators are commonly used in working with pointers, the address (&) operator and the indirection (*) operator. You can use the & operator to refer to the address of an object. For example, the assignment in the following function assigns the address of x to the variable p_to_int. The variable p_to_int has been defined as a pointer: void f(int x, int *p_to_int) { p_to_int = &x; }

12 The * (indirection) operator lets you access the value of the object a pointer refers to. The assignment in the following example assigns to y the value of the object that p_to_float points to: void g(float y, float *p_to_float) { y = *p_to_float; } The assignment in the following example assigns the value of z to the variable that *p_to_char references: void h(char z, char *p_to_char) { *p_to_char = z; }

13 Pointer Arithmetic You can perform a limited number of arithmetic operations on pointers. These operations are: Increment and decrement Addition and subtraction Comparison Assignment The increment (++) operator increases the value of a pointer by the size of the data object the pointer refers to. For example, if the pointer refers to the second element in an array, the ++ makes the pointer refer to the third element in the array.

14 The decrement (--) operator decreases the value of a pointer by the size of the data object the pointer refers to. For example, if the pointer refers to the second element in an array, the -- makes the pointer refer to the first element in the array. You can add an integer to a pointer but you cannot add a pointer to a pointer. If the pointer p points to the first element in an array, the following expression causes the pointer to point to the third element in the same array: p = p + 2;

15 If you have two pointers that point to the same array, you can subtract one pointer from the other. This operation yields the number of elements in the array that separate the two addresses that the pointers refer to. You can compare two pointers with the following operators: = =, !=,, =. Pointer comparisons are defined only when the pointers point to elements of the same array. Pointer comparisons using the = = and != operators can be performed even when the pointers point to elements of different arrays. You can assign to a pointer the address of a data object, the value of another compatible pointer or the NULL pointer.


Download ppt "Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer."

Similar presentations


Ads by Google