Thread: type cast or conversion?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    type cast or conversion?

    I want to read 4 char from a file and then store them at an array so i do:

    fread (&i_r_quan, sizeof (char), 4, f_ptr);

    After checking these characters I need to store them in a structure as an integer.
    Does the following work by type conversion or do I have to type cast i_r_quan to an integer first:

    strcpy (i_r_quantity, i_r_quan [4]);

    Also will a null be added to i_r_quan automatically when I read 4 characters into the array?:

    CHEERS!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    union conversion{
        char array[4];
        int i;
    };
    Try that on for size.

    Quzah.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Does the following work by type conversion or do I have to type cast i_r_quan to an integer first:
    No, you need to use a routine like atoi or sscanf

    If i_r_quantity is an int, then
    sscanf( i_r_quan, "%d", &i_r_quantity );

    > Also will a null be added to i_r_quan automatically when I read 4 characters into the array
    No - you need to do this bit yourself as well

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. type conversion
    By peterx in forum C Programming
    Replies: 3
    Last Post: 10-08-2005, 04:03 PM
  2. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  3. Type conversion
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 12-08-2003, 05:51 PM
  4. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM