Note that there are some explanatory texts on larger screens.

plurals
  1. POprinting memory as 1 byte hex in C
    primarykey
    data
    text
    <p>Could you please help me with the following C problem? I am trying to print a list of 30 successive values, starting at a certain memory address. I would like to print 1 byte as a hex per memory location. As an indication of which memory address to insert, I am printing the address of a dummy at the start of the program. </p> <p>The problem is that I get values of more than 1 byte. For long sequences of zeros this still results in 00, but as soon as non-zero values show up, I get a print of a 4 byte 'window'. This results in the following output:</p> <pre><code>Main function address is 0x8048494 Dummy variable address is 0x9a2e008 Enter the address (without the 0x):9a2e008 You entered: 9a2e008 Address Byte value 0x9a2e008 00 0x9a2e009 00 0x9a2e00a 00 0x9a2e00b 00 0x9a2e00c 00 0x9a2e00d 00 0x9a2e00e 00 0x9a2e00f 00 0x9a2e010 00 0x9a2e011 f1000000 0x9a2e012 ff10000 0x9a2e013 20ff100 0x9a2e014 20ff1 0x9a2e015 3900020f 0x9a2e016 61390002 0x9a2e017 32613900 0x9a2e018 65326139 0x9a2e019 30653261 0x9a2e01a 30306532 0x9a2e01b 38303065 0x9a2e01c 383030 0x9a2e01d 3830 0x9a2e01e 38 0x9a2e01f 00 0x9a2e020 00 0x9a2e021 00 0x9a2e022 00 0x9a2e023 00 0x9a2e024 00 0x9a2e025 00 </code></pre> <p>My code so far is:</p> <pre><code>#include &lt;assert.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define NUMBER_OF_BYTES 10 void showAddresses(void); void printMemoryAtAddress(void); int * dummy; int dumpSize = 30; int main(void) { dummy = (int *) malloc (sizeof(int)); showAddresses(); printMemoryAtAddress(); return 0; } void showAddresses(void) { printf("Main function address is %p \n", main); printf("Dummy variable address is %p \n",(void*)dummy); } void printMemoryAtAddress(void) { int input; printf("Enter the address (without the 0x):"); scanf("%x", &amp;input); printf("You entered: %x \n", input); printf("Address \tByte value \n"); int i; for(i=0;i&lt;dumpSize;i++) { int* address; address = (int*) (input+i); printf("%p \t", address); printf("%.2x \n", *address); } } </code></pre> <p>Any help with this problem would be very much appreciated! If this question is stupid, I apologize. (I am still learning and can't seem to find a solution after hours of trying!) Joe</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload