CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2002
    Posts
    756

    [SOLVED]: How to fix this warning?

    Hi, guys,
    I have following code:

    Code:
    int bufferSize = 1024;
    SQLSMALLINT lenUsed;
    for( int i = 0; i < numCols; i++ )
    {
        autoincrement = 0;
        columnNames[i] = new SQLWCHAR[sizeof( SQLWCHAR ) * SQL_MAX_COLUMN_NAME_LEN + 1];
        ret = SQLColAttribute( stmt_colattr, (SQLUSMALLINT) i + 1, SQL_DESC_LABEL, columnNames[i], bufferSize, &lenUsed, NULL );
    }
    Compiling this with MSVC 2010, I'm getting:

    warning C4244: 'argument' : conversion from 'int' to 'SQLSMALLINT', possible loss of data
    on the SQLColAttribute() call.

    How do I fix the warning?

    Thank you.
    Last edited by OneEyeMan; August 1st, 2017 at 11:47 AM. Reason: Marking as solved

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How to fix this warning?

    You could define i as a SQLUSMALLINT, or you could just ignore it.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to fix this warning?

    Try

    Code:
    SQLSMALLINT bufferSize = 1024;
    as the bufferlength param to SQLColAttribute() is typed as SQLSMALLINT - not int. See https://docs.microsoft.com/en-us/sql...ibute-function
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Aug 2002
    Posts
    756

    Re: How to fix this warning?

    @2kaud:
    That was it.
    Thank you.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to fix this warning?

    Or you may replace int with short...
    Victor Nijegorodov

  6. #6
    Join Date
    Jul 2017
    Posts
    14

    Re: How to fix this warning?

    replacing int with short is the best way I think

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: How to fix this warning?

    The bufferlength parameter of function SQLColAttribute() is defined as type SQLSMALLINT. Therefore a variable of this type should be passed. Whilst currently SQLSMALLINT equates to type short - and therefore passing a variable of type short will currently also avoid the compiler warning, IMO this type equivalence shouldn't be assumed as the underlying type of SQLSMALLINT could change in future releases. Always best to use the specified type.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured