Similar presentations

3
Why use pointers? Some routines can be more efficiently written with pointers Some routines need pointers

4
Memory Usage Programmer declares a variable int x; Compiler sets aside memory 4 bytes for an integer Memory has a unique address Address and variable name are associated Address is hidden from the programmer

5
Memory Address Address is an integer Can be printed out with %p –result represents a segment and an offset what segment of memory the location is in what offset (how far from the start of the segment) the location is –Most compilers show the two separated by a colon –Code Warrior does not

6
Pointers Address can be stored in another variable –pointer Pointer - variable that contains the address of another variable

7
Using Pointers Pointers must be declared before use Names must be unique Declaration: typename * ptrname; int * x; * - Indirection operator –Informs the compiler that the variable is a pointer

15
Arrays and Pointers The name of an array is a pointer to the array –points to the first byte of the first element –remains fixed throughout program execution Example: if int x[10] is an array declaration, then x and &x[0] are equivalent both represent the addresses of the first array element

16
Arrays and Pointers Consider the following code: int array[3] = {1, 2, 3}, *ptr = NULL, z=2; ptr = array; –array points to the first element in the array cannot be changed array = &z; causes a compiler error –ptr points to the first element in the array can be changed ptr = &z; does not cause a compiler error

17
Pointer Arithmetic Pointers are often manipulated arithmetically –incremention –decrementation The compiler ‘knows’ the size of a data type, and will increment the address accordingly –adding 1 to an integer pointer increments the address by 4 integers take up 4 bytes of space

22
Pointers and Functions Arrays cannot be passed into functions, but pointers can –In our programs when we pass in an array, we have actually passed in a pointer Prototype of a function that accepts an array int func1(int []); int func1(int x[]); int func1(int *x); } Almost equivalent, only difference is the first does not specify variable name

24
Characters, strings, and an introduction to dynamic memory allocation

25
Strings arrays of characters char string[10]; string: array of characters ending with the null character –null character: ‘\0’ To store ‘Computer’ in a string, need an array of 9 characters (8 for the letters, one for the null character)

27
String / Pointer Relationship Recall: Array name is a pointer to the first element in the array Character array is a pointer to the first element in an array –i.e. the beginning of the string ‘\0’ indicates the end of the string Array size is incidental Array name, and ‘\0’ give us all the information we need

29
Allocating Strings Strings can be allocated without actually declaring an array –Only the pointer to the first character and the ‘\0’ is needed Example: char * msg = “Hello”;is equivalent to: char msg[] = “Hello”;

30
Dynamic Allocation of Strings Dynamic memory allocation –allocating memory ‘on the fly’ –memory allocated at program execution time –using the malloc( ) function is one way to do this Always include Example: char *ptr = NULL; ptr = malloc(5);

36
Displaying Strings - puts( ) puts( ) accepts a pointer to a string –string literal evaluates as a pointer to a string prints the string to the screen automatically inserts new line character at end of string Example: char * name = “CSCI 131”; puts(name); puts(“this is a string literal”);

51
malloc( ) function revisited returns an address –pointer to type void –can store any of C’s data types accepts an integer parameter allows for more efficient memory usage malloc( ) is not restricted to character arrays should cast the returned pointer

56
realloc( ) functionality 1. Current space can be expanded –space is allocated, original pointer is returned 2. Current space cannot be expanded –new block is allocated –existing data is copied from old to new block –old block is freed –pointer to new block is returned

59
free( ) function When dynamically allocated memory is no longer needed, the resources should be freed –amount of memory available for allocation is finite Passing the pointer to the free( ) function will free up memory free( ) function –accepts a pointer as an argument –has no return value (i.e. void)

64
Passing Values to Functions Passing by Value –Changes to the parameter do not affect changes to the argument Passing by Reference –Arrays, pointers –Changes to the parameter do affect changes to the argument

65
Passing by Value x = 5; myFunction(x) –causes a copy of x to be made, and accessible to the function Changes at addresses 2018-2021 do not affect the contents of addresses 1000-1003

66
Passing by Reference Used with arrays and pointers myFunction(&x) –causes the address of x to be passed to the function Changes are made at the address (1000), and hence changes made in the function are reflected in the calling function

67
Why pass by Reference? If you need to modify the argument (i.e. if you need to modify the value of the variable in the calling program within the function) If you are using arrays

68
Passing by Reference Passing by reference Advantage: The function can modify the value of the argument Disadvantage: The function can modify the value of the argument Passing by reference should be avoided unless there is a specific reason for doing it

About project

Feedback

To ensure the functioning of the site, we use cookies. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy &amp Terms.
Your consent to our cookies if you continue to use this website.