Thread: Pointer Help

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    22

    Pointer Help

    I am trying to POINT to each byte in an array and pass that info along to the main, thus resulting in each byte being looped. I am getting the correct out put but the index where I need it, is not correct . I am new to C so please forgive me if I am missing something simple.

    output should look like :

    00 00 00 00 0 0
    01 00 00 00 1 1 This would be @ index c_ptr[0]

    at c_ptr[1] I would get

    00 01 00 00 256 256
    00 02 00 00 512 512

    and so on , here is what I coded :

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    void print_hex_signed_unsigned(int i)
    {
      unsigned char* c_ptr = (char*)&i;
      printf( "The Value is:   %02X 00 00 00        %14d     %14u\n", c_ptr[0],i,i);
      printf( "The Value is:    00 %02X 00 00       %14d     %14u\n", c_ptr[1],i,i);
      printf( "The Value is:    00 00 %02X 00       %14d     %14u\n", c_ptr[2],i,i);
      printf( "The Value is:    00 00 00  %02X      %14d     %14u\n", c_ptr[3],i,i);
      
      
    }
    int main ()
    
    {
         int i;
        int index;
        
        
        for (index = 0; index <256; index++)
            print_hex_signed_unsigned(index * 1);
            
        for (index = 0; index <256; index++)
            print_hex_signed_unsigned(index * 256);
            
        for (index = 0; index <256; index++)
            print_hex_signed_unsigned(index * 65536);
            
        for (index = 0; index <256; index++)
            print_hex_signed_unsigned(index * 16777216);
        
            
        getch();
        return(0);
        
        
    }
    Thank you everyone for all the help !!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How does what you get differ from what you want?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    22
    The indexing is off on it . might have something with my printf formatting ?
    I am getting all the data lined up in my first row

    01 00 00 00 where this would be 1
    01 00 00 00 and this is really 256

    but it's all in row 1 , not letting me know that I was actually looping through the second bit

    should be ordered 00 01 00 00 on the second bit

    00 00 01 00 - third bit
    00 00 00 01 - fourth bit

    I hope this clears it up a bit ? I am very sorry if it does not , hard to express in words for someone new to C like myself. Thank you for the help though !

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So here's what you've got: you pass in 0 to your function, it prints four lines:
    The Value is: 00 00 00 00 0 0
    The Value is: 00 00 00 00 0 0
    The Value is: 00 00 00 00 0 0
    The Value is: 00 00 00 00 0 0
    You pass 1 into your function, it prints four lines:
    The Value is: 01 00 00 00 1 1
    The Value is: 00 00 00 00 1 1
    The Value is: 00 00 00 00 1 1
    The Value is: 00 00 00 00 1 1
    and then 2, and then 3, and then 4, and then ....... and then 255, and then 0, and then 256, and then 512, and then ............. Did you want 0,0,0,0 then 1,256,65536,16M, etc? Then you need to have (a) your function only print one line and (b) one for loop that calls the function four times.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM