Skip to content
EMPS intranet

Back to top
Hi!   Hi!           Start main()

Try stepping through the code


  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.

Code

 Header...
// Read in one non-blank line, stripping final spaces // Allocate the resulting character array #define BUFLEN 28
char * readaline(void){
  ...
» char input[BUFLEN], *output=NULL; » int len; » while (1) { » if (fgets(input, BUFLEN, stdin)==NULL) { » fprintf(stderr, "Out of input!!\n"); » exit(99); } // Find the final non-space (if any) » for (len=strlen(input); len>0&&isspace(input(len-1)); --len) { }/// Empty loop: the for() does all the work » inputlen='\0';// Truncate the input » if (input0=='\0') // Blank line continue; » output=malloc(1+len); » if (output==NULL) { » fprintf(stderr, "Out of memory!\n"); » exit(98); } » strncpy(output, input, len+1); » return output; }
} int main(){
  ...
» double mass; » char *desc; » printf("Please enter the mass\n"); » scanf("%lg", &mass); » printf("Now please enter a short description\n"); » desc=readaline(); » printf("Mass: %g description: %s\n", mass, desc); » return 0;
}
Memory

Allocated memory

128
129
130
131
132
133
134
135
' ' ' ' 'f' ' ' 'Y' 'J' ' ' 'b'
136
137
138
139
140
141
142
143
' ' ' ' 'H' ' ' ' ' ' ' '4' ' '
144
145
146
147
148
149
150
151
' ' ':' 'Q' ' ' ' ' ' ' ' ' '\0'

readaline()

280
281
282
283
284
285
286
287
output = NULL
len = -5465484
288
289
290
291
292
293
294
295
input = ' ' '\0' '\0' '\0' ' ' ' ' ' ' '\t'
296
297
298
299
300
301
302
303
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
304
305
306
307
308
309
310
311
' ' ' ' ' ' '\t' ' ' '\0' '\0' '\0'
312
313
314
315
'\0' ' ' ' ' ' '

main()

364
365
366
367
desc = -139305762
368
369
370
371
372
373
374
375
mass = 4.94682842e-270

NB: the actual memory address of each variable is the
address shown plus 4289500992 (0xFFAC9740).

The actual address of allocated memory is the
address shown plus 159736424 (0x9856268).

Show Advanced options

.

Input was:

1.0659E16
Phobos (a moon of Mars)

Show output (Before looking at the output, work out what you think it put should be and see if you are right.)

Output

Please enter the mass
Now please enter a short description
Mass: 1.0659e+16 description: Phobos (a moon of Mars)
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Share
Back to top