1 / 28

Pointers

Pointers. Artem A. Lenskiy, PhD June 2, 2014. Content. Address of operator (&) Pointers Pointers and array. The & Operator: Familiar example. A familiar program. Could it be this way?. ?. Why not?. The & Operator: Familiar example. A familiar program. Could it be this way?. ?.

matty
Download Presentation

Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pointers Artem A. Lenskiy, PhD June 2, 2014

  2. Content • Address of operator (&) • Pointers • Pointers and array

  3. The & Operator: Familiar example • A familiar program Could it be this way? ? Why not?

  4. The & Operator: Familiar example • A familiar program Could it be this way? ? Why not? Because: • Return type have to be known before the function call; • Return value is already in use to notify whether the call was successful or not. So we have to sacrifice it, if we want to return a value; • Number of return values cannot vary.

  5. Finding Addresses: The & Operator • scanf() uses addresses & for arguments. • The unary & operator gives you the address where a variable is stored. Any C function that modifies a value in the calling function without using a return value uses addresses! %p is the specifier for addresses 24 0B76 will be printed out on the screen The address where pooh is stored is 0B76 You can think of the address as a location in memory.

  6. How to see where variables are stored? just the value was transferred the two as and bshave different addresses

  7. Altering Variables in the Calling Function Suppose you have two variables called x and y and you want to swap their values: temp = x; x= y; y= temp; Nothing changed! Why?

  8. Declaring Pointers A pointer is a variable (or, more generally, a data object) whose value is a memory address. • int* pi; // pi is a pointer to an integer variable • char* pc; //pc is a pointer to a character variable • float* pf, * pg; // pf, pg are pointers to float variables 4 bytes Machine address that pointer contains Memory can be represented as series of bytes The declaration float* sunmass; says that sunmassis a pointer and that *sunmassis type float i.e. the value occupies 4 bytes starting from address sunmass;

  9. Using pointers ptr= &a; /* assigns a's address to ptr */ ptr= &b; /* makes ptrpoint to bah instead of to a*/ • Now you can use the indirection operator (dereferencing operator)* to find the value stored in bah. • Example: ptr = &b; /* assigns b's address to ptr */ val = *ptr; /* finds the value ptr points to and assigns to val*/ • This is equivalent to val= b; val = *ptr; finding the value ptr points to

  10. Using Pointers to Communicate Between Functions Get memory address of variables pointers Use pointer to get values

  11. Pointers and Arrays Dates and bills are also pointers but constant, so we cannot change them

  12. 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 or intvariable, 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. Remember dates +2 == &date[2] /* same address */ *(dates + 2) == dates[2] /* same value */

  13. 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 intm, *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

  14. Pointers as functions arguments • The function parameters are declared as pointers • for example to pass arrays; • The dereferenced pointers are used in the function body; • When the function is called, the addresses are passed as actual arguments. The arguments are pointes to characters (strings) Copying strings

  15. Array sorting problem • A sorting algorithm is an algorithm that puts elements of a list in a certain order. • Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists. • the output must satisfy two conditions: • The output is in nondecreasing order (each element is no smaller than the previous element according to the desired total order) • The output is a permutation (reordering) of the input. • Example ( 6 5 3 1 8 7 2 4 ) ( 1 2 3 4 5 6 7 8)

  16. Bubble sorting algorithm First Pass:( 65 3 1 8 7 2 4 ) ( 56 3 1 8 7 3 4), Compares and swaps them.( 5 63 1 8 7 2 4 ) ( 5 36 1 8 7 2 4 ), Swap since 6 > 3( 5 3 61 8 7 2 4 ) ( 5 3 16 8 7 2 4 ), Swap since 6 > 1( 5 3 6 18 7 2 4 ) ( 5 3 6 18 7 2 6 ), Does not swap them, (8 > 1), ( 5 3 6 1 8 7 2 4 ) ( 5 3 6 1 7 8 2 4 ), Swap since 8 > 7 ( 5 3 6 1 7 82 4 ) ( 5 3 6 1 7 28 4 ), Swap since 8 > 2 ( 5 3 6 1 72 8 4 ) ( 5 3 6 1 72 48 ), Swap since 8 > 2 Second Pass:( 5 3 6 1 72 4 8 ) ( 3 5 6 1 72 4 8 ), ( 3 5 6 1 72 4 8 ) ( 356 1 72 4 8 ), ( 35 6 1 72 4 8 ) ( 35 1 672 4 8 ), ( 35 6 1 7 2 4 8 ) ( 35 1 1 7 2 4 8 ), ( 35 6 1 7 24 8 ) ( 35 1 12 74 8 ), ( 35 6 12 74 8 ) ( 35 1 12 478 ), ( 35 1 124 78) ( 35 1 172 78), Third Pass: ( 35 1 124 78 ) ( 35 1 172 78 ), N comparisons maximum N-1 comparisons Don’t need to change N-2 …

  17. Bubble sorting algorithm

  18. Bubble sorting using pointers Using array notation Using pointer notation There is no &. Why?

  19. 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. //The return value is a pointer

  20. 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 + 2) + 3

  21. Initializing a Two-Dimensional Array Two methods of initialization at compilation time: char *name[3] = {“New Zealand”, “Australia”, “India”}; //arrays of strings

  22. More Dimensions intbox[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. box[3][5][8]

  23. Calculate total and average rainfall • The program goal is to find • the total rainfall for each year, • the average yearly rainfall, and • 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. Given data

  24. Example: total rainfall

  25. The program output

  26. (*)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 typevalue. • Assigning the name of the function to the pointer doublemul(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)

  27. 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.

  28. Example: Using pointers to functions 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. //call back function

More Related