Unformatted text preview:

- Pointers* Every variable in a program is stored somewhere in memory* The memory location of that variable is referred to as its address* Variables are said to be born when they are defined* When a variable is born (defined) it is immediately associated with an addressThese Notes are in C, not C++, BUT only the I/O differscout << k is similar to printf(“%d”, k)cin >> k is similar to scanf(“%d””, &k)- Example 1int my_int; // defining my_intfloat my_float; // defining my_floatchar my_char;C programming language – Pointers by Julius Dichteri ?????????? 1000 ?????????? ?????????? 1004 1012 my_intmy_floatmy_doubleThe name of the variableThe contents, or value of the variableThe address of the variablePointer variables are just like any other variables. They have all the same properties1. Name2. An address3. A valueWhere they differ is that a pointer variable’s value carries a meaning. The value IS an addressLet us now add three more pointer variables to our definitions That results in the followingint * my_intp;float * my_floatp;char * my_charp;Our memory now looks like shown belowC programming language – Pointers by Julius Dichterii ?????????? 1000 ?????????? ?????????? 1004 1012 my_intmy_floatmy_char ?????????? 1013 ?????????? ?????????? 1017 1021 my_intpmy_floatpmy_charpNow, let us make some assignments and observe how ordinary variables and pointer variables behavemy_int = 6; my_float = 3.4; my_char = ‘J’;my_intp = &my_int;my_floatp = &my_floatp;my_charp = &my_char;The ordinary variable has stored a pure value. A pure value is simply a value to which the computer program has attached no meaning. Any value within the bounds of the type of variable is ok with the programA pointer variable also has a value, but that value is not pure. It is an address, and specifically an address of a particular type of variable. For example, my_intp is holding an address of an int type variableSince the pointer variable holds the address of another variable, we say that itpoints to it, and show the relationship in the following way:C programming language – Pointers by Julius Dichteriii 6 1000 3.4 J 1004 1012 my_intmy_floatmy_char 1000 1013 1004 1012 1017 1021 my_intpmy_floatpmy_charpWhen we print variables, we specify to the printf() function the type of the variable as well as its value.Each variable has a type:For example, my_int is if type int, my_char is of type char.It follows then that my_intp is of type int * and my_charp is of type char *When printing we have to use special care to be sure that the value we pass tothe printf() function is what we really wantprintf(“my_int = %d, and its address is %d.\n”,my_int, my_intp);=> my_int = 6, and its address is 1000.We can achieve the same effect by using the following constant addressprintf(“my_int = %d, and its address is %d.\n”,my_int, &my_int);=> my_int = 6, and its address is 1000.This is obvious since we assigned &my_int to my_intp earlierC programming language – Pointers by Julius Dichteriv my_intmy_floatmy_char my_intpmy_floatpmy_charp- Dereferencing Pointers- Using pointers in initializing statementsint i = 6, int_p = &i;double d = 3.421, double_p = &d;- What are the main reasons for using pointers?1. To enable changing a value in the function from the calling environment2. To speed up parameter passing in function calls3. To use standard file I/O4. To perform fast binary file I/O5. To use strings in "C"6. An alternative to using ordinary arrays7. To utilize dynamically allocated memory libraries8. To access a hidden variable in a nested scope9. Passing functions as parameters to other functions10 What was (9) again???- Dereferencing PointersC programming language – Pointers by Julius DichtervSince the pointer holds the address of the object it points to, we can alwaysaccess that object by simply dereferencing the pointer.If we place an asterisk in front of a pointer, we get the equivalent of whatever itis pointing to (in lvalue, rvalue, and type)int i = 5, int_p = &i, k;k = *int_p + 4; Same as the rvalue of int variable iNow k has the value of 9*int_p = 6 Same as the lvalue of int variable iNow i has been changed to be 6*int_p = *int_p++ Dangerous, indeterminant assignment, but legal in "C"int i = 5, int_p = &i, int k = 3;printf("i = %d\t*int_p = %d\n",i, *int_p);=> i = 5 *int_p = 5int_p = &k;printf("i = %d\t*int_p = %d\n",i, *int_p);=> i = 5 *int_p = 3C programming language – Pointers by Julius Dichtervi- Star ValueWe say that a variable when defined has a star valueint i , j , k;Variables i, j, and k have a star value of zero (0)int * ip, * jp, * kp;Variables ip, jp, and kp have a star value of one (1)Note that the asterisk does not apply to all variables that follow itint * ip, jp, kpNow only ip is a pointer, star value one. Variables jp and kp are plain int, starvalue of zeroWhen we dereference a pointer, we knock down its star value by oneWhen we precede any variable by the &, we increase its star valueTwo expressions cannot be compatible if their star values do not matchint k = 7, m = 5, * ip = &k, *jp;m = *ip + m; /* ok */ip = &m; /* ok */ip = jp; /* ok */ip = &jp; /* error */C programming language – Pointers by Julius Dichtervii- Using Pointers When Calling Functionsint i = 10, * ip = &i; void f(int * p) {printf ("i = %d\n",i); *p += 1; }f(ip);printf ("i = %d\n",i);=> i = 10 i = 11- Pointer ArithmeticThe type of a pointer is very important. While adding one (1) to a numericvariable increases the value by one, adding on to a pointer variable may not dothe sameint i=17, j=5, k=4, *ip = &j;double d=5.1, e=6.11, f=3.22, *dp = &e;printf ("*ip = %d *(ip+1) = \n",*ip, *(ip+1));=> *ip = 5 *(ip+1) = 17printf ("ip = %d ip+1 = %d dp = %d dp+1 = %d\n",ip,ip+1,dp,dp+1);ip = 1000 ip+1 = 1004 dp = 884 dp+1 = 892Pointers can be used in arithmetic. When adding some number k to a pointer weactually add k * (number of bytes in the type to which it points)This can be useful in both string as well as array operations.C programming language – Pointers by Julius


View Full Document

UB CS 400 - Pointers

Download Pointers
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Pointers and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Pointers 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?