trouble casting char to double

This is a discussion on trouble casting char to double within the C Programming forums, part of the General Programming Boards category; Hi I have the following code: Code: i = 1; while (fscanf(infile, "%[^:]:%[^:]:%s\n", line1,line2,line3) != EOF) { printf("Line was composed ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    trouble casting char to double

    Hi I have the following code:
    Code:
    i = 1;
    while (fscanf(infile, "%[^:]:%[^:]:%s\n", line1,line2,line3) != EOF) {
            printf("Line was composed of %s, %s, %s\n",line1,line2,line3);
            for(j = 1 ; j <= NumInput ; j++){
                    printf("char  :i=%d,j=%d and value=%c\n",i,j,line2[j-1]);
                    printf("float :i=%d,j=%d and value=%f\n",i,j,line2[j-1]);
                    printf("int   :i=%d,j=%d and value=%d\n",i,j,line2[j-1]-48);
                    printf("float :i=%d,j=%d and value=%f\n",i,j,(double)line2[j-1]-48);
            /*      printf("atof  :i=%d,j=%d and value=%f\n",i,j,atof(line2[j-1]));   */
                    printf("------\n");
                    exit(1);
            }
            i++;
        }
    (The infile has lines like: C0001:1001:1(string:0s and 1s:0 or 1))
    The output is:
    Code:
    Line was composed of C003, 1100, 1
    char   :i=1,j=1 and value=1
    float  :i=1,j=1 and value=-0.523882
    int    :i=1,j=1 and value=1
    float  :i=1,j=1 and value=1.000000
    ------
    I want to be able to cast the line2[j] to a float, since it appears to be a char. But if I uncomment the atof line i get the following error:
    Code:
    cc pnn.c -O -lm -o pnn
    pnn.c: In function ‘main’:
    pnn.c:74: warning: passing argument 1 of ‘atof’ makes pointer from integer without a cast
    Is the (double)line2[2]-48 the best way to do what I want to do?

    -deprekate

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Code:
    line2[j]
    is not a pointer; the subscript dereferences the pointer. To get a pointer, you can use
    Code:
    &line2[j]
    
    or
    
    line2+j

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "makes pointer from integer" is probably because line2[x] is a single char, and atof expects a char *pointer. You could try &line2[x], but you will likely have a problem with that because the "string" pointed to is not null terminated, methinks.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by MK27 View Post
    but you will likely have a problem with that because the "string" pointed to is not null terminated, methinks.
    since the last char of line2[] definitely has a NULL terminator, it doesn't matter where in the array you start reading the string.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Quote Originally Posted by drunken_scot View Post
    Code:
    line2[j]
    is not a pointer; the subscript dereferences the pointer. To get a pointer, you can use
    Code:
    &line2[j]
    
    or
    
    line2+j
    I tried both ways:
    Code:
                    printf("char  :i=%d,j=%d and value=%c\n",i,j,line2[j-1]);
                    printf("float :i=%d,j=%d and value=%f\n",i,j,line2[j-1]);
                    printf("int   :i=%d,j=%d and value=%d\n",i,j,line2[j-1]-48);
                    printf("float :i=%d,j=%d and value=%f\n",i,j,(double)line2[j-1]-48);
                    printf("atof  :i=%d,j=%d and value=%f\n",i,j,atof(&line2[j-1]));
                    printf("atof  :i=%d,j=%d and value=%f\n",i,j,atof(line2+(j-1)));
                    printf("------\n");
    ...
    Neither of those ways work.
    Code:
    Line was composed of 003, 0111, 1
    char  :i=1,j=1 and value=0
    float :i=1,j=1 and value=-0.062139
    int   :i=1,j=1 and value=0
    float :i=1,j=1 and value=0.000000
    atof  :i=1,j=1 and value=111.000000
    atof  :i=1,j=1 and value=111.000000
    ------
    I guess my (double)line2[j-1]-48 workaround is the best/and only way to do it?
    (the correct value should be the first character of the 2nd term of "Line composed of ____")
    Last edited by deprekate; 03-02-2009 at 02:11 PM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Excuse me, but I do not understand what you are actually trying to achieve.

    If you have this input:
    Code:
    C0001:1001:1
    and you want to translate that to a floating point value, yes?

    What sort of data is the original data, if I may ask?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Quote Originally Posted by matsp View Post
    Excuse me, but I do not understand what you are actually trying to achieve.

    If you have this input:
    Code:
    C0001:1001:1
    and you want to translate that to a floating point value, yes?
    No I want to take the second term(1001) of that line(: are delimiters), and then pull out each character of that term("1","0","0","1") each into its own variable.

    So I want to cast a single char(which happens to also be an underlying integer) to a float(using atof() presumably)", however I guess that atof() only accepts a pointer to a char and not a character/integer.

    The printf's are just there to illustrate, I really end up setting an array cell to the value of that character/integer.

    Quote Originally Posted by matsp View Post
    What sort of data is the original data, if I may ask?
    Mats
    The original data is a simple text file with lines like: C0001:1001:1

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by deprekate View Post
    No I want to take the second term(1001) of that line(: are delimiters), and then pull out each character of that term("1","0","0","1") each into its own variable.

    So I want to cast a single char(which happens to also be an underlying integer) to a float(using atof() presumably)", however I guess that atof() only accepts a pointer to a char and not a character/integer.

    The printf's are just there to illustrate, I really end up setting an array cell to the value of that character/integer.


    The original data is a simple text file with lines like: C0001:1001:1
    Why a float?

    Anyway, if you have a character that you know is numeric, then subtracting '0' must needs give you the numeric value of that character. If you want it to be a float, you can assign it to a float, just like you can say float foo = 4.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so lets put this a different way: If we have the string C0001:1001:1, and you want to extract the part that says 1001, what value do you want to get from it? 9, 1001 or 1001.0f?

    I agree with tabstop - there is something not completely understood here. I'm not sure where the lack of understanding is, but it certainly doesn't seem sensible to use float casts where they are at the moment.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    also what about

    ffe0 - is this string possible?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Quote Originally Posted by matsp View Post
    Right, so lets put this a different way: If we have the string C0001:1001:1, and you want to extract the part that says 1001, what value do you want to get from it? 9, 1001 or 1001.0f?

    I agree with tabstop - there is something not completely understood here. I'm not sure where the lack of understanding is, but it certainly doesn't seem sensible to use float casts where they are at the moment.

    --
    Mats
    So I take the string C001:1001:1 and I want to get teh 1001 part. and then I want to get each of the characters of 1001. So "1","0","0","1". and I will be putting these into a double array. arr[1]=1,arr[2]=0;arr[3]=0;arr[4]=1.

    This is simplified since I don't know the length that the second term(1001) will be: it could be (0101011,...)

    I need to use float casts because downsteam in the code it will be required that they are float.

    Quote Originally Posted by vart View Post
    also what about

    ffe0 - is this string possible?
    No the second term will always be in the form of binary:so only 0's and 1's
    Last edited by deprekate; 03-03-2009 at 10:47 AM. Reason: more info

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you need a loop that iterates over the substring, and extracts each digit using the line2[j]-'0' form. Casting to float is probably not necessary, as C allows conversion from int to float and float to int without casts.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Quote Originally Posted by matsp View Post
    So, you need a loop that iterates over the substring, and extracts each digit using the line2[j]-'0' form. Casting to float is probably not necessary, as C allows conversion from int to float and float to int without casts.

    --
    Mats
    Yes although I believe I tried the -'0' method and it did not work. I had to subtract 48: hence the
    Code:
    (double)line2[j]-48

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    Quote Originally Posted by deprekate
    Yes although I believe I tried the -'0' method and it did not work. I had to subtract 48
    Since '0' and 48 have the same value (and type!) where ASCII is concerned, that should not be the case.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Since '0' and 48 have the same value (and type!) where ASCII is concerned, that should not be the case.
    Assuming of course the compiler and the input text are both using ASCII or a character set derived from ASCII - but that is highly likely, so I don't really see how it can go wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  3. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 08:38 PM
  4. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21