Thread: I really don't quite understand the reason for this cast

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    I really don't quite understand the reason for this cast

    Code:
    #define BUFSIZE 1500
    
    char sendbuf[BUFSIZE];
    
    void send_v4(void)
    {
         int len;
         stuct icmp *icmp;
    
         icmp = (struct icmp *) sendbuf;
         icmp->icmptype = ICMP_ECHO;
         icmp->icmpcode = 0;
    
        /*rest of code*/
    }

    The question is why force a cast to the array sendbuf? Ie why not do something like
    Code:
    #define BUFSIZE 1500
    
    char sendbuf[BUFSIZE];
    
    void send_v4(void)
    {
         int len;
         stuct icmp *icmp;
    
         icmp->icmptype = ICMP_ECHO;
         icmp->icmpcode = 0;
    
        /*rest of code*/
    }
    I'm just sort of curious because this type of coding move appears in ping and a lot of unix network printer code.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well one reason that whatever you saw wasn't written the way you suggested is because they later dereference that icmp pointer, and you can't dereference a pointer with an undefined value.

    For whatever reason the programmer wanted an array of bytes (or at least I assume, given that it is a character array) interpreted like an instance of struct icmp. I wouldn't be surprised if what you're reading works over a network because data is basically sent over in packets like sendbuff.

    If you're trying to learn how to network, I'm not sure if reading the code for your ping application is a good teacher.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by citizen View Post
    Well one reason that whatever you saw wasn't written the way you suggested is because they later dereference that icmp pointer, and you can't dereference a pointer with an undefined value.
    Sure you CAN do that - it just won't give a good result, as it'd "undefined behaviour" and "anything can happen" - most likely, it's going to crash.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    And specifically that's why you can't. There is no reason not to forbid undefined behavior in the first place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 02:46 PM
  2. My thread stops for no reason
    By lectrolux in forum C Programming
    Replies: 7
    Last Post: 05-21-2003, 07:56 AM
  3. how to cast a char to an int
    By rozner in forum C Programming
    Replies: 2
    Last Post: 03-27-2003, 03:50 PM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM