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 a long word, allocate the resulting character array // Later we will encounter a more useful version of this function #define BUFLEN 20
char * readaword(void){
  ...
» char input[BUFLEN], *output=NULL; » int len; » if (scanf("%s", input)!=1) { » fprintf(stderr, "Out of input!!\n"); » exit(99); } // Now allocate the final string and copy the input to it » len=1+strlen(input);// +1 for closing '\0' » output=malloc(len);// NB: sizeof *output == 1 » if (output==NULL) { » fprintf(stderr, "Out of memory!\n"); » exit(98); } » strncpy(output, input, len); » return output;
} int main(){
  ...
» char *name[2]={NULL,NULL}; » for ( int i=0;i<2; ++i) { » printf("Player %d please enter your first name\n", i+1); » namei=readaword(); } » printf("Welcome %s and %s.\n", name0, name1); » return 0;
}
Memory

Allocated memory

128
129
130
131
132
133
'_' ' ' '_' ' ' ':' '\0'

144
145
146
147
148
' ' ' ' ' ' ' ' '\0'

readaword()

280
281
282
283
284
285
286
287
output = NULL
len = 43
288
289
290
291
292
293
294
295
input = ' ' '\0' '\0' '\0' '\0' '\0' '\0' '\0'
296
297
298
299
300
301
302
303
' ' '\0' '\0' '\0' '0' 'p' ' ' ' '
304
305
306
307
'G' ' ' ' ' ' '

readaword()

280
281
282
283
284
285
286
287
output = NULL
len = 43
288
289
290
291
292
293
294
295
input = ' ' '\0' '\0' '\0' ' ' '\0' '\0' '\0'
296
297
298
299
300
301
302
303
' ' '\0' '\0' '\0' '0' 'p' ' ' ' '
304
305
306
307
'G' ' ' ' ' ' '

main(): for()

344
345
346
347
i = 0

main()

348
349
350
351
name[0] =
352
353
354
355
name[1] =

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

The actual address of allocated memory is the
address shown plus 139555432 (0x8517268).

Show Advanced options

.

Input was:

Alice
Dave

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

Output

Player 1 please enter your first name
Player 2 please enter your first name
Welcome Alice and Dave.
                                                                                                                                                                                                                                                                       

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