Thread: sprintf and tons of warnings with types - how to remove

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

    sprintf and tons of warnings with types - how to remove

    Every time I use sprintf, I get this warning:

    warning C4057: 'function' : 'char *' differs in indirection to slightly different base types from 'unsigned char [2048]'

    I have the warning level set to the maximum, 4. Here's an example that causes this to get triggered:

    Code:
    // at the top
    unsigned char TextString[2048]; // a basic string with plenty of room
    
    ... // in a function
    sprintf(TextString, "This warning clutters things up, making error locations hard to find.");
    I can't set the string upon the variable declaration because the string actually depends on many parameters. The output I'm getting is what I'm expecting though at least (or I won't be able to see any of the numbers, menus, etc. I have). If I recall about 6 months ago, I wasn't getting this warning at all with this same setup. Did some update to VC++ cause this to start happening?
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think the difference is between char * and unsigned char *. Do you need unsigned char *?

  3. #3
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I do need unsigned char or my text drawing function (this function provides word wrap) won't work due to the use of special characters (those after 0x7F such as the infinity symbol). I'm using a bitmap font of my own creation so I can essentially have anything after the 0x7F.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can put characters higher than 0x7F into a plain char as well.... If you are doing a lot of conversion back-and-forth to larger data types (which includes using them as array indexes), then it's probably not worth it.

    There is a way to disable individual warnings using a #pragma in VS as I recall: warning

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    // at the top
    unsigned char TextString[2048]; // a basic string with plenty of room
    
    ... // in a function
    sprintf((char *)TextString, "This warning clutters things up, making error locations hard to find.");

  6. #6
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I know that you can have characters higher than 0x7F in a signed char. The reason I can't is because arrays cannot go negative. I have an array that contains the list of start positions and widths for each of the characters in the bitmap font (based on ASCII order until 0x7F) - anything above 0x7F will reference a negative array position causing the drawing to not work. Referencing 0x68, for example will have a reference to "CharWidth[104]" but referencing 0x90 would instead cause it to reference "CharWidth[-112]".

    The use of pragma to disable it might help, though I have yet to try the typecasting route. There are several hundred cases of this so disabling that warning it might be more efficient. Thanks though.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ulillillia View Post
    anything above 0x7F will reference a negative array position causing the drawing to not work. Referencing 0x68, for example will have a reference to "CharWidth[104]" but referencing 0x90 would instead cause it to reference "CharWidth[-112]".

    The use of pragma to disable it might help, though I have yet to try the typecasting route. There are several hundred cases of this so disabling that warning it might be more efficient. Thanks though.
    So why don't you fix the one drawing function to cast to unsigned, instead of the "several hundred cases" that don't need it?


    Quzah.
    Last edited by quzah; 07-21-2011 at 03:31 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Remove a specific char and remove multiple spaces
    By stam in forum C++ Programming
    Replies: 9
    Last Post: 12-18-2010, 07:50 AM
  2. Replies: 5
    Last Post: 02-12-2010, 08:02 PM
  3. Tons of memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2005, 10:19 AM
  4. Building a pc...water cooling/tons of cooling?
    By Shadow in forum Tech Board
    Replies: 22
    Last Post: 12-20-2002, 05:03 AM
  5. Float/double compiler error and TONS of questions!
    By musayume in forum C Programming
    Replies: 5
    Last Post: 10-24-2001, 01:40 PM

Tags for this Thread