Presentation is loading. Please wait.

Presentation is loading. Please wait.

+ Pointers. + Content Address of operator (&) Pointers Pointers and array.

Similar presentations


Presentation on theme: "+ Pointers. + Content Address of operator (&) Pointers Pointers and array."— Presentation transcript:

1 + Pointers

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? 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 sacrifices it, if we want to return a value; Number of return values cannot vary. Because:

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

6 + How to see where variables are stored? the two poohs and bahs have different addresses just the value was transferred

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 */ The declaration float* sunmass; says that sunmass is a pointer and that *sunmass is type float i.e. the value occupies 4 bytes starting from address sunmass; Memory can be represented as series of bytes Machine address that pointer contains 4 bytes

9 + Pointers ptr = &pooh; /* assigns pooh's address to ptr */ ptr = &bah; /* makes ptr point to bah instead of to pooh */ Now you can use the indirection operator (dereferencing operator) * to find the value stored in bah. val = *ptr; /* finding the value ptr points to */ The statements ptr = &bah; val = *ptr; taken together amount to the following statement: val = bah;

10 + Using Pointers to Communicate Between Functions The program that uses pointers to make the interchange() function work. pointers Get memory address of variables

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

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 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 */

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

14 + Pointers as functions arguments 1. The function parameters are declared as pointers (for example to pass arrays); 2. The dereferenced pointers are used in the function body; 3. 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

15 + 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.

16 + (*)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)

17 + 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 "+ Pointers. + Content Address of operator (&) Pointers Pointers and array."

Similar presentations


Ads by Google