1 / 37

Basic Pointers

305171 Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University. Basic Pointers. Pointer Fundamentals.

betsy
Download Presentation

Basic 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. 305171 Computer Programming RattapoomWaranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University Basic Pointers

  2. Pointer Fundamentals • Pointers are very powerful structures that can be used by C programmers to work with variables, functions, and data structures through their memory addresses. • Pointers are variables that contain a memory address as their value. • In other words, a pointer variable contains a memory address that points to another variable.

  3. Pointer Fundamentals • Example 0x948307 5 ptr x 0x94830b 0x9402ff 0x94830b 0x94830e

  4. Pointer Fundamentals • Pointers are variables. They follow all the normal naming rules of regular variables. • As with regular variables, you must declare pointer variables before using them. • There is a type of pointer for every data type in C; there are integer pointers, character pointers, floating-point pointers, and so on. • You can declare global pointers or local pointers, depending on where you declare them.

  5. Declaring and Initializing Pointer Variables • Pointer variables must be declared before they can be used, as shown in the following code: int x = 0; intiAge = 30; int *ptrAge; • Simply place the indirection operator (*) in front of the variable name to declare a pointer.

  6. Declaring and Initializing Pointer Variables int x = 0; int iAge = 30; int *ptrAge; 0 ptrAge x 0x94830b 0x9422af 30 iAge 0x94830e

  7. Declaring and Initializing Pointer Variables • When we declared the pointer ptrAge, we were telling C that we want our pointer variable to indirectly point to an integer data type. • Our pointer variable, however, is not pointing to anything just yet. • To indirectly reference a value through a pointer, we must assign an address to the pointer, as shown here: ptrAge = &iAge;

  8. Declaring and Initializing Pointer Variables int x = 0; int iAge = 30; int *ptrAge; ptrAge = &iAge; 0 ptrAge x 0x94830b 0x9422af 0x94830e 30 iAge 0x94830e

  9. Declaring and Initializing Pointer Variables ptrAge = &iAge; • In this statement, we assign the memory address of the iAge variable to the pointer variable (ptrAge). • This statement is telling C that we want to assign the memory address of iAge to the pointer variable ptrAge. • The operator (&) is often referred to as the “address of” operator.

  10. Declaring and Initializing Pointer Variables • Conversely, we can assign the contents of what the pointer variable points to—a non-pointer data value—as demonstrated next. x = *ptrAge; • The variable x will now contain the integer value of what ptrAge points to—in this case the integer value 30.

  11. Declaring and Initializing Pointer Variables int x = 0; int iAge = 30; int *ptrAge; ptrAge = &iAge; x = *ptrAge; 30 ptrAge x 0x94830b 0x9422af 0x94830e 30 iAge 0x94830e

  12. Summary • There are two pointer operators in C: • & The "address of" operator • * The dereferencing operator • Any time you see the & used with pointers, think of the words "address of." The & operator always produces the memory address of whatever it precedes. • The * operator, when used with pointers, either declares a pointer or dereferences the pointer's value.

  13. Assigning value to pointer variables • We can assign non-address values to pointers by using an indirection operator (*). int x = 5; int *iPtr; iPtr = &x; //iPtr is assigned the address of x *iPtr = 7; //the value of x is indirectly changed to 7 • This program assigns the memory address of variable x to the pointer variable (iPtr) and then indirectly assigns the integer value 7 to variable x.

  14. Assigning value to pointer variables int x = 5; int *iPtr; iPtr = &x; *iPtr = 7; 5 x 0x15

  15. Assigning value to pointer variables int x = 5; int *iPtr; iPtr = &x; *iPtr = 7; 5 iPtr x 0x15 0xf9

  16. Assigning value to pointer variables int x = 5; int *iPtr; iPtr = &x; *iPtr = 7; 5 iPtr x 0x15 0xf9 0x15

  17. Assigning value to pointer variables int x = 5; int *iPtr; iPtr = &x; *iPtr = 7; 7 5 iPtr x 0x15 0xf9 0x15

  18. Assigning value to pointer variables int x = 5; int *iPtr; iPtr = &x; *iPtr = 7; 7 iPtr x 0x15 0xf9 0x15

  19. Pointer Fundamentals • Exercise 1: Pointer Fundamentals • Start and prepare to run a new project in VC++ Express • Copy the code from pointer.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

  20. Pointer Fundamentals • Exercise 1: Pointer Fundamentals • Start and prepare to run a new project in VC++ Express • Copy the code from pointer.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

  21. Pointer Fundamentals • Exercise 2: More on Pointers • Start and prepare to run a new project in VC++ Express • Copy the code frommorepointer.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

  22. Pointers and Functions • One of the greatest benefits of using pointers is the ability to pass arguments to functions by reference. • By default, arguments are passed by value in C, which involves making a copy of the incoming argument for the function to use.

  23. Pointers and Functions • Passing arguments by value is not the most efficient programming means for programming in C. Making copies of two integer variables may not seem like a lot of work, but in the real world, C programmers must strive to minimize memory use as much as possible. • when C passes arguments by value you are unable to modify the original contents of the incoming parameters. This is because C has made a copy of the original variable and hence only the copy is modified.

  24. Passing Parameters by Value • Exercise 3: Passing Parameters by Value • Start and prepare to run a new project in VC++ Express • Copy the code frompass_by_value.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

  25. Passing Parameters by Value • After studying the code, you can see that we try to swap the incoming parameters. • The argument appears to be swapped when we print the contents in the swap’s printf() function. • However, when we print the contents of variables a and b from the main() function, it indeed is not swapped.

  26. Passing Parameters by Reference • To solve this problem, we use pointers to pass arguments by reference. • More specifically, we can pass the address of the variable (argument) to the function using indirection.

  27. Passing Parameters by Reference • Exercise 4: Passing Parameters by Reference • Start and prepare to run a new project in VC++ Express • Copy the code frompass_by_reference.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

  28. Pointers and Arrays • C allows pointer arithmetic (addition and subtraction). Suppose we have: char array[3]; char *array_ptr = &array[0]; • In this example, *array_ptr is the same as array[0], while *(array_ptr+1) is the same as array[1], *(array_ptr+2) is the same as array[2], and so on. Note the use of parentheses. • However, (*array_ptr)+1 is not the same as array[1]. The +1 is outside the parentheses, so it is added after the dereference. • So (*array_ptr)+1 is the same as array[0]+1.

  29. Pointers and Arrays 1 array[0] *array_ptr 0x120 0x120 2 array[1] 0x124 3 array[2] 0x128 4 array[3] 0x12c

  30. Pointers and Arrays 1 array[0] 0x120 2 array[1] 0x124 3 *(array_ptr+2) array[2] 0x128 0x128 4 array[3] 0x12c

  31. Pointers and Arrays • Exercise 5: Pointers and Arrays • Start and prepare to run a new project in VC++ Express • Copy the code from pointer_and_array1.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

  32. Pointers and Arrays • Exercise 6: Pointers and Arrays • Start and prepare to run a new project in VC++ Express • Copy the code from pointer_and_array2.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

  33. Pointers and Arrays • Exercise 7: Pointers and Arrays • Start and prepare to run a new project in VC++ Express • Copy the code from count_non0_index.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

  34. Pointers and Arrays • Exercise 8: Pointers and Arrays • Start and prepare to run a new project in VC++ Express • Copy the code from count_non0_ptr.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

  35. Passing Arrays to Functions • Knowing that an array name contains a memory address that points to the first element in the array, we can surmise that array names can be treated much like a pointer when passing arrays to functions. • It is not necessary to deal with unary (&) or indirection (*) operators when passing arrays to functions, however. • More importantly, arrays passed as arguments are passed by reference automatically.

  36. Passing Arrays to Functions • Exercise 9: Passing Arrays to Functions • Start and prepare to run a new project in VC++ Express • Copy the code from pass_array1.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

  37. Passing Arrays to Functions • Exercise 10: Passing Arrays to Functions • Start and prepare to run a new project in VC++ Express • Copy the code from pass_array2.c and paste into the code editor. • Run the code and observe the result. • Let’s play with the code.

More Related