170 likes | 362 Views
Pointers. 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?. ?. Why not?. Because:.
E N D
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? ? 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 sacrifices it, if we want to return a value; • Number of return values cannot vary.
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.
How to see where variables are stored? just the value was transferred the two poohsand bahshave different addresses
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?
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 sunmassis a pointer and that *sunmassis type float i.e. the value occupies 4 bytes starting from address sunmass; 4 bytes Machine address that pointer contains Memory can be represented as series of bytes
Pointers ptr= &pooh; /* assigns pooh's address to ptr */ ptr = &bah; /* makes ptrpoint 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;
Using Pointers to Communicate Between Functions Get memory address of variables The program that uses pointers to make the interchange() function work. pointers
Pointers and Arrays short double pointers + 0: 0x0064fd20 0x0064fd28 pointers + 1: 0x0064fd22 0x0064fd30 pointers + 2: 0x0064fd24 0x0064fd38 pointers + 3: 0x0064fd26 0x0064fd40
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 */
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
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. Example 1: Sorting function array passed as a pointer Example 2: copying strings
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.
(*)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)
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.