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

Info icon This 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.
Image of page 1

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

View Full Document Right 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:
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
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern