Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers Applications

Similar presentations


Presentation on theme: "Pointers Applications"— Presentation transcript:

1 Pointers Applications
The void Pointer - With one exception, it is invalid to assign a pointer of one type to a pointer of another type. This is true even though the values in both cases are memory addresses and would therefore seem to be fully compatible. Although the addresses may be compatible because they are drawn from the same set, what is not compatible in the underlying data type of the referenced object. C doesn’t let us use the assignment operator with pointers of different types; is we try to , we get a compile error. The exception to the rule is the void pointer. The void pointer can be used with any pointer and any pointer can be assigned to a void pointer. A void pointer cannot be dereferenced. Example: void *pVoid ; Arrays and pointers - There is a very close relationship between arrays and pointers. The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot be changed. Since the array name is a pointer constant to the first element, the address of the first element and name of the array both represent the same location in memory. We can, therefore, use the array name anywhere we can use a pointer, as long as you do not try to change it’s value. If we have the following declaration: int x[ 5 ] ; the following two expressions result in the same value x &x[0] Also dereferencing the array name gives the same value as a reference to the first element of the array: *x x[0] same same

2 Pointers Applications
Arrays and pointers (continued) - Since the name of the array is a pointer value we can assign this value to other pointer variables of the same type and then use the pointer the same way we use the array name. int main(void) { int a[ 5 ] = {2, 4, 6, 8, 22}; int *p = a; int i = 0; printf(“%d %d\n”, a[i], *p) return 0; } Main function memory space Addresses a[0] 2 48682 a[1] 4 48686 a[2] 6 48690 a[3] 8 48694 a[4] 22 48698 Even though p is a pointer variable, we can use it as an array name int main(void) { int a[ 5 ] = {2, 4, 6, 8, 22}; int *p; p = &a[1]; printf(“%d %d\n”, a[0], p[-1]); printf(“%d %d\n”, a[1], p[0]); return 0; }

3 Pointers Applications
Pointer arithmetic and arrays - Besides indexing, there is another powerful method of moving through an array: Pointer arithmetic. Pointer arithmetic offers a restricted set of arithmetic operators for manipulating the addresses in pointers. It is especially powerful when we need to move through an array from element to element, such as when we are searching an array sequentially. If we have an array, a, then a is a constant pointing to the first element and a + 1 is a constant to the second element. If we have a pointer, p, pointing to the second element of an array , then p - 1 is a pointer to the previous (first)element and p + 1 is a pointer to the next (third) element. Furthermore, given a, a + 2 is the address two elements from a and a + 3 is the address three elements from a. Given pointer, p, p  n is a pointer to the value n elements away. memory Addresses a a[0] 2 48682 p - 1 a + 1 a[1] 4 48686 p a + 2 a[2] 6 48690 p + 1 a + 3 a[3] 8 48694 p + 2 a + 4 a[4] 22 48698 p + 3 The meaning of adding or subtracting here is different from normal arithmetic. When you add an integer n to a pointer value, you will get a value that corresponds to another index location, n elements away. In other words, n is an offset from the original pointer. To determine the new value, C must know the size of one element. The size of the element is determined by the type of the pointer. This is one of the prime reasons why pointers of different types cannot be assigned to each other.

4 Pointers Applications
Pointer arithmetic allows us to assign an address of an array element to a pointer using the name of the array and the offset. p = a + 1; Second, we can use it with the indirection operator to access or change the value of the element we are pointing to. The two following expressions are exactly the same when a is the name of an array and n is an integer. *(a + n) is identical to a[n] memory Addresses *(a + 0) or a[0] 2 48682 *(a + 1) or a[1] 4 48686 *(a + 2) or a[2] 6 48690 *(a + 3) or a[3] 8 48694 *(a + 4) or a[4] 22 48698

5 Pointers Applications
Pointers and other Operators - Arithmetic operations involving pointers are very limited: Addition can be used when one operand is a pointer and the other is an integer. Subtraction can be used only when both operands are pointers or when the first operand is a pointer and the second operand is an integer. You can manipulate a pointer with the postfix and prefix unary increment and decrement operators. All the following pointer arithmetic operations are valid : p p p - 5 p1 - p2 p++ --p When a pointer is subtracted from another the result is an index representing the number of elements between the two pointers. The relational operators are allowed only if both operands are pointers of the same type. p1 >= p2 p1 != p2 Pointers and Two-Dimensional Arrays - Just like a single dimensional array the name of a two dimensional array is a pointer constant ( address ) to the first element of the array. In this case, however, the first element is another array. When we dereference the array name, we don’t get the value of the first element, we get the address of the first array (first row) in our two dimensional array. In other words, the dereference of the array name of a two-dimensional array is a pointer to a one-dimensional array. Therefore if we have a two dimensional array: int table [3] [4] the expression table[0] will refer to the first array in our two dimensional array

6 Pointers Applications
Memory Allocation Functions - Modern languages such as C have a feature known as dynamic memory allocation. Dynamic memory allocation uses predefined functions to allocate and release memory for data while the program is running. This effectively postpones the data definition to run time. To use dynamic memory allocation, the programmer must use either standard data types or already must have declared any derived types. Conceptually, we can say that memory is divided into program memory and data memory. Program memory consists of the memory used for main and all called functions. Data memory consists of definitions, such as global data and constants, local definitions, and dynamic data memory. It is also possible for more than one version of the function to be active at a time. In this case, multiple copies of the local variables will be allocated, although only one copy of the function is present. The memory facility for these capabilities is known as the stack. main called and standard functions PROGRAM MEMORY global program heap system stack DATA MEMORY MEMORY

7 Pointers Applications
Memory Allocation Functions (continued) - The heap is unused memory allocated to the program and available to be assigned during its execution. It is the memory pool from which memory is allocated when requested by the memory allocation functions. Four memory management functions are used with dynamic memory. Three of them, malloc, calloc, and realloc, are used for memory allocation. The fourth, free is used to return memory when it is no longer needed. malloc - allocates a block of memory that contains the number of bytes specified in its parameter. It returns a void pointer to the first byte of the allocated memory. The allocated memory is not initialized. The prototype for malloc is as follows: void *malloc( size_t size) ; The size_t type is defined in several header files including <stdio.h>. To provide portability, the size specification in malloc’s actual parameter is generally computed using the sizeof operator. pInt = ( int *) malloc ( sizeof (int) ); If malloc is not successful, it returns a NULL pointer. if( ! (ptr = (int *) malloc ( sizeof (int ) ) ) ) exit(100); calloc - this function is used to allocate memory for arrays. It allocates a contiguous block of memory large enough to contain an array of elements of a specified size. It returns a pointer to the first element of the allocated array. calloc also sets the memory allocated to zero. void * calloc ( size_t element-count, size_t element_size);

8 Pointers Applications
Memory Allocation Functions (continued) - free - When memory locations allocated by malloc, calloc, or realloc are no longer needed, they should be freed using the predefined function free. void free ( void *ptr ); You should free memory whenever it is no longer needed. It is not necessary, however, to clear memory at the end of the program. The operating system will release all memory when your program terminates. It is a logic error to use the pointer after memory has been released.


Download ppt "Pointers Applications"

Similar presentations


Ads by Google