CSC161 2011S Imperative Problem Solving
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Reading: K&R 5.1-5.2
Please submit a question by 9 p.m. on Monday, 14 March 2011.
The basics
I am a little lost about when to use * and when to use
&. Is * used for declaring/using and
& used to point to something else?
Pointers are addresses. In declarations, type *name means
. In code, name stores the address of value of type
type& is the address
of
operator: Given a variable, it tells you the address of the
variable. In code, * is the contents of
operator:
Given an address, it gives you the value stored at that address.
I understand what a pointer is but still haven't understood how it
works. How does the * operator exactly work? The example
in K&R didn't help at all.
The * operator finds the contents of something
pointed to.
What is dereferencing?
What does it mean to be dereferenced?
Finding the contents of an address that is pointed to.
If ip points to the integer x, would this statement change x or just what ip points at?
*ip = *ip +10
It changes x.
What is the main purpose of pointers? Are pointers necessary to create a dynamic data structure?
We had been excited all year to learn pointers. So is the purpose of pointers is to just assign stuff to an array or string?
Pointers give you access to the underlying memory. There are many things one can do with that access. A common use is in procedures that modify their arguments, but it's not the only use. They are certainly useful for creating dynamic data structures. (It's okay if you don't know what dynamic data structures are; we'll talk about them after break.)
Swapping
Would we use swap often?
We end up doing a lot of swapping when sorting. A few other algorithms use the same idea.
What I got from the reading is that swap is like renaming, but with the same functions. Is it like that?
I would not say that it's like renaming. You have two variables.
You want to swap what values the two variables store. The swap
function does that for you.
In the swap function that K&R use as an example in part 5.2, what do they mean by copies of a and b
produced by calling swap(a, b); instead of swap(&a, &b);?
As you may recall from our functions lab, when you call a procedure, it makes copies of the parameters. When you modify the copies, the modification does not affect the original.
What is swap(a,b) swapping if
swap(&a, &b) swaps the locations of the a and the b?
swap(&a, &b) swaps the contents of a
and b. swap(a, b) is an invalid call (and
will do really strange things if you try to force C to do it). In
the original version, the call to swap does nothing useful.
Pg. 96, with the swap function rewritten to use pointers, when we now call the function we should be passing pointers not variables, right?
swap (&a, &b)
instead of
swap (a, b)
Yes.
Wouldn't it be better to rewrite the function to take variables and then find the pointers in the function itself? Or does it not make much of a difference?
void
swap (int x, int y)
{
int px = &x;
int py = &y;
int temp;
temp = *px;
*px = *py;
*py = temp;
}
Miscellaneous.
Would any decent implementation of sort have to rely on pointers, or is it enough to produce a copy of an array in sorted form?
Typically, we just rearrange the array. You don't need pointers to do that. But sometimes that makes things better.
Do the * and & operators ever bind to the type declarations? i.e. is *int ip = &x; or something like it ever legal (intended to be equivalent to int *ip = &x;).
No. The operators bind to variables.
I don't understand the statement: double *dp, atof(char *); can we go over this in class?
This statement is effectively two declarations combined in a single statement. One is
double *dp;
The other is
double atof(char *);
The first says
.
The second says dp is a pointer to a double
.
atof is a function that takes a
string as input and returns a double
The reading says that pointers can only point to one data type. Can
you use atoi and those related functions to convert
pointers? Would this effectively change the pointed to item?
Remember, atoi builds a new value. Hence, it should
not change what is being pointed to. It is certainly possible to
make a pointer point to anything. But the type of the pointer will
force the pointed-to thing to be evaluated in a certain way.
How does the * operator work when declaring arrays of strings (2D arrays)?
Arrays of strings are not quite 2D arrays. The * operator
will give you the first string in the array.
Finally we got to the pointers! Could you go over how getint works?
Are we going to use pointers to remake programs we've already worked with, like binsearch, or do pointers work with completely different code types?
Um, neither. We probably won't rewrite the procedures we've written. However, we're going to write similar programs using pointers.
Can you explain the statement The unary operators
?
* and & bind more tightly than arithmetic operators
It means that if you have an expression that uses these operators and
things like addition, the * and & are
evaluated first.
Sometimes I get error messages about things being made into pointers without a cast. Is there actually a way to cast something to a pointer?"
Sure. You use the cast operator - prefix the thing with a pointer
type in parentheses, as in (int *) a. However, it's
generally a bad idea.
The book mentions, Notice that it is essential to pass the address of
. What does this mean,
and where does it do this in the code?
array[n] to getint
K&R mean that you have to pass an address (so that the function can
change its parameter). getint (&array[n]) does this.
Things to follow up on in class.
I think I understand pointers a little better now, but could you go over the "getint" example at the end of 5.2?
Sure.
I think we need a lab on this so I can know how it works.
I would agree. We'll have lots of labs that use pointers.
Questions that I don't understand.
Can you give another program that uses the main
program as a function?
I'm sorry. I don't understand this question.
When we use arrays, do we just call it using *ip?
I'm not sure what you're asking. It
can mean many things.
Try not to use pronouns.
Late questions.
I don't completely understand the code on page 97. My confusion mainly lies within the for. I don't understand what *pn is doing within this.
I feel like i understand the premise of pointers - they represent a different value - but i don't really understand how they differ from a variable. Doesn't int i = 0 point i to zero?
No. It means store 0 in the location associated with
.
i
What would the difference be between:
*ip = i;
vs.
x = i;
There's not a significant difference. Both represent a request to store
the value of i in a memory location. In the latter case,
it is the memory location associated with x. In the former,
it is the memory location stored in ip.
Since i'm having trouble understanding K&R, is there maybe a more C for dummies
source I could read about it? Do you have any suggestions?"
Labs. Tutors.
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue Mar 15 09:27:21 2011.
The source to the document was last modified on Tue Mar 15 09:27:15 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/pointers.html.