Concept explainers
Explanation of Solution
Corresponding modified code for “show_bytes”:
#include <stdio.h>
//Define variable "byte_pointer" in char datatype.
typedef unsigned char* byte_pointer;
//Function definition for show_bytes.
void show_bytes(byte_pointer start, size_t len)
{
//Declare variable "i" in int data type.
int i;
/* "For" loop to determine byte representation in hexadecimal */
for (i = 0; i < len; i++)
//Display each bytes in "2" digits hexadecimal value.
printf(" %.2x", start[i]);
printf("\n");
}
//Function to determine byte for "int" data type value.
void show_int(int x)
{
//Call show_bytes function with integer value.
show_bytes((byte_pointer) &x, sizeof(int));
}
//Function to determine byte for "float" data type value.
void show_float(float x)
{
//Call show_bytes function float value.
show_bytes((byte_pointer) &x, sizeof(float));
}
//Function to determine byte for "pointer" value.
void show_pointer(void *x)
{
//Call show_bytes function with pointer value.
show_bytes((byte_pointer) &x, sizeof(void *));
}
//Function to determine byte for "short" data type value.
void show_short(short x)
{
//Call show_bytes function with short int value.
show_bytes((byte_pointer) &x, sizeof(short));
}
//Function to determine byte for "long" data type value.
void show_long(long x)
{
//Call show_bytes function with long value.
show_bytes((byte_pointer) &x, sizeof(long));
}
//Function to determine byte for "double" data type value.
void show_double(double x)
{
//Call show_bytes function with double value.
show_bytes((byte_pointer) &x, sizeof(double));
}
//Test all show bytes.
void test_show_bytes(int val)
{
//Define variables.
int ival = val;
float fval = (float) ival;
int *pval = &ival;
//Call function.
show_int(ival);
show_float(fval);
show_pointer(pval);
//Define variables.
short sval = (short) ival;
long lval = (long) ival;
double dval = (double) ival;
//Call function.
show_short(sval);
show_long(lval);
show_double(dval);
}
//Main function.
int main(int argc, char* argv[])
{
//Define the sample value.
int sampleValue = 1024;
//Call test_show_bytes function.
test_show_bytes(sampleValue);
return 0;
}
The given program is used to display the byte representation of different program objects by using casting...
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
EBK COMPUTER SYSTEMS
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY