1 / 24

Understanding Pointers

Understanding Pointers. To understand pointers, you need to know how your computer stores information in memory. RAM consists of many thousands of sequential storage locations, and each location is identified by a unique address . RAM is Random Access Memory.

tricia
Download Presentation

Understanding 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. Understanding Pointers To understand pointers, you need to know how your computer stores information in memory. RAM consists of many thousands of sequential storage locations, and each location is identified by a unique address.

  2. RAM is Random Access Memory The amount of RAM is limited in a computer This means that the memory addresses in a given computer range from 0 to a maximum value that depends on the amount of memory installed.

  3. RAM used all the time • Declaring variables in a C program sets aside a memory location with a unique address to store that variable. • The compiler associates that address with the variable's name. • When your program uses the variable name, it automatically accesses the proper memory location. • The location‘saddressis used, but it is hidden from you, and you need not be concerned with it.

  4. An important difference in High and Low level programming languages • RAM addressing handled automatically by high level languages like : • C • C++ • Java • Any other language except Machine language and Assembly language which are low level languages

  5. So far… • A program variable is stored at a specific memory address.

  6. Creating Pointers to simple (non-array) variables • The address of the variable is a number and can be treated like any other number in C. • If you know a variable's address, you can create a second variable in which to store the address of the first. • The first step is to declare a variable to hold the address of the first variable.

  7. Pointers and Simple Variables • Declaring Pointers • A pointer is a numeric variable and must be declared before it can be used. • Pointer variable names follow the same rules as other variables and must be unique. • You can name pointers anything you want (within C's naming rules). • A pointer declaration takes the following form: • typename *ptrname;

  8. typename *ptrname; • typenameis any of C's variable types and indicates the type of the variable that the pointer points to. • The asterisk (*) is the indirection operator, and it indicates that ptrname is a pointer to type typename and not a variable of type typename. • Pointers can be declared along with nonpointer variables.

  9. char *ch1, *ch2; • /* ch1 and ch2 both are pointers to type char */ • float *value, percent; • /* value is a pointer to type float, and • /* percent is an ordinary float variable */

  10. Initializing Pointers • Uninitialized pointers can be used, but the results are unpredictable and potentially disastrous. So avoid that. • Your program can initialize pointers by using the address-of operator, the ampersand (&) • initialize a pointer with a statement of the form • pointer = &variable;

  11. Using Pointers(How to use them?) • The indirection operator (*) comes into play again. • When the * precedes the name of a pointer, it refers to the variable pointed to.

  12. Using pointers (Example) • Let us assume that pointer p_ratehas been initialized to point to the variable rate. • If you write *p_rate, it refers to the variable rate. • If you want to print the value of rate (which is 100 in the example), you could write • printf("%d", rate); or this: • printf("%d", *p_rate);

  13. Direct and Indirect Access • Accessing the contents of a variable by using the variable name is called direct access. • Accessing the contents of a variable by using a pointer to the variable is called indirect access or indirection.

  14. Summary • If you have a pointer named ptr that has been initialized to point to the variable var, the following are true: • *ptr and var both refer to the contents of var (that is, whatever value the program has stored there). • ptr and &var refer to the address of var.

  15. Basic Pointer Use #include <stdio.h> /* Declare and initialize an int variable */ intvar = 1; /* Declare a pointer to int */ int *ptr; int main() { /* Initialize ptr to point to var */ ptr = &var; /* Access var directly and indirectly */ printf("\nDirect access, var = %i", var); printf("\nIndirect access, var = %i", *ptr); /* Display the address of var two ways */ printf("\n\nThe address of var = %i", &var); printf("\nThe address of var = %i\n", ptr); return 0; }

  16. Pointers and Variable Types • Different variable types occupy different amounts of memory. • Each individual byte of memory has its own address, so a multibyte variable actually occupies several addresses. • The address of a variable is actually the address of the first (lowest) byte it occupies. Example : • intvint = 12252; • char vchar = 90; • float vfloat = 1200.156004; • In this example, the int variable occupies two bytes • The char variable occupies one byte, and the float variable occupies four bytes.

  17. Pointers and Arrays • There is a special relationship between pointers and arrays in C.

  18. The Array Name as a Pointer • An array name without brackets is a pointer to the array's first element. • data[], data is the address of the first array element. • The expression &data[0] gives the address of the array's first element. • In C, the relationship (data == &data[0]) is true. • The name of an array is a pointer to the array. Remember that this is a pointer constant; it • can't be changed and remains fixed for the duration of program execution. • This makes sense: If you changed • You can, however, declare a pointer variable and initialize it to point it to the first element of an array or it could be pointed at other elements of an array.

  19. Array Element Storage • The elements of an array are stored in sequential memory locations with the first element in the lowest address. • Subsequent array elements (those with an index greater than 0) are stored in higher addresses. How much higher depends on the array's data type (char, int, float, and so forth).

  20. Pointer Arithmetic • Incrementing Pointers • For single increments • use ++ operator (for single increments) • ptr_to_int++; • the value of ptr_to_int is increased by the size of type int (usually 2 bytes) • For increments greater than 1. • If you add the value n to a pointer, C increments the pointer by n array elements of the associated data type. Therefore, • ptr_to_int += 4; (increases the value stored in ptr_to_int by 8 (assuming that an integer is 2 bytes)

  21. Decrementing Pointers • The same concepts that apply to incrementing pointers hold true for decrementing pointers. • Decrementing a pointer is actually a special case of incrementing by adding a negative value. • If you decrement a pointer with the -- or -= operators, pointer arithmetic automatically adjusts for the size of the array elements.

  22. Usage • You can use pointer arithmetic and pointer notation to access array elements.

  23. Other Pointer Manipulations • The only other pointer arithmetic operation is called differencing, which refers to subtracting two pointers. • If you have two pointers to different elements of the same array, you can subtract them and find out how far apart they are. • Again, pointer arithmetic automatically scales the answer so that it refers to array elements. • Thus, if ptr1 and ptr2 point to elements of an array (of any type), the following expression tells you how far apart the elements are: ptr1 - ptr2 • Pointer comparisons are valid only between pointers that point to the same array.

  24. List of Pointer operations. • Assignment You can assign a value to a pointer. The value should be an address, obtained with the address-of operator (&) or from a pointer constant (array name). • Indirection The indirection operator (*) gives the value stored in the pointed-to location. • Address of You can use the address-of operator to find the address of a pointer, so you can have pointers to pointers. • Incrementing You can add an integer to a pointer in order to point to a different memory location. • Decrementing You can subtract an integer from a pointer in order to point to a different memory location. • Differencing You can subtract an integer from a pointer in order to point to a different memory location. • Comparison Valid only with two pointers that point to the same array.

More Related