Wrong cast (atof)

This is a discussion on Wrong cast (atof) within the C Programming forums, part of the General Programming Boards category; Hi i'm here again I have this code : Code: DWORD aProcesses[1024], cbNeeded, cProcesses; unsigned int i; ... ... printf( ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    Wrong cast (atof)

    Hi i'm here again

    I have this code :

    Code:
    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;
    ...
    ...
    printf( "%s  (PID: %u)\n", szProcessName, aProcesses[i] );
    ShellExecute( NULL, "open", "pausep.exe", aProcesses[i], NULL, SW_HIDE );
    ...
    ...
    the problem is that shellexecute wants as 4th argument a pointer to a null-terminated string that specifies the parameters to be passed to the application... if I leave the code like it is, the program crushes..

    so i thought to use atof for a cast but it seems i'm not able to use it in the right way...

    the new code is:

    Code:
    char parametro[MAX_PATH];
    ..
    ..
    printf( "%s  (PID: %u)\n", szProcessName, aProcesses[i] );
    parametro = (char)atof(aProcesses[i]);
    ShellExecute( NULL, "open", "pausep.exe", parametro, NULL, SW_HIDE );

    61 \main.c [Warning] passing arg 1 of `atof' makes pointer from integer without a cast

    ..integer ? where the hell is the integer ?? mh..
    any suggestion ?

    thanx for your time guys
    This forum is the best one I've ever seen. Great ppl, great coders

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    parametro = (char)atof(aProcesses[i]);
    Look at the man page for atof, and you'll see that it expects you to pass it a pointer to a character. You're passing it an integer, or rather, a DWORD.

    On an aside, what the hell are you doing to its return value? Casting a float to a char? Why? What's the point of using atof if you only want a char from whatever it is you think you're trying to do.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What you think you are doing witih atof:
    converting a float to a string
    What you are really doing:
    Converting a string to a float. Read the function name: alphanumeric (a) to float (f)

    Why did you think to use atof when you want to convert a DWORD?

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thanx for your help

    Ah! so a DWORD is quite an int.. I thought it was a char

    Quote Originally Posted by quzah
    On an aside, what the hell are you doing to its return value? Casting a float to a char? Why? What's the point of using atof if you only want a char from whatever it is you think you're trying to do.
    what I want to do is to use the aProcesses[i] (that is a PID..a process ID) as parameter for pausep.exe... but I cant figure out how to cast that aProcesses[i] into a pointer to a null-terminated string for use it as 4th argument in shellexecute
    Last edited by BianConiglio; 05-24-2004 at 08:40 AM.
    This forum is the best one I've ever seen. Great ppl, great coders

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    DWORD stands for double word so its actually bigger then an int (twice the size usally)

    A null terminated string would be passed as a char *. You will need to write your own routine or find one that converts from DWORD to char *

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thanx

    i will try to write one, then i'll post it here
    This forum is the best one I've ever seen. Great ppl, great coders

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    done

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include "psapi.h"
    #include <string.h>
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char * lpCmdLine, int  nCmdShow)
    {
        char szProcessName[MAX_PATH] = "<unknown>";
        
        char separatori[] = " ";
        char setipath[MAX_PATH];
        
        double tmp;
        char *s;
    
        
        int  attivo;
        int  inattivo;
            
        char *seti;
        char *att;
        char *inatt;
                
        seti  = strtok( lpCmdLine, separatori );
        att   = strtok( NULL, separatori );
        inatt = strtok( NULL, separatori );
    
        strcpy (setipath, seti);
        attivo =(int)atof(att);
        inattivo =(int)atof(inatt);
            
        DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;
    
        if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
            return;
    
        cProcesses = cbNeeded / sizeof(DWORD);
    
        for ( i = 0; i < cProcesses; i++ )
        
        {
        
          HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       FALSE, aProcesses[i] );
               
          if (NULL != hProcess )
           {
              HMODULE hMod;
              DWORD cbNeeded;
    
              if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                   &cbNeeded) )
              {
                  GetModuleBaseName( hProcess, hMod, szProcessName, 
                                   sizeof(szProcessName) );
              }
           }
        
           if (!strcmp(szProcessName, setipath))
             {
               printf( "%s  (PID: %u)\n", szProcessName, aProcesses[i] );
               tmp=(double) aProcesses[i];
               sprintf(s,"%g",tmp);   
             ShellExecute( NULL, "open", "pausep.exe", s, NULL, SW_HIDE );
               
             }
           
          CloseHandle( hProcess );
        
        }
       
        
       
       system("PAUSE");
    }
    This forum is the best one I've ever seen. Great ppl, great coders

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Maybe I'm missing it but where are you giving s any space? All I see is char *s; which only allocates space for the pointer but no storage for the string.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    I give it no space, I'm used to do it (as u can see i'm a newbie), I bet dev-cpp (my compiler)allocates space for it... because it works fine
    This forum is the best one I've ever seen. Great ppl, great coders

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    do this:
    Code:
    char *s = NULL;
    and see if that works. What is happening is that the value on the stack when you declare s just so happens to able to interpet it as a valid address to use. This is a very bad thing because you have no idea what you are pointing to and what you are overwritting.

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    thanx for ur suggestion but if I write

    char *s = NULL;

    it crushes ! and it comes back working if i delete the = NULL
    This forum is the best one I've ever seen. Great ppl, great coders

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    EXACTLY!

    See you never really provided room for the character string. You were relaying on a bug. Simply change it from a pointer to an array and you'll be all set.

    One more thing:
    Code:
    (double) aProcesses[i];
    While double and DWORD are the same size a double can not hold the max value of DWORD accurately. This is because some of the bits are being used to store other information (such as the exponent). There really isn't a single cast that will accurately convert a dword into another type for use with sprintf()

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Hmm I must be rusty when it comes to windows programming. On dev cpp the sizeof DWORD is 4 so you could actually use:
    Code:
    sprintf(s, "%u", (unsigned int)aProcesses[i]);
    Sorry about that

  14. #14
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    Quote Originally Posted by Thantos
    Sorry about that
    dont worry

    like I said i'm a newbie and you r helping me learning C
    thanx for your time and suggestions
    This forum is the best one I've ever seen. Great ppl, great coders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Attribute Refuses Cast
    By Hyle in forum C# Programming
    Replies: 6
    Last Post: 03-21-2006, 09:52 AM
  2. type cast problem in VS2005
    By AngKar in forum C++ Programming
    Replies: 4
    Last Post: 01-27-2006, 01:16 AM
  3. atof()
    By Luigi in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2003, 10:08 AM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. reinterpret_cast, C-style cast or function-style cast
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2002, 10:07 PM

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