Thread: typecasting?

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    21

    typecasting?

    Sheesh I'm just full of questions.

    I got a question about typecasting in one particular instance. I know that normally you'd typecast like this:

    (type)command

    Right? Well, they use a malloc command with a * next to the typecast, like this:

    struct list {
    int x;
    struct *next;
    };

    struct list *root;
    struct list *pointer;
    struct list *new;

    new = (list*) malloc (sizeof (struct list));

    Can anyone tell me what the * is next to the typecast for? Would the malloc command have worked even without a typecast?

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> Can anyone tell me what the * is next to the typecast for?

    malloc() returns the address of the allocated memory (a pointer to the memory), so it is casted to the appropriate pointer type.

    >> Would the malloc command have worked even without a typecast?

    In C programming the malloc would've worked. In C you don't need to cast malloc().

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    If malloc only returns one type of data, can you think of any reason why the writer of that program would typecast malloc?

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    It has to do with making your program type-safe. IOW, making sure that someting gets casted to the correct type and not relying on the compiler.

    >> can you think of any reason why the writer of that program would typecast malloc?

    It probably increases code readability. Other than that, it's just a style of programming IMO.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    With a C++ compiler, due to stricter rules, malloc, and all other functions returning void* must explicitely cast the result. So the author might have been using such a compiler.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    21
    But malloc doesn't return void does it? I guess I can live with it just being the writer's own style, but I still don't understand what the * was doing next to the cast. Why not just go like this:

    (list)malloc(sizeof(struct list));

    ?

    I don't get what the dillyoh is with the *. If you're typecasting a pointer, wouldn't you put it like this:

    (*list)



    ???

  7. #7
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> If you're typecasting a pointer, wouldn't you put it like this: (*list)

    No, whenever pointers are involved, the statement/expression/declaration/whatever is always read from right to left.

    Here are a few examples of how the code is read.
    Code:
    int *i_ptr;            //i_ptr is a pointer to an int
    float *f_ptr;         //f_ptr is a pointer to a float
    char **c_pptr;     //c_pptr is a pointer to a pointer to a char 
    void *function();  //function returns a pointer to void
    So, to cast malloc(), the indirection operator, *, is placed after the type. So in this case :
    (list*)malloc(sizeof(struct list));

    malloc() would be casted to a pointer to list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer Arithmetic and Typecasting
    By pobri19 in forum C Programming
    Replies: 2
    Last Post: 03-19-2009, 11:06 PM
  2. c and c++ style typecasting confusion
    By vaibhav in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2005, 07:29 AM
  3. typecasting
    By sreetvert83 in forum C++ Programming
    Replies: 7
    Last Post: 07-22-2005, 01:55 PM
  4. Typecasting a void* to a function pointer
    By Procyon in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2004, 05:43 PM
  5. typecasting
    By JaWiB in forum C++ Programming
    Replies: 14
    Last Post: 06-01-2003, 10:42 PM