Value and type of last evaluated expression: Address of array element: Address of array elementx: (none)
Your browser does not support the canvas element
which will make some the features unavailable.
If you are using Internet Explorer within the University of Exeter
try going to the Settings menu (probably the gear shape at the top right
of this page),
selecting "Compatibility View settings",
unchecking "Display intranet sites in Compatibility View"
and reloading this page.
» int choice;
» do{
»printf("\nChoice?\n\n"
"0. Quit\n"
"1. Add new food\n"
"2. Calculate calories\n");
»scanf("%d", &choice);
» switch (choice) {
» case 0:
» break;
» case 1:
»new_food();
» break;
» case 2:
»calculate_calories();
» break;
» default :
»fprintf(stderr, "Input out of range\n");
}
}while (choice) ;
» return 0;
}
void new_food(void){ ...
» Food *new=NULL;
»new=xmalloc(sizeof *new);
»new->next=firstfood;
»firstfood=new;
»printf("Name of food?\n");
»scanf("%15s", new->name);
»printf("Calories per gramme?\n");
»scanf("%f", &new->calories);
}
void calculate_calories(void){ ...
» Food *tmp;
» float calories=0, grammes;
» for (tmp=firstfood; tmp!=NULL; tmp=tmp->next) {
»printf("How many grammes of %s?\n", tmp->name);
»scanf("%f", &grammes);
»calories+=grammes*tmp->calories;
}
»printf("Your food contains %.2f calories\n", calories);
}
Memory
Static variables
128
129
130
131
firstfood = NULL
Allocated memory
264
265
266
267
268
269
270
271
264.name = ' ' 'm' ' ' ' '
' '
' '
' '
' '
272
273
274
275
276
277
278
279
' '
' '
' '
'0'
' '
'%'
' '
'w'
280
281
282
283
284
285
286
287
264.calories = -1.17141343e+24
264.next = -150045226
296
297
298
299
300
301
302
303
296.name = ' ' ' ' ' ' ' '
'c'
'x'
'#'
'W'
304
305
306
307
308
309
310
311
'-'
'1'
' '
' '
'k'
'I'
' '
' '
312
313
314
315
316
317
318
319
296.calories = -5.06744869e-25
296.next = -155950081
calculate_calories()
576
577
578
579
580
581
582
583
tmp = -4286774159
calories = 0
584
585
586
587
grammes = 3.99957958e-34
new_food()
584
585
586
587
new = NULL
new_food()
584
585
586
587
new = NULL
main()
632
633
634
635
choice = 1
NB: the actual memory address of each variable is
the address shown plus 4286774160 (0xFF82FB90).
The actual address of allocated memory is
the address shown plus 160268768 (0x98D81E0).
Show Advanced options
.
Input was:
1
Cheese
4.02
1
Apple
0.52
2
100
60
0
Show output
(Before looking at the output,
work out what you think it put should be and see if you are right.)
Output
Choice?
0. Quit
1. Add new food
2. Calculate calories
Name of food?
Calories per gramme?
Choice?
0. Quit
1. Add new food
2. Calculate calories
Name of food?
Calories per gramme?
Choice?
0. Quit
1. Add new food
2. Calculate calories
How many grammes of Apple?
How many grammes of Cheese?
Your food contains 293.20 calories
Choice?
0. Quit
1. Add new food
2. Calculate calories