EMPS intranet

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 48
»  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 *word;

»  printf("Please enter a long word\n");

»  printf("Wow, %s\n\tis a long word!\n", word);

»  return 0;
}
```
Memory

### Allocated memory

128
129
130
131
132
133
134
135
' ' 'Y' ' ' ' ' ' ' ' ' ' ' 'i'
136
137
138
139
140
141
142
143
' ' ' ' ' ' ' ' ' ' ' ' 'G' ' '
144
145
146
147
148
149
150
151
' ' ' ' ' ' ' ' ' ' '@' ' ' 'I'
152
153
154
155
156
157
158
159
' ' ' ' ' ' '+' ' ' ' ' ' ' 'r'
160
161
162
'C' ' ' '\0'

292
293
294
295
output = NULL
296
297
298
299
300
301
302
303
len = -144141781
input = 't' ' ' ' ' ' '
304
305
306
307
308
309
310
311
' ' '\0' '\0' '\0' ' ' '@' ' ' '\t'
312
313
314
315
316
317
318
319
' ' ' ' ' ' ' ' 'i' ' ' ' ' ' '
320
321
322
323
324
325
326
327
' ' '@' ' ' '\t' ' ' '\0' '\0' '\0'
328
329
330
331
332
333
334
335
'K' ' ' ' ' ' ' ' ' '\0' '\0' '\0'
336
337
338
339
340
341
342
343
'\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'
344
345
346
347
' ' ' ' ' ' ' '

### main()

376
377
378
379
word = -4293436223

NB: the actual memory address of each variable is the

The actual address of allocated memory is the

.

Input was:

```supercalifragilisticexpialidocious
```

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 a long word
Wow, supercalifragilisticexpialidocious
is a long word!
```