Question

In: Computer Science

Design a C program to print out the unsigned char (integer) values of the 4 bytes...

Design a C program to print out the unsigned char (integer) values of the 4 bytes in sequential order (i.e., the first byte is printed first). Using the code framework provided below, the printing is done by the function void byte_value(int *), in which only pointer variables can be declared and used.
#include <stdio.h>
void byte_value(int *);
int main() {
    int n = 1;
    byte_value(&n);
    
    printf("Enter an integer: ");
    if (scanf("%d", &n) == 1)
        byte_value(&n);
    
    return 0;        
}
void byte_value(int *p) {
    // fill in the body using only pointer variables
    // printout the memory address of the byte and its unsigned char (integer) value
    // one line for each pair, a total of 4 lines
    return;
}

Solutions

Expert Solution

The function code & a sample output of running the given program is attached below:

(*Note: Please up-vote. If any doubt, please let me know in the comments)

Function Code:

void byte_value(int *p) {

    // fill in the body using only pointer variables

    // printout the memory address of the byte and its unsigned char (integer) value

    // one line for each pair, a total of 4 lines

    unsigned char* cp = p; //create pointer of type unsigned char and point it to same address as p

    printf("%p \t %u\n",cp,*cp);  //print memory address of first byte & its unsigned char (integer) value

    printf("%p \t %u\n",cp+1,*(cp+1)); //print memory address of second byte & its unsigned char (integer) value

    printf("%p \t %u\n",cp+2,*(cp+2)); //print memory address of third byte & its unsigned char (integer) value

    printf("%p \t %u\n",cp+3,*(cp+3)); //print memory address of fourth byte & its unsigned char (integer) value

    return;

}

Output Screenshot:


Related Solutions

Initialize and Print an Array Write a program that accepts two integer values, called "arraySize" and...
Initialize and Print an Array Write a program that accepts two integer values, called "arraySize" and "multiplier", as user input. Create an array of integers with arraySize elements. Set each array element to the value i*multiplier, where i is the element's index. Next create two functions, called PrintForward() and PrintBackward(), that each accept two parameters: (a) the array to print, (b) the size of the array. The PrintForward() function should print each integer in the array, beginning with index 0....
Lab 5 a) Write a program that reads in an unsigned integer K and sums the...
Lab 5 a) Write a program that reads in an unsigned integer K and sums the first K many integers that are divisible by 7. You should output the sum on a formatted manner b)Consider the following diamond it is an 11 by 11 diamond made with * signs. Write a program that takes as input positive odd integer K (greater than or equal to three and outputs a K by K diamond made with * signs * *** *...
Design a program that will read each line of text from a file, print it out...
Design a program that will read each line of text from a file, print it out to the screen in forward and reverse order and determine if it is a palindrome, ignoring case, spaces, and punctuation. (A palindrome is a phrase that reads the same both forwards and backwards.) Example program run: A Toyota's a Toyota atoyoT a s'atoyoT A This is a palindrome! Hello World dlroW olleH This is NOT a palindrome! Note: You are only designing the program...
Write a C program to Declare an integer array of size 10 with values initialized as...
Write a C program to Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9])...
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
1. Make the flow diagram and the C ++ program that receives three integer values ​​R,...
1. Make the flow diagram and the C ++ program that receives three integer values ​​R, T and Q, determine if they satisfy the expression below, and if so, show the corresponding values ​​of R, T and Q. R'- T '+ 4 * Q? <820 2. Construct a flow chart and the corresponding program in C which, when receiving Y as data, calculates the result of the following function and prints the values ​​of X and Y.
IN C++ PLEASE!!! Design and implement a program (name it Coins) that determines the values of...
IN C++ PLEASE!!! Design and implement a program (name it Coins) that determines the values of coins in a jar. The program prints out the total dollars and cents in the jar. The program prompts the user to enter the number of coins (quarters, dimes, nickels, and pennies). Print out the number of coins entered for each coin type on separate lines followed by the total amount of money in the jar as dollars and cents as shown below.
convert this string type   program into interger data type #include "stdafx.h" #include #include unsigned int putIntoHashTable(char...
convert this string type   program into interger data type #include "stdafx.h" #include #include unsigned int putIntoHashTable(char *ptrInputData, unsigned int bufferLength); // function to add to hash table unsigned int getFromHashTable(char *ptrOutputData, unsigned int bufferLength); // function to retrieve data from hash table #define INPUT_BUFFER_SIZE 200 // local buffer used for adding data to the hash table (there is no reason in this assignment to change this value) #define HASH_SIZE 100 // size of hash table to be used (for testing...
4. Construct a flowchart and the C ++ program that, receiving a four-digit integer as data,...
4. Construct a flowchart and the C ++ program that, receiving a four-digit integer as data, determines whether all the digits of the number are even. For example, if the number were 5688, it would not meet the condition since the most significant digit (5) is odd; if on the contrary, the number were 6244, it would be true, since all the digits are even. 5. Make the flowchart and the C ++ program that, when receiving N integers as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT