The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 340 Weeks Ago, 3 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Question: At line 45, we are printing only one byte of the 32-bit variable, myInt. myInt as defined to be 0x11223344. Why is the last byte shown first?
CODE PROVIDED BELOW
/*
* Module name: debugging_demo_main.c
* Author: The teacher
* Date: August 22, 2017, 4:13 PM
* Description: Demonstration of debugging (there is a bug in the code).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Description: main function
* Receives: Nothing
* Returns: Exit status
* Requires: Nothing
*/
int main(void) {
char *myString = "This is my string!";
char *buffer;
int myInt = 0x11223344;
int strSize;
int value;
// Get the number of characters in the string
strSize = strlen(myString);
printf("The size of myString is %d.n", strSize);
// Allocate memory for buffer, make it a bit larger than needed
buffer = malloc(strlen(myString) + 10);
// Fill the buffer with known values
value = 0x41;
for (int i = 0; i < (strlen(myString) + 10); i++) {
buffer[i] = value++; // Copy value, then increment value
}
// Now copy myString
strncpy(buffer, myString, strSize);
// Print results
printf("Original string: %sn", myString);
printf("Copy of string: %sn", buffer);
// Print the first byte of myInt
printf("The first byte of myInt = %hhxn", myInt);
// Free up the memory we allocated
free(buffer);
return (EXIT_SUCCESS);
}
----------- ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly