Thread: Different types for function parameter - how?

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    Different types for function parameter - how?

    I've been getting two types of warnings that are starting to bother me. The most common of which is with my "LoadFile" function. I'm seeing this warning a lot and it's bogging down the list of warnings making it hard to search for where syntax errors are occurring. Thing is, I don't understand what the warning means (the ending part in particular).

    Code:
    char LoadFile(const char FileName[64], char FileType, BITMAPINFOHEADER *BMPInfo, 
                  unsigned char *BMPData, char FogUsed, double ObjectScaling, 
                  char LoadType)
    ...
    LoadFile("HillsBG.tga", 2, &HillsInfo, &HillsData, 1, Hills.Scaling, 0);
    That's the function header and how it's called. This is the warning I'm getting, taking up about half of the list of warnings I've got:

    c:\my documents\my programs\interactive animation\interactive animation\interactive animation.c(1779) : warning C4047: 'function' : 'unsigned char *' differs in levels of indirection from 'unsigned char (*__w64 )[163840]'
    c:\my documents\my programs\interactive animation\interactive animation\interactive animation.c(1779) : warning C4024: 'LoadFile' : different types for formal and actual parameter 4
    The one referenced is the HillsData (when called) and BMPData (in the function parameter list). The BMPData is an array of a variable size. The image is 1024x40 and 32-bit color so the image data uses 163840 bytes, the reason for that array size. The mountains are 1024x128 and 32-bit color and thus use 524,288 as the array length. The sky is 640x240 and 24-bit color so it has 460,800. In other words, it's all over the place. What does this warning mean? I understand the "unsigned char *" part of it (a pointer to an unsigned char type), but what about the "unsigned char (*__w64 )[163840]" part? How do I fix this?

    The other warnings occur every time I use strcpy, sprintf, and some other common C things. This warning is:

    c:\my documents\my programs\interactive animation\interactive animation\interactive animation.c(1130) : warning C4996: 'sprintf' was declared deprecated
    c:\program files\microsoft visual studio 8\vc\include\stdio.h(345) : see declaration of 'sprintf'
    Message: 'This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
    What is wrong with using strcpy or sprintf? Why is it deprecated? These types of things take up about 2/5 of the warnings, most of which from using sprintf.

    Ignoring these, I have very few warnings - only 5. Otherwise, I have 67 at the moment (!).
    Last edited by Dave_Sinkula; 04-21-2007 at 01:31 PM. Reason: Wrapped code.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The one thing you missed was the actual declarations of those variables -- it's an important piece of information. Since it sounds like true arrays, then the & is extraneous.
    Code:
    LoadFile("HillsBG.tga", 2, &HillsInfo, &HillsData, 1, Hills.Scaling, 0);
    Same location; but the wrong type.
    What is wrong with using strcpy or sprintf? Why is it deprecated? These types of things take up about 2/5 of the warnings, most of which from using sprintf.
    They can be used in unsafe ways; Microsoft tries to get you to use their non-standard stuff instead.

    Consider snprintf as a replacement for sprintf, or follow the warnings' instructions to get rid of them.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    This is the related area for how they are declared. Leaving out the ampersand causes compiler errors to occur.

    Code:
    unsigned char HillsData[163840]; // The hills background - 1024x40x32
    LPVOID HillsDataPointer;
    BITMAPINFO HillsMainInfo;
    BITMAPINFOHEADER HillsInfo;
    LPBITMAPINFOHEADER HillsInfoPointer;
    HBITMAP HillsBMPHandle;
    struct SceneryData Hills;
    That's how the elements for the first part are defined. The third parameter is okay, but the fourth is causing the warning.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Yes. On that one did you remove the & from the call? Did the warning go away?

    [edit]
    Code:
    LoadFile("HillsBG.tga", 2, &HillsInfo, HillsData, 1, Hills.Scaling, 0);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Wow! I had more instances than I thought. I removed that & from the call and from 67 warnings, it dropped down to just 23 (that's almost 1/3 the original)! Why are strcpy, fopen, sprintf, etc. unsafe?

    Another warning I'm confused about, this one not making any sense.

    c:\my documents\my programs\interactive animation\interactive animation\interactive animation.c(372) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
    There are many instances of this, but the odd part is that nothing in the area uses a double. This is line 372, the one in question:

    Code:
    // locals, for reference (in the LoadFile function)
    	float NewColor = 0;
    	float FogIntensity = (float)ObjectScaling;
    	float FogRange = (float)visibility;
    
       NewColor = (((float)FogColor[0]*FogIntensity+(float)BMPData[ArrayIndex]
                    *(FogRange-FogIntensity))/FogRange)+0.5; // fog formula
    Visibility is a double as well as ObjectScaling. They are type casted to a float though so why is the warning present here?
    Last edited by Dave_Sinkula; 04-21-2007 at 02:32 PM. Reason: Wrapping again.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by ulillillia View Post
    Why are strcpy, fopen, sprintf, etc. unsafe?
    You can write beyond the size of the receiving string using strcpy and sprintf.

    Quote Originally Posted by ulillillia View Post
    Another warning I'm confused about, this one not making any sense.



    There are many instances of this, but the odd part is that nothing in the area uses a double. This is line 372, the one in question:

    Code:
    // locals, for reference (in the LoadFile function)
    	float NewColor = 0;
    	float FogIntensity = (float)ObjectScaling;
    	float FogRange = (float)visibility;
    
       NewColor = (((float)FogColor[0]*FogIntensity+(float)BMPData[ArrayIndex]
                    *(FogRange-FogIntensity))/FogRange)+0.5; // fog formula
    Visibility is a double as well as ObjectScaling. They are type casted to a float though so why is the warning present here?
    0.5 is a double, 0.5F is a float.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Ah, I can see the reasoning for the deprecation.

    I was told that by adding a .0 or .f at the end of a number (like 5 or 182 with no fractional part) would make it a float. Oh well. Either I misunderstood the message (and this was in November of 2006) or I was told wrong. Thanks for clearing it up. This thread can be closed now if needed.

  8. #8
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Quote Originally Posted by ulillillia View Post
    I was told that by adding a .0 or .f at the end of a number (like 5 or 182 with no fractional part) would make it a float. Oh well. Either I misunderstood the message (and this was in November of 2006) or I was told wrong. Thanks for clearing it up. This thread can be closed now if needed.
    Probably a misunderstanding as it does force a number to become floating point (a format), although not necessarily a float (a type).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM