Lecture 2026-02-05
Recall:
void inc (int *x){
*x++; //wrong,
// because of precedence ( the postfix always takes precedence over prefix ! )
*x = *x + 1;
*x+= 1;
++*x;
//above three implementations work
}In C, post fix takes precedence over prefix.
So *x++ means *(x++)
Address is incremented, a value is fetched from the unincremented adder and thrown away. No change to the original var.
Correct:
int inc (int *x){
(*x)++;
}Now consider:
void swap(struct Postn *p){ //its going to take in a POINTER
int temp = *p.x = *p.y;
*p.x = *p.y;
*p.y = temp;
}Postfix before prefix *p.x means *(p.x)
So, its often a good idea to use parens.
Make sure its clear when to use an arrow and when to use a dot.
Full access through ptr happens a lot - shortcut:
means
(*p).x
More sophisticated user input:
Reads
xas a decimal integer.Skips leading white step.
Why is this wrong?
scanfis a function - can't modifyx
thus, instead:
scanfreturns the number of arguments actually read.A lot of options and variety to modify
scanf—its very complicated.
Advanced Mutation
Mutating structures + lists
In Scheme
can mutate parts of
consof a cons withset-car!set-cdr!
In Racket
cons fields are immutable
cannot be mutated.
For mutable pairs, Racket has
mconsMutating fields with:
mset-car!mset-cdr!
For structs: provide the option #:mutable to allow mutation.
Semantics:
1A -
(posn v1 v2)is a valueNOW-
(posn v1 v2)cannot be a simple value if its mutablehas to be behave more like a BOX
since its a list of elts
How?
is a struct automatically boxed?
or is the struct a box?
SOL: CHECK IN RACKET
A struct is not automatically boxed, but it does box its contents !
So we can rewrite
(pos v1 v2)as:(define _val1 v1)(define _val2 v2)(posn _val1 _val2)
Recall what underscore means ( won't be expanded ).
where
pis(posn _val _val2)find the definition for
_val, fetch thev alue
where
pis(posn _val1 _val2)find
(define _val1 ....)replace with
(define _val1 v)
THINK BOXES!!
generalizes to any mutable
structs,mcons
Consider:
Without underscore variables, this produce
1, but in fact its4.
The two (box 1)s are the same box , can't observe this without the _ vars.
under our new ruels
Note how the shared item is now reflected and explictly shown !
But its not just the box that is shared.
The entire tail is shared since
lst1is shared!Not being duplicated but memory and their associated values are being shared.
This can be seen since using cons is
O(1)
Both occurrences of lst1 refer to the same lst1.
So we need to rethink define:
Thus, it is NOT just a replacement of all x's with 3, like we initially treated in 1A, otherwise we would have gotten 3.
So x is not just a value
Its something we can mutate
an entity we can access
Thus, x must denote a LOCATION and the location contains the value.
So we don't have just one lookup δ : var -> value
Thus we have two lookups:
var -> location
location -> value
Thus, set! changes the lcoation -> map, but not the var->location map (nothign changes that )
This is generally true, but only in the last two weeks we will discuss the complexities in racket and for why its not true.
(define ... ) creates a location, fills it with a value.
In C:
Consider
Outputs: 3 3 3
Why?
yinitialized tox's addressypoints to the location wherexresideszinitalized equal toyTherefore, z is also to
xaddress
*y=2 stores 2 at x's location x==*y==*z==2
*z=3 stores 3 at x's location x==*y ==*z ==3
x,*y,*z
three different names for the same data
Called aliasing
accessing the same data by different names.
Last updated