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 6.1-6.5, 6.7
Please submit a question by 9 p.m. on Tuesday, 19 April 2011.
When makepoint initializes a structure, does it allocate memory for that structure?
In general, memory is allocated for a structure when the structure is
declared. However, if you want to alllocate memory on the fly, you need
to use the amazing malloc operation which we will discuss
on Friday.
The reading says the only legal operations on a structure are copying it or assigning it to a unit, taking its address with &, and accessing its members
but it shows "p1.x += p2.x" on the next page. I don't understand this because the original statement suggests structures can't be changed but then the example shows the value of a structure being changed.
To K&R, changing the value of a member is just an instance of
accessing a member
.
On page 128, it states that a member of one structure and a member of another structure can have the same name because they're in different structures. Do any processes exist for which it would be problematic to have two members with the same name, even though the members are in different structures?
I can't think of any cases in which there would be a problem. And I can think of some cases in which it would be advantageous. (For example, if you're sure about one field, you can write code that might let you use either one type or the other (but not both).)
On page 135, it says you can write as
#define NKEYS (sizeof keytab / sizeof(struct key))
or
#define NKEYS (sizeof keytab / sizeof keytab [0]).
It goes ahead and says that the second one has the advantage that it does not need to be changed if the type changes, but are there advantages to the first one?
Most of us find the first one clearer.
Is makepoint some x and some y in the cartesian plane?
It is intended to represent some point in the Cartesian plane.
I'm confused about the relationship between a structure and a multi-dimensional array. Are they two different ways of essentially creating the same thing? How do they differ?
Structs differ from arrays in three major ways: (a) arrays are homogenous, structs can be heterogenous; (b) array elements are accessed by number, struct elements are access by name; (c) arrays can be declared in multiple sizes, structs have a fixed size.
Typedef
What is the main purpose of using typedef? What does it mean to parameterize a program against portability problems?
The main purposes of typedef are: (a) clarity, (b) generality, and (c) portability. Clarity comes from being able to write things like
StringComparator comp;
rather than
int (*comp)(char *, char *);
Generality comes from making it easy to change the behavior of the program by changing just the typedef.
Portability comes from the same idea. For example, if we're relying
on integers being 32 bits, we might define a type int32.
Then, we might typedef int32 as int on some
systems, short on others (e.g., where an int is
64 bits and a short is 32), and long on others
(e.g. where an int is 16 bits and a long is 32 bits).
Just to be sure, typedef is for renaming existing
structures/data types only?
More or less. You can use typedef in conjunction with
a data-type definition. We'll cover this in lab.
The string typedef in the book is:
typedef char *String
How does C know that you want String to be a separate token, and not connected to the asterisk?
The same way it knows it for a variable, as in
char *str;
More generally, stars are never parts of names in Scheme.
Is typedef like a let statement in Scheme, but in this case, for C?
No. Let statements bind values to names. Typedefs bind types to names.
Other topics
Why is pointer addition illegal but subtraction fine?
Addition is meaningless. There's no use to adding two addresses.
However, subtraction tells you the distance
in memory between
two locations. That distance can be useful in many ways, particularly
if both addresses are in the same array. For example, if you have the
address of the start of the array and immediately after the end of the
array, you can find the sie of the array.
Questions with annoying responses
Can you go over the main concept that we need to know?
Yes.
What does it mean to canonicalize coordinates of a rectangle?
Context! I need context.
Can you elaborate more on trees? (preferably in class)
No. You should have learned about them in 151 and it's irrelevant to what we're doing right now.
Could we go over some of the programs implemented in the reading? Specifically the "addtree" program from section 6.5
I'll go over similar programs on Friday (and beyond), but not the
details of addtree.
Can we go over self-referential structures? Really confusing.
Friday.
Can you go over keytab function?
Nope. It's not relevant to the bigger picture.
I don't think I have any specific questions. I could just use more explanation on structures in general...
I'll try during the first few minutes of class.
(Clarification) All operations are legal on elements inside the structure, but there are only a few legal operations that can be done to the structure itself?
I can't quite parse this. There are only a few operations that can be done to most values (assign to, get value of, take address of, allocate). So, structs are similar to most values.
Questions Sam has left to answer
How does struct point work?
What is the purpose of structure and its functions?
Wait... I don't understand how the tree on page 139 is read. How are the words read in that particular order?
Can you also just explain pg. 132 the part up until 6.3?
[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 Wed Apr 20 10:46:29 2011.
The source to the document was last modified on Wed Apr 20 10:46:25 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/structures.html.