Exercises : Answer the following Question

Submitted by Anonymous (not verified) on Sun, 03/10/2013 - 20:41

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix E, "Answers to Quiz Questions and Exercises."
Quiz

    Provided that the char data type is 1 byte, the int data type is 2 bytes, and the float data type is 4 bytes, how many bytes of memory do the following functions try to allocate?
        malloc(100 * sizeof(int))
        calloc(200, sizeof(char))
        realloc(NULL, 50 * sizeof(float))
        realloc(ptr, 0)
    Given an int pointer, ptr, that is pointing to a block of memory that can hold 100 integers, if you want to reallocate the memory block to hold up to 150 integers, which of the two following statements do you use?
        ptr = realloc(ptr, 50 * sizeof(int));
        ptr = realloc(ptr, 150 * sizeof(int));
    After the following statements are executed successfully, what is the final size of the allocated memory block pointed to by the ptr pointer?

    . . .
    ptr = malloc(300 * sizeof(int));
    . . .
    ptr = realloc(ptr, 500 * sizeof(int));
    . . .
    ptr = realloc(ptr, 60 * sizeof(int));

    What is the final size of the allocated memory block pointed to by the ptr pointer, if the following statements are executed successfully?

    . . .
    ptr = calloc(100 * sizeof(char));
    . . .
    free(ptr);
    ptr = realloc(NULL, 200 * sizeof(char));
    . . .
    ptr = realloc(ptr, 0);

Exercises

    Write a program to ask the user to enter the total number of bytes he or she wants to allocate. Then, initialize the allocated memory with consecutive integers, starting from 1. Add all the integers contained by the memory block and print out the final result on the screen.
    Write a program that allocates a block of memory space to hold 100 items of the float data type by calling the calloc() function. Then, reallocate the block of memory in order to hold 50 more items of the float data type.
    Write a program to ask the user to enter the total number of float data. Then use the calloc() and malloc() functions to allocate two memory blocks with the same size specified by the number, and print out the initial values of the two memory blocks.
    Rewrite the program in Listing 17.4. This time, use the two special cases of the realloc() function to replace the malloc() and free() functions.

 

Related Items

Adding More Expressions into for

Adding More Expressions into for

The C language allows you to put more expressions into the three expression fields in the for statement. Expressions in a single expression field are separated by commas.

The Null Statement

The Null Statement

Looping Under the for Statement

Looping Under the for Statement

The general form of the for statement is

for (expression1; expression2; expression3) {
   statement1;
   statement2;
   .
   .
   .
}

Using Nested Loops

Using Nested Loops

You can put a loop inside another one to make nested loops. The computer will run the inner loop first before it resumes the looping for the outer loop.

Listing 7.7 is an example of how nested loops work.

 

The do-while Loop

The do-while Loop

You may note that in the for and while statements, the expressions are set at the top of the loop. However, in this section, you're going to see another statement used for looping,