Pointer Notes (MS Word)

advertisement
Pointers
1. Usage/Introduction
 “Kinda can” return 2 values = fishing = put a variable into a function,
change the variable, and get back the variable’s changed value in main or
the calling function
 Brings closer to memory = know how variables are stored in memory =
makes C one of the most basic languages = in advanced languages like
C++ and Java, cannot keep track of all variables, i.e. don’t know when a
variable if ‘un-declared’.
 Pointers are probably the least expensive, in terms of memory, things to
declare.
2. Declaration
 <data_type> * <variable_name>;
 The * needs to be put with the variable name i.e. int* ptr1, ptr2;
declares a pointer and a variable, not two pointers.
 To initialz
 The * has two usages – don’t confuse declaration with dereferencing.
3. The & operator
 Gives the address of a variable in the memory.
 Can be applied to any variable of any type, even to pointer !!
 Returns the pointer to the variable to which it is applied.
 Example: int * ptr; int var; ptr = &var; This makes ptr point to wherever
var is stored in memory. The three stmts. can be put into 1 stmt too: int
var, *ptr = &var;
 Discuss the usage of the & in the scanf and its absence in printf: in scanf,
we are writing to memory, so we wanna make sure its written exactly in
the write place, so we write directly to memory. In other words, we access
the variable wherever it is stored and change it there, so need the address
to change the variable at. In printf, however, we are just reading from
memory, so we don’t need to see the address of where the thing is stored.
4. The * operator
 Gives the stuff that is stored in the address to wherever the pointer is
pointing to i.e. returns the value of the variable to which the pointer is
pointing.
 Clearly understand the difference between the * and & operator
i. & can be applied to anything, even a pointer
ii. * can be applied only to a pointer
iii. & returns the address – is usually a mumbo-jumbo number – can
also be seen as returning a pointer to the variable to which it is
applied since pointers store variables
iv. * returns the value – is the value stored in a variable of whatever
datatype.
5. Using pointers


A pointer can point only to a variable of the same datatype i.e. datatype of
pointer and variable must match for a pointer to store the address of a
variable.
The classic swap example:
i. Version 1 : without pointers… doesn’t work !!
Int main ()
{
int a,b;
//scan into a and b. Lets say a is 17 and b is 12
swap ( a, b ); // would like to swap the values of a and b
// we are sending the variables a and b to the function. So we have
//two integer variables on the other end (in the functions’ parameter
//list ) to catch ‘em.
//print a and b
// 17 and 12 is printed and not 12 and 17
}
// the swap function
void swap ( int x, int y ) // remember – this can be envisioned as two
//variables x and y being declared right here and they being
//initialized with the values of the variables that are passed as
//arguments to the function call in the calling function (in this case,
//main)
{
int temp;
temp = x;
x = y;
y = temp;
}
// important, all the variables, x, y and temp vanish after the call. So
//the values of x and y are not saved anywhere. What we could have
//done was return x and y and store them in b and a – envision like
//(b,a) = (x,y) or even (a,b) = (y,x). but can’t return 2 values !! In
//essence, we don’t get what was desired.
ii. Version 2: wit pointers, actually works.
Int main ()
{
int a,b;
//scan into a and b. Lets say a is 17 and b is 12
swap ( &a, &b ); // would like to swap the values of a and b
//note that this time we are sending the addresses of a and b to the
//function… so who should we have as the catcher on the other end
//????????????????????????????????????????????????????????????
//two pointers… !!!
//print a and b
// this time12 and 17 is printed
}
// the swap function
void swap ( int* x, int* y ) // this time over we have two pointers.
//These pointers are pointing to the physical address in where a and b
//are stored. These pointers are also declared temporarily, but they
//are initialized with the addresses of a and b, and not their values.
//These pointers too are gonna vanish at the end of the function
{
int temp;
temp = *x; // x was a pointer. * applied to that gives back… 17. that
//17 is stored into temp… all good so far
*x = *y; // * applied to y gives back 12. Now comes the tricky part //when the * is applied to x, it does give back 17, but, it actually
//takes us to the place in memory where x was stored. Think of a
//pointer, in this case x, as a string whose one end is tied to where a
//particular variable is stored. The other end is in our hand. Applying
//* to a pointer lets us traverse on that string and it takes us to the
//hidden place where our treasure, the value, is stored. Now if that
//place happens to be on the left hand side of an assignment operator
//(the = ), it does get changed and the treasure stored there is
//overwritten by the treasure coming on the right side of the =
*y = temp; // similarly, the treasure to where y was pointing gets
//replaced by the value of temp, which was 17
}
// now, with all this treasure replacing business, what we have just
//done is change the values of a and b themselves !!!. This was done
//since we are going into wherever a and b were hiding, i.e. found
//their addresses, kicked them out and put new guys into their homes.
//So we had to kick a and b outta their houses to put new people in.
//Think of this like that : earlier (in version 1 ) we just had a and b’s
//e-mail addresses, we couldn’t kick ‘em outta their houses, with just
//their superficial addresses. So we went ahead and got their
//addresses, where they lived and went there and kicked them out.
Download