Please comment this code. typedef unsigned char BYTE; int main(void) { unsigned
ID: 3721714 • Letter: P
Question
Please comment this code.
typedef unsigned char BYTE;
int main(void)
{
unsigned int addr;
int i,n;
BYTE *ptr;
printf("Address of main function: %x ",(unsigned int) main);
printf("Address of add variable: %x ",(unsigned int) &addr);
printf(" Enter a (hex) address: ");
scanf("%x",&addr);
printf("Enter number of bytes to view: ");
scanf("%d",&n);
printf(" ");
printf(" Address Bytes Characters ");
printf(" ------- ------------------------------ ---------- ");
ptr=(BYTE *)addr;
for(;n>0;n-=10){
printf("%8X ",(unsigned int)ptr);
for(i=0;i<10&&i<n;i++)
printf("%.2X ",*(ptr+i));
for(;i<10;i++)
printf(" ");
for(i=0;i<10&&i<n;i++){
BYTE ch=*(ptr+i);
if(!isprint(ch));
ch='.';
}
printf(" ");
ptr+=10;
}
return 0;
}
Explanation / Answer
here is the commented code..Please note i do not know what the program's objective is..I have commented each and every line as written.
=============================================================
Program:
==============================================================
#include<stdio.h>
//here we hava typecased the name of unsigned char as BYTE
typedef unsigned char BYTE;
//start of main function
int main(void)
{
//variables
unsigned int addr;
int i,n;
//this a BYTE pointer
BYTE *ptr;
//print the address of main and variables
printf("Address of main function: %x ",(unsigned int) main);
printf("Address of add variable: %x ",(unsigned int) &addr);
//accept a hex address from the user
printf(" Enter a (hex) address: ");
scanf("%x",&addr);
//ask to user to input the number of bytes to view
printf("Enter number of bytes to view: ");
scanf("%d",&n);
//display a new line
printf(" ");
//display the heading
printf(" Address Bytes Characters ");
printf(" ------- ------------------------------ ---------- ");
//here we have typecased the addr int BYTE pointer
ptr=(BYTE *)addr;
for(;n>0;n-=10){
//display the octal value of ptr pointer
printf("%8X ",(unsigned int)ptr);
//loop for 10 times
for(i=0;i<10&&i<n;i++)
//display the value of *(ptr+i)
printf("%.2X ",*(ptr+i));
//loop for 10 times that display 10 spaces
for(;i<10;i++)
printf(" ");
//loop for 10 times
for(i=0;i<10&&i<n;i++){
//assign ch value of *(ptr+i)
BYTE ch=*(ptr+i);
//if the statement is true assign ch a value '.'
if(!isprint(ch));
ch='.';
}
//print a new line
printf(" ");
//incrment pointer by 10
ptr+=10;
}
return 0;
}
===============================================================
Kindly Check and Verify Thanks..!!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.