PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 16, 2022

[FIXED] How can i edit this line to don't be a warning aymore

 July 16, 2022     c++, warnings     No comments   

Issue

The following line gives me a warning:

for (int i = 0; i < SpamBannListArray.size(); i++)
char.cpp: In member function 'bool CHARACTER::SpamListCheck(const char*)':
char.cpp:7280: warning: comparison between signed and unsigned integer expressions

What do I need to change in order to get rid of the warning above?


Solution

You should use a unsigned type for the declaration of i in the for() loop header, since SpamBannListArray.size() most probably returns an unsigned type:

 for (unsigned int i = 0; i < SpamBannListArray.size(); i++)
   // ^^^^^^^^

or

 for (size_t i = 0; i < SpamBannListArray.size(); i++)

Otherwise your code might be prone for signed value overflows / wraparounds when hitting negative values.


As pointed out in comments using a range based for() loop, without need to specify an indexing variable should be preferred with the current c++ standard:

for (auto item : SpamBannListArray) {
    // Do something with item
}

In case you need to manipulate items inplace, use auto &.



Answered By - πάντα ῥεῖ
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing