Thread: Storing values in memory

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    Storing values in memory

    I want to write a small C program that reads a number, stores it as a floating point number (both in single precision and double precision formats) and outputs, in hex digits, each of the bytes occupied by that number in memory in each of the two formats.

    Someone has suggested to use a char-pointer targeted on the position of the floating point variables... Anyway this is my code so far:

    Code:
    int main() {
    	float num1;
    	char *ptr;
    	int size;
    
    	printf("Enter: ");
    	scanf("%f", &num1);
    
    	ptr = &num1;
    	size = sizeof(ptr);
    
    	printf("%X\n", ptr);
    	printf("%d\n\n", size);
        
        return(0);
    }
    Btw, the above also outputs the number of bytes allocated to the float in the memory (that's what size is for)... However I don't know whether it is the double-precision or single-precision format.

    When the program is run, whatever I enter, I get 22FF74 as my output (with size 4, which I think is correct)... So is this the correct idea? How do I deal with SP and DP formats?

    Any words of wisdom are appreciated.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    the above also outputs the number of bytes allocated to the float in the memory
    No, size reports the size of ptr, which is a char *. You should do sizeof(num1) if you want the size of your float.

    I get 22FF74 as my output
    You're printing the value of ptr, which is the address of num1. Not the value of the float at all. What they meant was something more like this:
    Code:
    int main(void)
    {
      float num1;
      char *ptr;
      int size;
      int i;
    
      printf("Enter: ");
      fflush(stdout);
      scanf("%f", &num1);
    
      ptr = (char *)&num1;
      size = sizeof(num1);
    
      printf("Float is %d bytes\n", size);
    
      // Loop through each byte of the float by using the ptr
      for(i = 0;i < size;++i)
        printf("%02X ", *ptr++);
      putchar('\n');
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    OK! I got now! Thanks very much for your help.

    How do i go about the single/double precision formats? This I have absolutely no idea about.

    And one last question: What was the purpose of the suggestion of using a char-pointer?

    EDIT: when I enter "2" I get "00 00 00 40". Also when "1" is entered, the output is "00 00 FFFFFF80 3F". Why is that? Thanks.
    Last edited by Opel_Corsa; 09-28-2006 at 01:21 AM.

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Change
    Code:
      char *ptr;
    to
    Code:
      unsigned char *ptr;
    Also add

    Code:
    #include <stdio.h>
    to start of code.

    For single precision use a float, for double precision use a double.


    If you are trying to understand the float/double intrenal layout, see

    http://en.wikipedia.org/wiki/IEEE_fl...point_standard
    Last edited by SKeane; 09-28-2006 at 05:27 AM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    It's working great but what does the following do:
    ptr = (char *)&numf;

    Also why when I changed the char pointer to unsigned it worked fine?
    Last edited by Opel_Corsa; 09-28-2006 at 06:19 PM.

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    &numf = address of variable numf

    (char *) = cast to "pointer to char"

    = means assign value to lvalue, in your case ptr.

    so ptr gets assigned the value of the address of numf cast to a "pointer to char".

    Of course if you change ptr to "unsigned char" you should change the cast to "unsigned char *"

    Code:
    ptr = (unsigned char *)&numf;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. Storing values, internal mechanisms?
    By plan7 in forum C Programming
    Replies: 1
    Last Post: 09-07-2008, 11:31 AM
  3. storing integer values in files using WriteFile
    By Fender Bender in forum Windows Programming
    Replies: 1
    Last Post: 01-15-2006, 12:15 AM
  4. problems storing values in varibles
    By stodd04 in forum C Programming
    Replies: 7
    Last Post: 02-08-2005, 11:56 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM