This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Pointer Incrementing Warning

Hi Friends,
I'm Currently working on C51v6.02 developed by Keil, I'm facing a problem with the compiler when I compile a similar code like this

main()
{
   char xdata *Data_Ptr = 0x0000; /* Pointer to the 0x000 location */

   while(some loop)
   *Data_Ptr++; /* Points to the next data location on the RAM-- here 0x0001 */
}

when I compile this it gives the following warning message,
 .\FILENAME(LINENUMBER): warning C275: FILENAME: expression with possibly no effect 

Is there any way to avoid this warning message?? Thank you,

With Regards,
Yaswanth

Proudly wasting time since 1981 

  • Yaswanth,
    if you use:

    main()
    {
       char xdata *Data_Ptr = 0x0000; /* Pointer to the 0x000 location */
    
       while(some loop)
       Data_Ptr++; /* Points to the next data location on the RAM-- here 0x0001 */
    }
    
    instead of your original notation (no *Data_Ptr++;) you get same result but no any
    warning.

    BTW:
    Your line -
       char xdata *Data_Ptr = 0x0000; 
    
    is same as
       char xdata *Data_Ptr;
       Data_Ptr = 0x0000; 
    


    Regards,
    Vaclav

  • "you get same result"

    NO you don't!

    Data_Ptr++; // Advances the pointer to the next location
    
    *Data_Ptr++ // Increments the value of the data pointed-to by the pointer!
    

  • I'm afraid, that's not true. The two statements are equivalent.
    The ++ and * operators have same precedence but are evaluated right to left.
    - Mike

  • Oh yeah.

    Maybe that's the reason for the warning: the pointer has been incremented, and then dereferenced, but nothing has been done with the dereferenced value?

  • Vaclav,
    I guess we need to do it this way.

    char xdata *Data_Ptr;
    Data_Ptr = (char *) 0x0000; /* I guess this is the way it must be done */
    

    /* If done this way */
    Data_Ptr = 0x0000;
    /* you are not type casting it then in that case the compiler should/will give a warning -- Haven't tried it though. */
    

    And I haven't heard of
    Data_Ptr++; /* Will point to the next location */
    *Data_Ptr++; /* But surely this will point to the next location */
    

    But why should it give a warning when both does the same stuff, and I guess
    *Data_Ptr++;
    
    is the correct way of incrementing it, is it not?

    Yaswanth
     Proudly wasting time since 1981 

  • Andrew,

      *Data_Ptr++ // Increments the value of the data pointed-to by the pointer!
    
    What you meant in case above is:
     (*Data_Ptr)++;
    

    Regards,
    Vaclav

  • Hmm... Not sure what "the correct way" means. But the rest of the world increments a pointer this way:

    Data_Ptr++;
    
    Of course, there are other ways:
    ++Data_Ptr;
    or
    Data_Ptr += 1;
    or
    Data_Ptr = Data_Ptr + 1;
    
    Or you can do it this way:
    *Data_Ptr++;
    this expression is same as
    *(Data_Ptr++);
    or
    Data_Ptr += 1, *Data_Ptr;
    
    But the dereferencing operator * is absolutely unnecessary because its result is not used. That's why the compiler gives the warning.
    Regards,
    - Mike

  • Got you...
    Thanks Mike.

    -Yaswanth

     Proudly wasting time since 1981 

  • Yaswanth,

    " I guess we need to do it this way.

    char xdata *Data_Ptr;
    Data_Ptr = (char *) 0x0000; /* I guess this is the way it must be done */

    /* If done this way */
    Data_Ptr = 0x0000;
    /* you are not type casting it then in that case the compiler should/will give a warning -- Haven't tried it though. */
    "

    There isn't necessary to do casting because it is only initialized pointer to char in xdata memory at location 0.
    Compiler cannot be confused anyway I think. C51 Keil compiler don't generate warning in this case.

    Rest of your question:
    I agree with Mike's last and Andrew's second contribution.

    Vaclav

  • I don't think the warning message is about pointer incrementing, instead, it is telling you that you are not doing any assignment operations in that loop - "expression with possibly no effect ".

  • I don't think the warning message is about pointer incrementing, instead, it is telling you that you are not doing any assignment operations in that loop - "expression with possibly no effect ".

    That's exactly what it's telling you. Look folks, we need to learn ISO/ANSI C before we can start attacking the Keil compiler. If * didn't have precedence over ++ then how would the classic memcpy() example ever work? E.g.

    while (count--)
    {
        *pDest++ = *pSrc++;
    }
    Anyhow, now that we all understand some precedence let's look at what you do with your dereferenced value... nothing. It's no different that saying:
    int main(void)
    {
        int status;
        for (;;) status; /* legal but useless */
    
        return 0;
    }