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

Problem 2: (8 pts in lab 4 score): Given the C code on page 3, Note: \"%p\" is u

ID: 3675690 • Letter: P

Question

Problem 2: (8 pts in lab 4 score): Given the C code on page 3,

Note:

"%p" is used to print values in hexadecimal notation (base 16) – the value will be displayed using digits from 0-9, A, B, C, D, E, and F, i.e. 00F3F89C. Typically, "%p" is used to display the (memory) address.

The function sizeof( ) yields the size of its operand with respect to the size of type char. http://msdn.microsoft.com/en-us/library/4s7x1k91.aspx

Run the code, observe the answers from the code and fill in the blanks on the next page

Write your answers down so that TA can grade your answers

1) From section A in the code,

What do you observe from the value of a pointer (e.g. pch_x) and the address of the corresponding variable (i.e. ch_x) – value of &ch_x? ______________________

Q1) What is & in front of the variable for? ___________________

2) From section B in the code,

*pch_x = ____________      ch_x = ____________

*pi_x = ____________           i_x =____________

*pf_x = ____________          f_x = ____________

*pd_x = ____________          d_x = ____________

Q2) What is * in front of a pointer for? ___________________

3) From section C in the code,

size of char-type data is ____________ bytes

A

size of integer-type data is ____________ bytes

size of float-type data is ____________ bytes

size of double-type data is ____________ bytes

size of a pointer that points to char-type data is __________ bytes

size of a pointer that points to int-type data is __________ bytes

size of a pointer that points to float-type data is __________ bytes

size of a pointer that points to double-type data is __________ bytes

Q3) From the above, the size of a pointer is always ____________ bytes regardless of the data type that the pointer points to.

4) From section D in the code,

size of array containing 10 char-type elements is ___________ bytes

size of array containing 10 int-type elements is ___________ bytes

A

size of array containing 10 double-type elements is ___________ bytes

Q4) How does the size of array with 10 elements relate to the size of one scalar variable in          

with the same data type? For example, how does the size of ix_array relates to size of i_x?

#include <stdio.h>

int main(void)

{

       char *pch_x, ch_x = 'A';

       int *pi_x, i_x = 175;

       float *pf_x, f_x = 1.2345;

       double *pd_x, d_x = 100.678;

       char chx_array[10];

       int ix_array[10];

       double dx_array[10];

      

       pch_x = &ch_x;

       pi_x = &i_x;

       pf_x = &f_x;

       pd_x = &d_x;

       //Section A of Problem 1 in the lab 4 handout

       printf("***--- section A --- *** ");

       printf("memory address where ch_x is stored is %p, %p ", pch_x, &ch_x);

       printf("memory address where i_x is stored is %p, %p ", pi_x, &i_x);

       printf("memory address where f_x is stored is %p, %p ", pf_x, &f_x);

       printf("memory address where d_x is stored is %p, %p ", pd_x, &d_x);

       printf(" ");

       //Section B of Problem 1 in the lab 4 handout

       printf("***--- section B --- *** ");

       printf("*pch_x = %c ch_x = %c ", *pch_x, ch_x);

       printf("*pi_x = %d i_x = %d ", *pi_x, i_x);

       printf("*pf_x = %.5f f_x = %.5f ", *pf_x, f_x);

       printf("*pd_x = %.5lf d_x = %.5lf ", *pd_x, d_x);

       printf(" ");

       //Section C of Problem 1 in the lab 4 handout

       printf("***--- section C --- *** ");

       printf("size of char-type data is %d bytes ", sizeof(ch_x));

       printf("size of integer-type data is %d bytes ", sizeof(i_x));

       printf("size of float-type data is %d bytes ", sizeof(f_x));

       printf("size of double-type data is %d bytes ", sizeof(d_x));

       printf(" ");

       printf("size of a pointer that points to char-type data is %d bytes ", sizeof(pch_x));

       printf("size of a pointer that points to int-type data is %d bytes ", sizeof(pi_x));

       printf("size of a pointer that points to float-type data is %d bytes ", sizeof(pf_x));

       printf("size of a pointer that points to double-type data is %d bytes ", sizeof(pd_x));

       printf(" ");

       //Section D of Problem 1 in the lab 4 handout

       printf("***--- section D --- *** ");

       printf("size of array containing 10 char-type elements is %d bytes ",sizeof(chx_array));

       printf("size of array containing 10 int-type elements is %d bytes ", sizeof(ix_array));

       printf("size of array containing 10 double-type elements is %d bytes ", sizeof(dx_array));

       return 0;

}

A

Explanation / Answer

I am answering the question by filling the gap(bold) after running the given code;

1) From section A in the code,

What do you observe from the value of a pointer (e.g. pch_x) and the address of the corresponding variable (i.e. ch_x) – value of &ch_x? 0x7ffc5fcb757f, 0x7ffc5fcb757f

Q1) What is & in front of the variable for? & is passing by reference value. & is the reference operator, and read as addressof the variable

2) From section B in the code,

*pch_x = _____A_______      ch_x = ___A_________

*pi_x = __175__________           i_x =_____175_______

*pf_x = __ 1.23450__________          f_x = _ 1.23450___________

*pd_x = 100.67800         d_x = ____ 100.67800 ________

Q2) What is * in front of a pointer for?

ANS: This show that it's a pointer. * on a pointer and it returns the value stored in the address kept in the pointer variable.

3) From section C in the code,

size of char-type data is ___1_________ bytes

size of integer-type data is ___4_________ bytes

size of float-type data is ______4______ bytes

size of double-type data is _______8_____ bytes

size of a pointer that points to char-type data is ___8_______ bytes

size of a pointer that points to int-type data is _______8___ bytes

size of a pointer that points to float-type data is ____8______ bytes

size of a pointer that points to double-type data is __8________ bytes

Q3) From the above, the size of a pointer is always _______4_____ bytes regardless of the data type that the pointer points to.

4) From section D in the code,

size of array containing 10 char-type elements is ____10_______ bytes

size of array containing 10 int-type elements is ____40_______ bytes

size of array containing 10 double-type elements is ______80_____ bytes

Q4) How does the size of array with 10 elements relate to the size of one scalar variable in          

with the same data type? For example, how does the size of ix_array relates to size of i_x?

ANS: The size of ix_array is 4 TIMES OF THE size of i_x. HENCE the size of array with 10 elements relate to FOUR TIMES of the size of one scalar variable in          

with the same data type. Example when we rumn the code of section a and d,we get:

size of integer-type data is 4 bytes.

size of array containing 10 int-type elements is 40 bytes

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote