Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C program This is a study guide for an exam. Answers along with explanations wou

ID: 3851496 • Letter: C

Question

C program

This is a study guide for an exam. Answers along with explanations would be extremely helpful. Thanks.

Using the code below apply the following assumptions: The address of num1 is 2000, of num2 is 3000, of num3 is 4000 of pr1 is 5000 of pr2 is 6000 of pr3 is 7000.

a. #include <stdio.h>

b. int main (void)

c. {

d.         int num1=123, num2=234, num3=345;

e.         int *pr1, *pr2, **pr3;

f.         pr2 = &num1;

g.         pr1 = &num3;

h.         pr3 = &pr2;

j.         printf(" Value here is: %d", *pr2);

k.         printf(" Value here is: %p", pr3);

m.         printf(" Value here is: %p", *pr3);

n.         printf(" Value here is: %d", **pr3);

o.         printf(" Value here is: %p", &pr1);

p.         *pr3 = &num2;

q.         printf(" Value here is: %d", *pr2);

r.         printf(" Value here is: %p", pr3);

s.         return 0;

t. }

    

List the output for the printf() statements in this program in the order they appear.

Using the code below apply the following assumptions: size of an int is 2 bytes, size of a char is 1 byte, size of a float is 4 bytes. IGNORE COMPILE ERRORS.

a.   #include <stdio.h>

b.   #include <stdlib.h>

c.   int main( void )   {

d.        int nelem = 8, i, index;

e.        char *arrPtr, *itemPtr;

f.        arrPtr = malloc(nelem * sizeof(char));

g.        printf("arrPtr = %p, size of char = %lu ", arrPtr,

h.                   sizeof(char));

g.        for ( i = 0; i < nelem; i++)   {

h.             itemPtr = arrPtr + i;

j              printf("Storing char at memory location = %p (index                         = %i) ", itemPtr, i);

k.             *itemPtr = 'x';

m.        }

n.        printf(“%c”, *itemPtr);

n.        return 0;

p.   }



2. How many bytes of space will be set aside by the malloc() on line f?

3. What will the printf() on line “n” display?

4. T   /    F There is a memory leak in this program.

Explanation / Answer

a. #include <stdio.h>

b. int main (void)

c. {

d.         int num1=123, num2=234, num3=345;

e.         int *pr1, *pr2, **pr3;

f.         pr2 = &num1; // pr2 = 2000

g.         pr1 = &num3; //pr1 = 4000

h.         pr3 = &pr2; // pr3 = 6000

j.         printf(" Value here is: %d", *pr2); // *pr2 = num1 = 123

k.         printf(" Value here is: %p", pr3); // pr3 = 6000

m.         printf(" Value here is: %p", *pr3); // *pr3 = 2000

n.         printf(" Value here is: %d", **pr3); // **pr3 = num1 = 123

o.         printf(" Value here is: %p", &pr1); // 5000

p.         *pr3 = &num2; // pr2 = 3000

q.         printf(" Value here is: %d", *pr2); // 234

r.         printf(" Value here is: %p", pr3); // 6000

s.         return 0;

t. }

Output:

Value here is: 123

Value here is: 6000

Value here is: 2000

Value here is: 123

Value here is: 5000

Value here is: 234

Value here is: 6000

2.

arrPtr = malloc(nelem * sizeof(char));

8 * (1 byte) = 8 bytes

3.

x

*itemPtr = x

4. T, True there is memory leak as free() method is not used to free the allocated memory at the end of the code.