Answers/Solutions to Exercises in Chapter 4, Exercise 4

E4: What is wrong with the following program?
    #include <stdio.h>
    int main()
    {
        char* p;

        p = realloc(p,100);
        strcpy(p,"hello");

        p = realloc(p,200);
        strcat(p," and good bye");
        return 0;
    }

A4: p is not initialized, so when first used in realloc(), all problems discussed in the book concerning an uninitialized pointer can occur. More importantly, realloc() can be compromised and/or confused.

Back to Answers/Solutions Index                          Back to Answers/Solutions for Chapter 4 Index