Casting Question (I think)

This is a discussion on Casting Question (I think) within the C Programming forums, part of the General Programming Boards category; Sorry for the obscure title. I assume this is casting (i hope) Code: struct ip_header *ip ip = (struct ip_header ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    15

    Casting Question (I think)

    Sorry for the obscure title.

    I assume this is casting (i hope)

    Code:
    struct ip_header *ip
     
    ip = (struct ip_header *) (packet + ethernetHeader)
    Assuming i am correct, this next bit of code threw me.

    Funtion pcap_next retruns a u_char pointer to the packet it captures.

    I thought the code would be:

    Code:
    u_char *packet
    packet = pcap_next(exp1, exp2)
    However in the book i have its written:

    Code:
    u_char packet*
    packet = (u_char *) pcap_next(exp1, exp2)
    Is this casting?? Was my original understanding of casting incorrect, it not a subject im very familiar about.

    Just having trouble understanding why the code in the book (which is correct) has the (u_char) before pcap_next.

    Many thanks for any help!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I assume this is casting (i hope)
    Yes. Whether it's safe casting is another matter, and that depends on the types and values of packet and ethernetHeader.

    >Is this casting??
    Yes, but if the function returns a pointer to u_char, as you say, then the cast is unnecessary.

    >Just having trouble understanding why the code in the book
    >(which is correct) has the (u_char) before pcap_next.
    Too many programmers use casts where they aren't needed, for a variety of reasons. The return value of malloc is a biggie.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    15

    cheers

    Thankyou for the reply!

    I see what you mean, just wanted to make sure i has understood casts!! Was looking at bitwise operators yesterday too, all fun!

    Mind me asking about the malloc comment?

    My understanding of malloc was that it returned a void pointer type, just assigns the memory to the type structure.

    Thus:

    char a
    a = 5

    could you do:

    char a*
    malloc(size of a)

    So you have allocated the space in memory to put your data before you place the data in that space?

    Just curious how casting and malloc were related??

    Anyway, thanks for the reply, appreciate it!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    could you do:

    char a*
    malloc(size of a)
    Close. What you would actually do is:
    Code:
    char *a; /* Create a pointer to a char.  Name this pointer 'a' */
    a = malloc(sizeof(*a)); /* allocate memory to store 1 char.  Assign 'a' to this allocated memory */

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    I see! cool.

    Dont know why i missed the '='!

    Cheers.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Just curious how casting and malloc were related??
    In the past (way way back), there was no such thing as a pointer to void. Pointers to char were used as the generic pointer type, but there's not a conversion from char* to other pointer types. So this wouldn't work under the old rules:
    Code:
    int *p = malloc ( 10 * sizeof ( int ) );
    Because there's not a conversion from char* to int*, that would throw an error. The fix was to cast the result to int*:
    Code:
    int *p = (int *)malloc ( 10 * sizeof ( int ) );
    That's all well and good, but then C89 introduced the void pointer to replace char pointer as a generic pointer type. It also has implicit conversions to and from and other pointer types, so the cast for malloc is no longer required. It's been that way for over twenty years, but people still feel the need to use the cast.

    The problem with using the cast is that it hides legitimate errors, such as a failure to include <stdlib> for a declaration of malloc. So keep in mind that in many cases, a cast is both unnecessary *and* potentially dangerous.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    15
    wow, thats quite complicated! Cheers tho, very interesting!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Question on casting 0
    By laertius in forum C Programming
    Replies: 2
    Last Post: 11-09-2008, 11:15 AM
  3. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 05:01 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21