How to fix this warning?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: How to fix this warning?

  1. #1
    Join Date
    Aug 2002
    Posts
    640

    [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,499

    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
    5,632

    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. 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/

    C, C++ Compiler: Microsoft VS2017.2

  4. #4
    Join Date
    Aug 2002
    Posts
    640

    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
    Wallisellen (ZH), Switzerland
    Posts
    18,940

    Re: How to fix this warning?

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

  6. #6
    Join Date
    Jul 2017
    Posts
    10

    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
    5,632

    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. 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/

    C, C++ Compiler: Microsoft VS2017.2

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)