1 / 23

Pointers

Pointers. Pointers. Every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&amp;) operator. E.g. int var_1 ; printf (“address of var_1 variable: %x<br>&quot;, &amp; var_1 ); Output of these statements:

demont
Download Presentation

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. Pointers

  2. Pointers • Every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator. E.g. intvar_1; printf(“address of var_1 variable: %x\n", &var_1 ); • Output of these statements: address of var_1 variable: cef50400 • A pointer is a variable whose value/content is the address of another variable, i.e., direct address of its memory location.

  3. Pointers • Like any variable or constant, you must declare a pointer before you can use it to store any variable address. • The general form of a pointer variable declaration is: type *ptr_var_name; • type is the pointer's base type; it must be a valid C data type (primitive or programmer defined) • ptr_var_name is the name of the pointer variable. The asterisk (*) used to declare a pointer is used to designate a variable as a pointer.

  4. Pointer Declaration • Following are valid pointer declarations: int *p; /* pointer to an integer */ double *d; /* pointer to a double */ float *f; /* pointer to a float */ char *c /* pointer to a character */

  5. Initializing Pointer Variables • The address of the first byte of a variable is the address of the variable. • In the following figure, the address of the integer variable i is 3100: • We can store the address of a variable i in the pointer variable p and we say that p “points to” i. 3100 i 3101 p i

  6. Initializing Pointer Variables • One way to initialize a pointer variable is to assign it the address of a variable: int i, *p; p = &i; • Assigning the address of i to the variable p makes p point to i: p ? i

  7. Initializing Pointer Variables • It’s also possible to initialize a pointer variable at the time it’s declared: int i; int *p = &i; • The declaration of i can even be combined with the declaration of p: int i, *p = &i; p ? i ? i

  8. The Indirection Operator (*) • Once a pointer variable points to a variable, we can use the * (indirection) operator to access what’s stored in the variable. • If p points to i, we can print the value of i as follows: printf("%d\n", *p); • As long as p points to i, *p is an alias for i. • *p has the same value as i. • Changing the value of *p changes the value of i.

  9. The Indirection Operator Example p = &i; i = 10; printf("%d\n", i); /* prints 10 */ printf("%d\n", *p); /* prints 10 */ *p = 20; printf("%d\n", i); /* prints 20 */ printf("%d\n", *p); /* prints 20 */ p p p ? 10 20 i i i

  10. The Indirection Operator • Applying the indirection operator to an uninitialized pointer variable causes unpredictable behavior: int *p; printf("%d", *p); /*** WRONG ***/ • Assigning a value to *p is also wrong when p is not pointing to any valid variable: int *p; *p = 1; /*** WRONG ***/

  11. Pointer Assignment • We canuse of the assignment operator to copy pointers of the same type. • Assume the following declaration: int i, j, *p, *q; • Example of pointer assignment: p = &i; q = i; • qnow points to the same place as p p p ? ? i i q q

  12. Pointer Assignment • If p and q both point to i, we can change i by assigning a new value to either *p or *q: *p = 1; *q = 2; • Any number of pointer variables may point to the same variable. q q p p 2 1 i i

  13. Pointer Assignment • The following assignment statements are NOT the same: q = p; *q = *p; • The first statement is a pointer assignment, but the second assigns the value of whatever is pointed-to by p to whatever is pointed-to by q.

  14. Pointer Assignment p = &i; q = &j; i = 1; *q = *p; p q p q 1 ? 1 1 i j i j

  15. Pointers and Arrays

  16. Pointers and Arrays • Arrays and pointers are very much related concepts • The name of an array acts as a pointer to the first element of that array • Consider the following declaration: intmy_array [10]; int *my_ptr; • Then these are valid assignment statements: my_ptr = &my_array[0]; OR my_ptr = my_array;

  17. Pointers and Arrays • After this statement, my_ptr and my_array are equivalent. • Difference between the two: • my_ptr can be assigned some other address since it is a pointer variable • my_array can never be re-assigned since it will always represent the same block of 10 integers. • Therefore, the following statement is invalid: my_array = my_ptr;

  18. Pointers and Arrays • After assigning my_ptr the address of the first element of my_array, we can access other elements of my_array just as well: my_ptr = &my_array[0]; *(my_ptr + 1) = 20; // assign 20 to 2nd element of my_array *(my_ptr + 4) = 30; // assign 30 to 5th element of my_array my_ptr= &my_array[3]; *(my_ptr + 1) = 50; // assign 50 to 5th element of my_array • Practice program

  19. Pointer Arithmetic • For pointers, only addition and subtraction are allowed • Other arithmetic operations don’t make much sense • With pointers however, addition and subtraction have different behavior than integer addition or subtraction • That depends upon the type of data a pointer points to…

  20. Pointer Arithmetic • As we know that different data types have different sizes • Size of data types depends upon the compiler being used • Suppose, char is 1-byte, int is 2-bytes and float is 4-bytes • Now we declare three pointers of types char, int and float as follows: char *char_ptr; int *int_ptr; float *float_ptr;

  21. Pointer Arithmetic • Assume that the three pointers point to the three memory locations 1000, 2000 and 3000 respectively • This can be visually represented as follows: 3003 3007 1003 2003 2001 3001 3005 1001 3006 1002 3000 3002 1000 2000 3004 2002 1000 2000 3000 char_ptr float_ptr int_ptr

  22. Pointer Arithmetic • See the effect of following statements: char_ptr++; int_ptr++; float_ptr++; 3003 3007 1003 2003 2001 3001 3005 1001 3006 1002 3000 3002 1000 2000 3004 2002 1001 2002 3004 char_ptr float_ptr int_ptr

  23. Pointer Arithmetic • Notice the difference between: int_ptr++; (*int_ptr)++; 300 300 2003 2003 2001 2001 126 125 2002 2000 2002 2000 2002 2000 int_ptr int_ptr

More Related