Thread: convert to integer...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    22

    convert to integer...

    Hi friends.
    this is my code
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
            char buf[4]={'5','a','4','b'};
            int i;
            i=(int)buf;
            printf("%d\n",sizeof(int));
            printf("%d\n",sizeof(buf));
            printf("%c",buf[0]);
            printf("%c",buf[1]);
            printf("%c",buf[2]);
            printf("%c\n\n",buf[3]);
            printf("%d",i);
            return 0;
    
    }
    what i wanted to know is ...when there is an array of 4 charecters...can i take that to an integer..(where int is 4 bytes). say the array is '5' 'a' '4' 'b' and when i take this array to a single integer of 4 bytes i want the exact "5a4b" in the integer...is this possible anyway...just a thing struck my mind....

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Many machines have alignment requirements for various types - for example, allowable int addresses may only be one every 4 bytes (assuming an int is 4 bytes). In this case, if you try to cast a char pointer to an int pointer and dereference, you will often get a segfault, since you only have a 1 in 4 chance of having a valid int address. To avoid this, you can instead define an array of ints, and then cast the int pointers to char pointers. I don't know if just respecting this makes things portable though.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    wow... thanks vart i wonder how u guys got these tricks and knowledge in C...
    thanks a lot for that link...
    Quote Originally Posted by robatino
    since you only have a 1 in 4 chance of having a valid int address.
    sorry robatino i didnt get what u are telling....

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Well, I don't know about the probability, but if the alignment restriction holds, only one out of every 4 possible char addresses is a valid int address (if sizeof(int) == 4). Typically it would be just those addresses that are divisible by 4.
    For example, on my machine the output of
    Code:
    #include <iostream>
    
    int main() {
      double d;
      std::cout << "double d address is " << (void *)&d << "\n";
      float f;
      std::cout << "float f address is " << (void *)&f << "\n";
      char c;
      std::cout << "char c address is " << (void *)&c << "\n";
    }
    is

    double d address is 0xbff79798
    float f address is 0xbff79794
    char c address is 0xbff79793

    (I remember reading recently that one should cast pointers to void * to print them, and the char pointer printed as garbage until I did this.) Notice that the addresses of d, f, and c are divisible by 8, 4, and 1, corresponding to the sizes.

    Edit: Sorry for the code, I keep forgetting when I post to the C board instead of the C++ board. I don't remember offhand how to print a pointer using printf() but you should be able to look it up.
    Last edited by robatino; 01-22-2007 at 04:15 AM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    22
    oh.. thanks robatino...i got it...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  2. Convert integer to char..
    By sniper83 in forum C Programming
    Replies: 7
    Last Post: 07-18-2007, 01:23 PM
  3. convert from an integer to an unsigned char
    By Landroid in forum C Programming
    Replies: 4
    Last Post: 05-02-2005, 01:43 AM
  4. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM