200 likes | 284 Views
EEE 243B Applied Computer Programming. Pointers meet Functions Pointers to Pointers §9.2 – 9.3 Prof Sylvain P. Leblanc. Review. How will the following two variables be represented in memory: char character = 'a' ; char string[] = "a";
E N D
EEE 243BApplied Computer Programming Pointers meet Functions Pointers to Pointers §9.2 – 9.3 Prof Sylvain P. Leblanc
Review • How will the following two variables be represented in memory: char character = 'a' ; char string[] = "a"; • Will my friend the compiler complain about this? What will happen?: char temp[] = ""; char text[] = "Some text"; inti; for (i = 0, strlen(text), i++) temp[i] = text[i]; Prof S.P. Leblanc
Outline • Pointers and functions • Passing Pointers as Parameters • Returning a Pointer from a Function • Function Communication Summary • Pointers to pointers • voidpointers • Uses • Declaration • Casting Prof S.P. Leblanc
1. Pointers meet functions • One of the most useful applications of pointers in C is to pass function parameters by address • As we saw with arrays, it is sometimes a lot more efficient to pass information by reference instead of passing it by value • Passing data by reference is not only efficient, but sometimes necessary in order to permit some functions to do their work Prof S.P. Leblanc
1a. Passing Pointers as Parameters • As we discussed with arrays, when a parameter is passed by reference, any changes made to the referred variable inside the function are directly applied to the actual variable • Consider functions that would implement sorting algorithms as a great example of the need to pass parameters by reference. Prof S.P. Leblanc
1a. Passing by Value is Unworkable Prof S.P. Leblanc
1a. Passing by Reference Works Prof S.P. Leblanc
1a. Pointers as Parameters • Only pass pointers (reference) when it is necessary to have access to the variable • when you want to change the value of the actual parameter • If there is no need to modify it, pass your parameter by value • you will therefore protect the actual parameter from unintended changes Prof S.P. Leblanc
1b. Returning Pointers from functions • Functions do not only accept pointers they can return them as well!!! • This is useful when you want to return a reference to one of the parameters passed to the function • An example follows, where a function compares two int and returns a reference to the smallest • The pointer (address) is then stored in a pointer variable in the caller Prof S.P. Leblanc
1b. Returning Pointers from functions Prof S.P. Leblanc
1c. Function Communication Summary Prof S.P. Leblanc
2. Pointers to pointers • So if a pointer holds an address, can it hold the address of another pointer? • Yes it can. • A pointer to a pointer represents two levels of indirection • In fact there is no limit to number of levels of indirection you can use in C • In practice, however, you will seldom go beyond two levels of indirection Prof S.P. Leblanc
2. Pointers to pointers • Here is how you would declare a pointer to a pointer to an int int** p; //a pointer to a pointer to an int • Each time you want to access the value being pointed to by a pointer to a pointer, you must dereference it twice: int** p; int* r; int a = 0; r = &a; //r points to a p = &r; //p points to r which points to a **p = 5; //value of a changes to 5 Prof S.P. Leblanc
3. void pointers • As indicated in the last lecture, C does not generally allow for comparison or mixing of pointer types. There is one exception to this rule: • the void pointer • In C the void pointer is a generic or universal pointer that can be used where the type of data is not known at compile time or execution Prof S.P. Leblanc
3a. Uses of void pointers • We will soon look at dynamic memory allocation. When you ask for memory, C will return you a pointer for the memory that you requested. This pointer is a void pointer • Because the function that grants you the memory (malloc) has no idea of the type you are using • If I want to write a generic function to sort all kinds of data (int, char, float, or derived types), I can accept a void pointer for the data to be sorted Prof S.P. Leblanc
3b. void Pointer Declaration • You would declare a void pointer like this: void* pointerToVoid; • Because the pointer does not point to a variable of a specific type, it cannot be dereferencedin a statement intmyVar; myVar = *pointerToVoid; //error! *pointerToVoid = myVar; //error! Prof S.P. Leblanc
3c. Casting Pointers as void • We can use casting with pointer types int* pInt; char* pChar; void* pVoid; … pInt = pChar; //compile error pInt = (int*) pVoid; //good pChar = (char*) pVoid; //good pInt = (int*) pChar; //valid but not logical! Prof S.P. Leblanc
3c. Casting Pointers as void • You have to cast a function if it returns a void pointer. Here we show an example of the malloc function: //malloc returns an address to an empty block of memory void* malloc(intsize); //as it is defined in C int* iPtr; … iPtr = (int*)malloc(10 * sizeof(int)); //casting Prof S.P. Leblanc
Quiz Time char anotherChar, myChar = 'a'; int* pNum; char* pChar; char** ppChar; void* pNothing; void main(void) { ppChar = &pNum; //1. Is this legal? pChar = &myChar; //2. Is this legal? pNothing = pChar; //3. Is this legal? ppChar = &pChar; //4. Is this legal? *pChar = 'z'; //5. Is this good? What does it do? **ppChar = 'y'; //6. Is this good? What does it do? anotherChar = *pNothing; //7. Can I do this? pChar = pNothing; //8. Is this legal? } Is pChar = pNothinglegal? Prof S.P. Leblanc
Next Lecture • Modules and Information Hiding Prof S.P. Leblanc