-4

I am trying to check if ip address is in range by doing an and operation with the net mask , this code is giving me the following error :

invalid conversion from ‘const char*’ to ‘uint32_t {aka unsigned int}’ [-fpermissive]

I am a beginner in c++ is there any solution to this?

int main() {
    uint32_t ip = "192.168.2.1"; 
    // value to check 
    uint32_t netip = "192.168.2.0"; // network ip to compare with 
    uint32_t  netmask = "255.255.255.0"; // network ip subnet mask
    if (  (netip & netmask) == (ip & netmask)) {
        // is on same subnet... 
        std::cout << "On the same subnet" << std::endl;
    } else {
        // not on same subnet... 
        std::cout << "Not on the same subnet" << std::endl;
    }
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Please read about [mcve] and include one in the question. Please do not include links or images of code – 463035818_is_not_an_ai Jun 16 '19 at 08:40
  • Please add that information to your question via [edit]ing. – πάντα ῥεῖ Jun 16 '19 at 08:43
  • i added the code – Meriem OUADAH Jun 16 '19 at 08:52
  • `uint32_t netip = "192.168.2.0";` that initialization is invalid. What's actually unclear about the compiler error message? The `const char[]` array literal cannot be converted to a `uint32_t`. – πάντα ῥεῖ Jun 16 '19 at 08:52
  • i don't know what i should change to make it work – Meriem OUADAH Jun 16 '19 at 08:54
  • _"i don't know what i should change to make it work"_ I believe there's need to change a lot of things in your code even after getting the stuff to compile. You really should read some [good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) about the c++ basics, and if you grasped about these you might refer to some stuff related about network programming (Steven's bible maybe) – πάντα ῥεῖ Jun 16 '19 at 08:58
  • it works when i change uint32_t to const std::string but then i get another error which is No match for 'operator&' – Meriem OUADAH Jun 16 '19 at 08:59
  • Hint: You need to parse the four bytes from the string and represent them in the `uint32_t` in network byte order. – πάντα ῥεῖ Jun 16 '19 at 09:04
  • 1
    You really just need to learn the language before you try to write code to do networking stuff. – David Schwartz Jun 16 '19 at 09:08
  • that's what i'm here for , do u have any suggestions ? – Meriem OUADAH Jun 16 '19 at 09:21
  • 2
    @MeriemOUADAH _"that's what i'm here for"_ Unfortunately that doesn't match the purpose of this site. You cannot really learn the language by attending here. The site is meant to build a FAQ like repository of recurring programming problems and helpng to solve these for future research. Your question is too basic to fall into this category. As mentioned learn from _good books_ in 1st place before asking questions here. – πάντα ῥεῖ Jun 16 '19 at 09:51

1 Answers1

0

The problem is on these lines

uint32_t ip = "192.168.2.1"; // value to check
uint32_t netip = "192.168.2.0"; // network ip to compare with
uint32_t netmask = "255.255.255.0"; // network ip subnet mask

You can't assign string literals to integer variables. You need to parse the content of the string literals into their numeric equivalents. You can use a socket API function like inet_addr() for that, eg:

uint32_t ip = inet_addr("192.168.2.1"); // value to check
uint32_t netip = inet_addr("192.168.2.0"); // network ip to compare with
uint32_t netmask = inet_addr("255.255.255.0"); // network ip subnet mask
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • i did the same thing and it worked but in my case the ip address that i want to convert is inside a variable "s" so it doesn't work when i do this :int32_t ip = inet_addr(s); // value to check – Meriem OUADAH Jun 20 '19 at 10:03
  • @MeriemOUADAH `inet_addr()` takes a `const char *` as input. Assuming the variable is a `std::string`, simply call its `c_str()` method: `inet_addr(s.c_str())` – Remy Lebeau Jun 20 '19 at 15:00