LEC 2 Flashcards
*
– evaluate expression to integer
– use integer as address to get value from memory
*(&name)
get address associated with name
get value from memory at that address
assignments are
expressions
space allocated to declarations on ?
on stack
while(expression
while(expression)
statement ->
- evaluate expression
- if non‐zero then
i. execute statement
ii. repeat from 1. - if zero then end iteration
• break in statement ends enclosing iteration
Iteration: for
for(exp1;exp2;exp3) statement -> exp1; while(exp2) { statement exp3; }
- execute statement exp1
- repeatedly test exp2
- each time exp2 is true
- execute statement
- execute statement exp3
• all exps and statement
are optional
Condition: if
if(expression)
statement1
else
statement2 ->
- evaluate expression
- if non‐zero then execute statement1
- if zero then execute statement2
• else statement2 is optional
Condition: switch
switch(expression) { case constant1: statements1 case constant2: statements2 ... default: statementsN }
- evaluate expression to a value
- for first constanti with same value, execute statementsi
- if no constants match expression, evaluate default
statementsN
Function declaration
type name(type1 name1,...,typeN nameN) { declarations statements }
- evaluate expression to a value
- for first constanti with same value, execute statements
- if no constants match expression, evaluate default
statementsN
Function call
name(exp1,…,expN)
- evaluate expifrom left to right
- push expi values onto stack
- execute body, allocating stack space to any
declarations - reclaim stack space for parameters & any
declarations - return result if any
• NB end statement function call with ;
Changing parameters
• parameters passed by value
• value of actual parameter copied to space for
formal parameter
• final value is not copied back from formal to
actual
• so changing the formal does not change the
actual
Pointers
type*name;
• variable name holds address for byte sequence for type • allocates stack space for address but does not create instance of type • must allocate space explicitly NULL • empty pointer
How to actually change parameters?
• to change actual parameter
– pass address (&) of actual to formal
– change indirect (*) on formal
What is FILE?
• type for system representation of file
• all files are treated as text
– i.e. character sequences
FILE * name;
• name is the address of a FILE value