0

below is c++ code. And I am getting these warnings when compile

comparison of integer expressions of different signedness: ‘int’ and ‘std::__cxx11::basic_string::size_type’ {aka ‘long unsigned int’} [-Werror=sign-compare] 15 | for(int x=0; x<s.length();x++) ~^~~~~~~~~~~

different signedness means that I am comparing long unsigned int which is 64 bits to int (which should be 32 bit) I did not know.. that string.length returns that type. So my question is how to write these simple instructions in c++ without any error showing when compiled with -Wall -Werror

I also get following error message. I supposed string class is in namespace std so the question is: when do we get this error and what that error tells. I think its saying that my program uses only namespace which is std so its not required to do std:string s to create a variable . this removes the error string s without namespace. is this correct thinking or is there any other meaning too

f.cpp:7:1: error: label ‘std’ defined but not used [-Werror=unused-label] 7 | std:string s="hello";

#include <iostream>
#include <cstring>
using namespace std;

int main()
 {
   string s="hello";
   char c[s.length()-1];
   s[1]='b';
   char d='x';
   s.push_back(d);
   s+='x'; 
   strcpy(c,s.c_str());
   for(int x=0; x<s.length();x++)
       {
          cout<<c[x];
       }
}
user786
  • 3,902
  • 4
  • 40
  • 72
  • 3
    int is signed, length is unsigned. This is obvious. Use size_t x = 0. – 273K May 16 '21 at 06:50
  • It's `std::string`, not `std:string`. `::` is a different token than `:`; it's not two `:` tokens in a row. – j6t May 16 '21 at 06:54
  • std:string s declares a label std: (for goto) where string s var is declared. You want std::string s. – 273K May 16 '21 at 06:54
  • `for (size_t x = 0; ...` is the direct answer to your question (in addition to the other fixes suggested above), but you are better off using a [Range-based for loop (since C++11)](https://en.cppreference.com/w/cpp/language/range-for) and simply `for (const auto& ch : s) std::cout << ch;` see also [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) Save the link to [cppreference.com](https://en.cppreference.com/) -- best reference on the internet. – David C. Rankin May 16 '21 at 07:02
  • Your code has buffer overflow. Pay attention when copying strings longer than buffer can hold. Or, better yet, don't use C-style arrays. – sklott Feb 17 '23 at 19:31

1 Answers1

0

Because str.length() returns long unsigned int rather than int, casting it as an int should prevent it from giving an warning or error.

Instead of

std::string str = "Test";
for (int i = 0; i < str.length(); i++) {
  something(str[i]);
}

Try

std::string str = "Test";
for (int i = 0; i < (int) str.length(); i++) {
  something(str[i]);
}
NoahM
  • 1
  • 1
  • 1
    Rather than using a horrible, C-style cast, why not just declare the loop variable as the appropriate (`size_t`) type: `for (size_t i = 0; i < str.length(); ++i) ...`? – Adrian Mole Feb 21 '23 at 12:28