Presentation is loading. Please wait.

Presentation is loading. Please wait.

ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.

Similar presentations


Presentation on theme: "ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions."— Presentation transcript:

1 ADVANCED POINTERS

2 Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions returning pointers Pointers to functions

3 Review: Pointers and Arrays flizny == &flizny[0]; // name of array is the address of the first element Both flizny and &flizny[0] represent the memory address of that first element. Both are constants because they remain fixed for the duration of the program. They can be assigned as values to a pointer variable, and the variable can be changed.

4 Pointers and Arrays short double pointers + 0: 0x0064fd20 0x0064fd28 pointers + 1: 0x0064fd22 0x0064fd30 pointers + 2: 0x0064fd24 0x0064fd38 pointers + 3: 0x0064fd26 0x0064fd40

5 Pointers and Arrays The value of a pointer is the address of the object to which it points. How the address is represented internally is hardware dependent. The address of a large object, such as type double variable, typically is the address of the first byte of the object. Applying the * operator to a pointer yields the value stored in the pointed-to object. Adding 1 to the pointer increases its value by the size, in bytes, of the pointed-to type. dates +2 == &date[2] /* same address */ *(dates + 2) == dates[2] /* same value */

6 Troubles with pointers List of some pointer errors Assigning values to uninitialized pointers int *p, m = 100; *p = m;// Error Assigning values to a pointer variable int *p, m = 100; p = m;// Error Not dereferencing a pointer when required int *p, x = 100; *p = &x; printf(“%d”, p);// Error Assigning the address of an uninitialized variable int m, *p; p = &m; Comparing pointers that point to different objects char name1 [20], name2 [30]; char *p1 = name1; char *p2 = name; If (p1 > p2)… //Error

7 Multidimensional Arrays Two dimensional array definition: float rain[5][12]; // array of 5 arrays of 12 floats This tells us that each element is of type float[12]; that is, each of the five elements of rain is, in itself, an array of 12 float values. rain  Pointer to first row rain + 1  pointer to ith row *(p+i)  pointer to first element in the ith row *(p+i) + j  pointer to jth element in the ith row *(*(p+i) + j )  value stored in the cell (i,j) *(rain + 2) + 3

8 Initializing a Two-Dimensional Array More Dimensions int box[10][20][30]; You can visualize a one-dimensional array as a row of data, a two- dimensional array as a table of data, and a three-dimensional array as a stack of data tables. Typically, you would use three nested loops to process a three- dimensional array, four nested loops to process a four-dimensional array, and so on. char *name[3] = {“New Zealand”, “Australia”, “India”};

9 The program goal is to find a)the total rainfall for each year, b)the average yearly rainfall, and c)the average rainfall for each month. To find the total rainfall for a year, you have to add all the data in a given row. To find the average rainfall for a given month, you have to add all the data in a given column. The two-dimensional array makes it easy to visualize and execute these activities.

10 Pointers as functions arguments The function parameters are declared as pointers; The dereferenced pointers are used in the function body; When the function is called, the addresses are passed as actual arguments. array passed as a pointer Example 1: Sorting function Example 2: copying strings

11 Functions returning pointers The function larger receives the addresses of the variables a and b, decides which one is larger using the pointers x and y and the returns the address of its location. The returned value is then assigned to the pointer.

12 Pointers to functions A pointer to a function is declared as follows: type (*fptr) (); This tells the compiler that fptr is a pointer to a function, which returns type value. Assigning the name of the function to the pointer double mul(int, int); double (*p1)(); p1 = mul; Now we can call the function by the pointer p1 with the list of parameters. (*p1)(x,y)  mul(x,y)

13 Example: A program that uses a function pointer as a function argument A program prints the function values over a given range of values. The printing is done by the function table by evaluating the function passed to it by the main.


Download ppt "ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions."

Similar presentations


Ads by Google