Menu

What type of var to store IP & Host address?

2002-11-21
2012-09-26
  • Nobody/Anonymous

    What type of variable should i use on a windows program to store an IP adress (xxx.xxx.xxx.xxx)? And what should i use to store a host address (xxxx.xxxxxxx.xxxxxx.net)?

     
    • Nobody/Anonymous

      char *pszBuffer = NULL;

      pszBuffer = malloc(strlen("xxx.xxx.xxx.xxx"));

       
      • Nobody/Anonymous

        Don't forget that of you assign memory for a string you must also add one byte for the null terminator.

        e,g,

        char *pszBuffer = NULL;

        pszBuffer = malloc(strlen("xxx.xxx.xxx.xxx")+1);

        however, you would normally only use malloc if the memory you required was going to change frequently. With a fixed length string you can just use an array.

        e,g,

        char pszBuffer[16];

        BlakJak :]

         
    • Nobody/Anonymous

      When it comes to ip addresses you always know at least the biggest size theyll get (12 numbers, 3 points and a null terminator=16), but for hosts youd better use malloc anyway.

      I have a program that im working on and i had a simmilar problem, except that in my case, the user can fill the fields with a host or ip address. In that case i decided to make it iporhost[50] just in case.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.