Functions4-B - A brief intro to pointers for the purposes...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
A brief intro to pointers for the purposes of passing reference parameters With respect to functions, we have only talked about pass by value parameters. Today we will discuss pass by reference parameters. But, in order to use these, we have to understand a bit about how pointers work in C. A computer's memory can be visualized as a very long numbered array of bytes. Each byte of memory is numbered with it's address. Perhaps a simple analogy would be that each byte of memory is a house on a really long street. Each of these houses are numbered in order. If you ever want to retrieve a value in a house, one way you could do so is use the correct street address. This is what a pointer is - a street address directing you to where some information is stored. However, this is not how you access or manipulate information usually. Instead, you create a variable through a declaration. Internally, when you declare a variable, what the computer does is find a "house" to store that variable. Once a house is found, it keeps a list of all the declared variables and the houses they live in. Anytime in your program you refer to a variable, it automatically goes to the contents of the house that match the address given on the main list. In some sense, you are referring to the house by who lives there. (The computer takes care of looking up where that person lives. ..) What a pointer allows you to do is access a variable, without using a name for it. Instead, a pointer just stores a location in memory, and through that pointer, you are allowed to change the value of the variable stored at that location.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
First, let's go through the basic syntax of how to declare a pointer: int *p; This literally says to create a variable p. The type of the variable p is a "pointer to an integer," not just a "pointer." p can not store a value, but it can store a memory address. Now, let's say we have the added declaration: int a = 7; One statement that is useful with pointers is to assign them a location to point. However, this statement: p = a; is illegal? Why? Instead, we need a way to find the memory address of where the variable a is stored to make this a valid statement. We can do this with the "address of" operator, &. The correct statement is p = &a; Basically here is what these three statements are doing in memory:
Background image of page 2
The other thing we want to be able to do is to access a variable that a pointer is pointing to. We can do this through the "dereferencing" operator, *. Notice that * is used in two different ways. When we declare a pointer, we use * to denote that a variable is a pointer. But NOW, we see that the * allows
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/09/2009 for the course COP 3223 taught by Professor Guha during the Fall '08 term at University of Central Florida.

Page1 / 12

Functions4-B - A brief intro to pointers for the purposes...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online