Jump to content

What am I missing?!

- - - - -

  • Please log in to reply
3 replies to this topic

#1
jcampos8782

jcampos8782

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
So this program is supposed to print out the byte patter for an integer argument in 3 different ways. It is pretty straightforward code, but for some reason I cannot get the float to show anything other than 0x 00 00 00 00

Am I missing something?


/* rtsb.c */


void run_show_bytes(int val)

{

	int ival = val;

	float fval = (float) ival;

	int *pval = &ival;

	


	printf("%f\n\n", fval); // THIS PRINTS OUT A FLOAT VALUE

	show_int(ival);

	show_float(fval);

	show_pointer(pval);

}


/* show_bytes.c */



#include <stdio.h>


typedef unsigned char *byte_pointer;


void show_bytes (byte_pointer start, int len) 

{

	int i;

	for (i = 0; i < len; i++)

	{

		printf(" %.2x", start[i]);

	}

	printf("\n");

}


void show_int(int x)

{

	show_bytes((byte_pointer) &x, sizeof(int)); // WORKS

}


void show_float(float x)

{

	show_bytes((byte_pointer) &x, sizeof(float)); // prints 0x 00 00 00 00 

}


void show_pointer(void *x)

{

	show_bytes((byte_pointer) &x, sizeof(void *)); // WORKS

}



#2
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts
Look at this link: C++ — Convert int to string « Not So Frequently Asked Questions

Have you thought about writing the value out as a string and seeing if that makes a difference?
-CDG10620
Software Developer

#3
quantus

quantus

    Newbie

  • Members
  • Pip
  • 7 posts
Haven't tested it...but some ideas:

- start[i] is int but you try to access it as float???
- Does it work with double? It may be as strange as double working on...64 bit os???

what the poster above me has said is another option ^

#4
jcampos8782

jcampos8782

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Thanks for the replies.

@cdg: Not trying to change it to a string. The assignment was to read the individual bytes in hex format so that we could see the differences in byte patterns.
@quantus: start[i] is not an int, it is a byte_pointer (unsigned char).

Turns out the above code works fine... I just linked a couple files wrong :(




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users