Can somebody explain pointer typecasting in c.

Posted on 2011-05-05
Last Modified: 2012-05-11
Can somebody explain me pointer typecasting in c.
Question by:tpat
    LVL 38

    Expert Comment

    LVL 38

    Expert Comment

    LVL 31

    Expert Comment

    Suppose you have a function that can process several different structs:
        void processSeveralDifferentStucts( void * someStruct, int messageType );

    Your code might look like:
    struct message1 { // ~ messageType = 1
    int a;
    int b;
    short c;
    struct message2{ // ~ messageType = 2
    int x;
    short y;
    short z;
    char message[20];
    } msg2;
    processSeveralDifferentStucts( &msg1, 1);
    processSeveralDifferentStucts( &msg2, 2);

    Open in new window

    No typecasting is required here (but it is desirable, for clarities sake) because C allows conversion of pointers to void* without typecasting. However, typecasting is required in processSeveralDifferentStucts.
    void processSeveralDifferentStucts( void * someStruct, int messageType ) {
      struct message1 ptr1; 
      struct message2 ptr2;
      switch (messageType) {
       case 1:
         ptr1 = (struct message1 *)someStruct;
         printf("%d %d %d \n", ptr1->a , ptr1->b ,ptr1->c);
       case 2:
         ptrStruct = (struct message2 *)someStruct;
         printf("%d %d %d %s\n", ptr2->x , ptr2->y ,ptr2->z, ptr2->message);

    Open in new window

    Well, typecasting here isn't required because, again, C allows a void* pointer to be assigned to another pointer of a different type without the cast.

    But suppose you were doing a TCP accept:
       int accept(int socket, struct sockaddr *address, socklen_t *restrict address_len);

    Now, when using accept, you have to typecast the 2nd argument in the call because you will not get automatic conversion to the type of the passed in pointer.
        struct sockaddr_in serv_addr, cli_addr;
        newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

    Open in new window

    The above two lines were take from:
    LVL 8

    Accepted Solution

    Since you are already doing socket programming,
    the accept call explained by "phoffric" is a good example.
    The same kind of pointer typecasting is needed in bind.

    Mainly, typecasting is needed, wherever the type in the caller and the type in the prototype don't match.
    Since, all pointers takeup only 4 bytes of space, you can typecast any pointer type to any other pointer type.
    But, inside the called function, you will have to take care to unfold it in proper way.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
    Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now