what is typecasting a pointer to the type (void *)p mean?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Abhishek

    what is typecasting a pointer to the type (void *)p mean?

    why do I see that in most C programs, pointers in functions are
    accepted as:
    int func(int i,(void *)p) where p is a pointer or an address which is
    passed from the place where it is called. what do you mean by pointing
    to a void and why is it done?
    Aso, what happens when we typecast a pointer to another type. say for
    example int *i=(char *)p;
    under different situations? I am kind of confused..can anybody clear
    this confusion by clearly explaining to me what actually is happening
    out here?
    Bye

  • Michael Rasmussen

    #2
    Re: what is typecasting a pointer to the type (void *)p mean?

    On Wed, 25 Jan 2006 12:17:23 -0800, Abhishek wrote:
    [color=blue]
    > int func(int i,(void *)p) where p is a pointer or an address which is
    > passed from the place where it is called. what do you mean by pointing to
    > a void and why is it done?[/color]
    A pointer to a void is a pointer to an unqualified place in memory. If you
    want to work with threads this is unavoidable. One of the reasons is a
    way of transfering data from one process to another. You cannot do this
    and at the same time preserve the type information. This also means that
    the receiving process must know how to dereference the pointer - cast it
    to a known type.

    #include <stdio.h>
    #include <pthread.h>
    #include <unistd.h>

    void test_p(void *t)
    {
    int i;
    char *text = (char *) t;
    for (i = 0; i < 10; ++i)
    printf("%s\n", text);
    pthread_exit(NU LL);
    }

    int main()
    {
    pthread_t t1 = 0;
    char *text = "test";

    printf("Howdy world!\n");
    pthread_create( &t1, NULL, (void *)&test_p, (void *) text);
    pthread_join(t1 , NULL);
    return 0;
    }


    [color=blue]
    > Aso, what happens when we typecast a pointer to another type. say[/color]
    for[color=blue]
    > example int *i=(char *)p;[/color]
    This is dangerous since you could loose information an depending on your
    compiler could course either a warning or an error.
    --
    Hilsen/Regards
    Michael Rasmussen


    Comment

    • Scorpio

      #3
      Re: what is typecasting a pointer to the type (void *)p mean?


      Abhishek wrote:[color=blue]
      > why do I see that in most C programs, pointers in functions are
      > accepted as:
      > int func(int i,(void *)p) where p is a pointer or an address which is
      > passed from the place where it is called. what do you mean by pointing
      > to a void and why is it done?[/color]

      void pointer is a generic pointer, meaning that it can be used to store
      address of any data type.

      by (void*)p you are casting the address of p to void* type.
      This is actually redundant. Typecasting is not necessary
      while assigning value to a void* pointer.
      [color=blue]
      > Aso, what happens when we typecast a pointer to another type. say for
      > example int *i=(char *)p;[/color]

      This should not even compile because you're trying to assign a wrong
      type
      of address to int *i.
      [color=blue]
      > under different situations? I am kind of confused..can anybody clear
      > this confusion by clearly explaining to me what actually is happening
      > out here?
      > Bye[/color]

      Sharath A.V

      Comment

      • Abhishek

        #4
        Re: what is typecasting a pointer to the type (void *)p mean?

        Does this alsomean that I get the following flexibility?

        like I can use the test_p function to accept different types of
        pointers by typecasting them to type void * and later perform an action
        depending on a particular flag variable I pass to the function?
        like void test_p(void *t, int i)
        {
        int k;
        if(i==1)
        {
        char *text = (char *) t;
        for (k = 0; k < 10; ++k)
        printf("%s\n", text);
        }
        else
        {
        int *j=(int *)t;
        for(k=0;k<10;k+ +)
        printf("%d\n",* j);
        }

        }

        here...dependin g upon the value of 'i' I pass, I can take appropriate
        action. However, I know that if I pass i=1, then I pass a pointer to a
        string by typecasting to void * and if I pass i !=2 then I pass a
        pointer to an integer by type casting the integer pointer to type void
        *???
        Pls clarify

        Michael Rasmussen wrote:[color=blue]
        > On Wed, 25 Jan 2006 12:17:23 -0800, Abhishek wrote:
        >[color=green]
        > > int func(int i,(void *)p) where p is a pointer or an address which is
        > > passed from the place where it is called. what do you mean by pointing to
        > > a void and why is it done?[/color]
        > A pointer to a void is a pointer to an unqualified place in memory. If you
        > want to work with threads this is unavoidable. One of the reasons is a
        > way of transfering data from one process to another. You cannot do this
        > and at the same time preserve the type information. This also means that
        > the receiving process must know how to dereference the pointer - cast it
        > to a known type.
        >
        > #include <stdio.h>
        > #include <pthread.h>
        > #include <unistd.h>
        >
        > void test_p(void *t)
        > {
        > int i;
        > char *text = (char *) t;
        > for (i = 0; i < 10; ++i)
        > printf("%s\n", text);
        > pthread_exit(NU LL);
        > }
        >
        > int main()
        > {
        > pthread_t t1 = 0;
        > char *text = "test";
        >
        > printf("Howdy world!\n");
        > pthread_create( &t1, NULL, (void *)&test_p, (void *) text);
        > pthread_join(t1 , NULL);
        > return 0;
        > }
        >
        >
        >[color=green]
        > > Aso, what happens when we typecast a pointer to another type. say[/color]
        > for[color=green]
        > > example int *i=(char *)p;[/color]
        > This is dangerous since you could loose information an depending on your
        > compiler could course either a warning or an error.
        > --
        > Hilsen/Regards
        > Michael Rasmussen
        > http://keyserver.veridis.com:11371/p...rch=0xE3E80917[/color]

        Comment

        • Emmanuel Delahaye

          #5
          Re: what is typecasting a pointer to the type (void *)p mean?

          Abhishek a écrit :[color=blue]
          > Does this alsomean that I get the following flexibility?[/color]

          'flexibility' and 'void*' are often associated. Your intuition is correct.
          [color=blue]
          > like I can use the test_p function to accept different types of
          > pointers by typecasting them to type void * and later perform an action
          > depending on a particular flag variable I pass to the function?[/color]

          Somehow, yes. Note that the conversion from/to void* doesn't need an
          explicit typecast.
          [color=blue]
          > like void test_p(void *t, int i)
          > {
          > int k;
          > if(i==1)
          > {
          > char *text = (char *) t;[/color]

          char *text = t;
          if (text != NULL)
          {[color=blue]
          > for (k = 0; k < 10; ++k)
          > printf("%s\n", text);[/color]
          }[color=blue]
          > }
          > else
          > {
          > int *j=(int *)t;[/color]

          int *j = t;
          if (j != NULL)
          {
          [color=blue]
          > for(k=0;k<10;k+ +)
          > printf("%d\n",* j);[/color]
          }[color=blue]
          > }
          >
          > }
          >
          > here...dependin g upon the value of 'i' I pass, I can take appropriate
          > action.[/color]

          Correct.

          --
          A+

          Emmanuel Delahaye

          Comment

          • Michael Rasmussen

            #6
            Re: what is typecasting a pointer to the type (void *)p mean?

            On Wed, 25 Jan 2006 13:01:56 -0800, Abhishek wrote:
            [color=blue]
            > Does this alsomean that I get the following flexibility?
            >
            > like I can use the test_p function to accept different types of pointers
            > by typecasting them to type void * and later perform an action depending
            > on a particular flag variable I pass to the function? like void
            > test_p(void *t, int i)[/color]
            For this kind of problem I would recommend using a struct.

            #include <stdio.h>

            enum Kind {integer, string};

            struct container {
            enum Kind kind;
            void *ptr;
            };

            void test_p(void *data)
            {
            struct container *c = (struct container *) data;
            switch (c->kind) {
            case integer:
            printf("Receive d integer:\t%d\n" , (int) c->ptr);
            break;
            case string:
            printf("Receive d string:\t%s\n", (char *) c->ptr);
            break;
            default:
            printf("Undefin ed data type\n");
            }
            }

            int main()
            {
            struct container c;
            char *text = "test";
            int i = 1;

            c.kind = string;
            c.ptr = (void *)text;
            test_p((void *) &c);
            c.kind = integer;
            c.ptr = (void *)i;
            test_p((void *) &c);
            return 0;
            }

            --
            Hilsen/Regards
            Michael Rasmussen


            Comment

            • Michael Rasmussen

              #7
              Re: what is typecasting a pointer to the type (void *)p mean?

              On Wed, 25 Jan 2006 22:56:57 +0100, Emmanuel Delahaye wrote:
              [color=blue]
              >
              > Somehow, yes. Note that the conversion from/to void* doesn't need an
              > explicit typecast.
              >[/color]
              Well, not always but in this situation it is mandatory:
              void foo(void *t) {
              printf("%s", (char *) t);
              }

              int main() {
              char *t = "test";
              foo(t);
              return 0;
              }

              So might as well use explicit casting since it is a lot more readable for
              an outsider which later has to maintain the code. IMHO

              --
              Hilsen/Regards
              Michael Rasmussen


              Comment

              • Al Balmer

                #8
                Re: what is typecasting a pointer to the type (void *)p mean?

                On Wed, 25 Jan 2006 23:07:54 +0100, Michael Rasmussen <mir@miras.or g>
                wrote:
                [color=blue]
                >On Wed, 25 Jan 2006 22:56:57 +0100, Emmanuel Delahaye wrote:
                >[color=green]
                >>
                >> Somehow, yes. Note that the conversion from/to void* doesn't need an
                >> explicit typecast.
                >>[/color]
                >Well, not always but in this situation it is mandatory:
                >void foo(void *t) {
                > printf("%s", (char *) t);
                >}
                >
                >int main() {
                > char *t = "test";
                > foo(t);
                > return 0;
                >}
                >
                >So might as well use explicit casting since it is a lot more readable for
                >an outsider which later has to maintain the code. IMHO[/color]

                void foo(void *t) {
                char *rt = t;
                printf("%s", rt);
                }

                --
                Al Balmer
                Sun City, AZ

                Comment

                • Abhishek

                  #9
                  Re: what is typecasting a pointer to the type (void *)p mean?

                  Thank you all for that wonderful explanation.
                  So, whats the difference between a null pointer and a pointer which is
                  pointing to void?
                  Are these same?

                  Michael Rasmussen wrote:[color=blue]
                  > On Wed, 25 Jan 2006 22:56:57 +0100, Emmanuel Delahaye wrote:
                  >[color=green]
                  > >
                  > > Somehow, yes. Note that the conversion from/to void* doesn't need an
                  > > explicit typecast.
                  > >[/color]
                  > Well, not always but in this situation it is mandatory:
                  > void foo(void *t) {
                  > printf("%s", (char *) t);
                  > }
                  >
                  > int main() {
                  > char *t = "test";
                  > foo(t);
                  > return 0;
                  > }
                  >
                  > So might as well use explicit casting since it is a lot more readable for
                  > an outsider which later has to maintain the code. IMHO
                  >
                  > --
                  > Hilsen/Regards
                  > Michael Rasmussen
                  > http://keyserver.veridis.com:11371/p...rch=0xE3E80917[/color]

                  Comment

                  • Michael Rasmussen

                    #10
                    Re: what is typecasting a pointer to the type (void *)p mean?

                    On Wed, 25 Jan 2006 14:18:42 -0800, Abhishek wrote:
                    [color=blue]
                    > Thank you all for that wonderful explanation. So, whats the difference
                    > between a null pointer and a pointer which is pointing to void?
                    > Are these same?
                    >[/color]
                    A null pointer points to nothing - eg. you have a pointer of some type
                    which has not jet been assigned any value. A pointer to void is a pointer
                    of some unknown type assigned an address in memory.

                    --
                    Hilsen/Regards
                    Michael Rasmussen


                    Comment

                    • Keith Thompson

                      #11
                      Re: what is typecasting a pointer to the type (void *)p mean?

                      "Abhishek" <abhisheksgumad i@gmail.com> writes:[color=blue]
                      > Thank you all for that wonderful explanation.
                      > So, whats the difference between a null pointer and a pointer which is
                      > pointing to void?
                      > Are these same?[/color]

                      Please don't top-post. Your response belongs below, or mixed with,
                      the article to which you're replying, and you should trim anything
                      that's not relevant to your response. This makes it much easier to
                      read each individual article from top to bottom.

                      No, they're very different things.

                      A null pointer is a particular pointer *value*. There's a null
                      pointer value for each pointer type (typically with the same
                      representation) . A null pointer doesn't point to anything. (It's
                      typically, but not necessarily, represented as all-bits-zero.)

                      A void pointer is a pointer *type*, declared as "void*". It's a
                      generic pointer type. An object of type void* can point to any
                      object, or it can have a null pointer value.

                      You should read sections 4 and 5 of the comp.lang.c FAQ, available at
                      <http://www.c-faq.com/>. In fact, you should read the whole thing.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                      We must do something. This is something. Therefore, we must do this.

                      Comment

                      • Flash Gordon

                        #12
                        Re: what is typecasting a pointer to the type (void *)p mean?

                        Abhishek wrote:

                        Please don't top post. Your reply belongs under the text you are
                        replying to after snipping anything not relevant to your reply. I've
                        corrected it this time.
                        [color=blue]
                        > Michael Rasmussen wrote:[color=green]
                        >> On Wed, 25 Jan 2006 22:56:57 +0100, Emmanuel Delahaye wrote:
                        >>[color=darkred]
                        >>> Somehow, yes. Note that the conversion from/to void* doesn't need an
                        >>> explicit typecast.
                        >>>[/color]
                        >> Well, not always but in this situation it is mandatory:
                        >> void foo(void *t) {
                        >> printf("%s", (char *) t);
                        >> }
                        >>
                        >> int main() {
                        >> char *t = "test";
                        >> foo(t);
                        >> return 0;
                        >> }
                        >>
                        >> So might as well use explicit casting since it is a lot more readable for
                        >> an outsider which later has to maintain the code. IMHO[/color][/color]

                        In general casts are a bad thing. They tell the compiler you know what
                        you are doing even if you don't. So those who do not have a thorough
                        understanding should not use them until they know *why* they are needed
                        in a given situation. My experience is that I have seen more casts to
                        shut the compiler up without solving the underlying problem (and leaving
                        in significant errors without the compiler complaining as a result) then
                        I have seen casts that were required. They also add visual clutter which
                        IMHO makes it harder to read.
                        [color=blue][color=green]
                        >> --
                        >> Hilsen/Regards
                        >> Michael Rasmussen
                        >> http://keyserver.veridis.com:11371/p...rch=0xE3E80917[/color][/color]

                        Please snip peoples signatures (the bit after the -- normally) unless
                        you are commenting on them.
                        [color=blue]
                        > Thank you all for that wonderful explanation.
                        > So, whats the difference between a null pointer and a pointer which is
                        > pointing to void?
                        > Are these same?[/color]

                        What is the difference between an integer variable and the value 0?

                        The null pointer is a specific pointer value that does not point
                        anywhere. You can have null pointers of any pointer type and they always
                        compare equal to each other when converted to a common pointer type.
                        Null pointers are also returned by various library functions to indicate
                        failure.

                        A pointer to void is a specific type of pointer (just as long is a
                        specific type of integer). Any pointer to object type (not a pointer to
                        a function) can be converted to void* and back safely. Converting
                        to/from void* to other object pointer types does not require a cast. So
                        pointers to void are a generic pointer type.
                        --
                        Flash Gordon
                        Living in interesting times.
                        Although my email address says spam, it is real and I read it.

                        Comment

                        • Flash Gordon

                          #13
                          Re: what is typecasting a pointer to the type (void *)p mean?

                          Michael Rasmussen wrote:[color=blue]
                          > On Wed, 25 Jan 2006 14:18:42 -0800, Abhishek wrote:
                          >[color=green]
                          >> Thank you all for that wonderful explanation. So, whats the difference
                          >> between a null pointer and a pointer which is pointing to void?
                          >> Are these same?
                          >>[/color]
                          > A null pointer points to nothing - eg. you have a pointer of some type
                          > which has not jet been assigned any value.[/color]

                          No, a pointer that has not been assigned any value is *not* a null
                          pointer in the general case. If it has static duration that is true, but
                          if it has automatic duration (i.e. a pointer variable declared in a
                          function that is not specified as static) it would be a pure fluke if it
                          happened to be a null pointer.

                          It is true that a null pointer does not point anywhere. It is also the
                          only pointer value that you are guaranteed to be able to convert to any
                          other pointer value. Null pointers are normally either generated by you
                          explicitly using them in the source or on a library routine that returns
                          a pointer failing.
                          [color=blue]
                          > A pointer to void is a pointer
                          > of some unknown type assigned an address in memory.[/color]

                          A variable of type void* might point to something, or on the other hand
                          it could be uninitialised or a null pointer. The pointer itself has no
                          type information so it could point at any type of object (not a
                          function) and it is up to the programmer to keep track of what type it
                          points to.
                          --
                          Flash Gordon
                          Living in interesting times.
                          Although my email address says spam, it is real and I read it.

                          Comment

                          • Lucien Kennedy-Lamb

                            #14
                            CALL FOR COMMENT ON NULL: Re: what is typecasting a pointer to the type (void *)p mean?

                            Abhishek wrote:[color=blue]
                            > So, whats the difference between a null pointer and a pointer which is
                            > pointing to void?
                            > Are these same?[/color]

                            You require a proper explanation of what the terms 'null' and 'void'
                            actually mean in C. Their meanings differ from the standard English
                            sense (just to confuse everyone).


                            Null...

                            In the English language "null" means insignificant, missing or
                            amounting to nothing. It can also mean zero. In fact the German
                            language for number zero is "null".



                            Don't let that confuse you.

                            In C terms a null pointer is a pointer that doesn't point to any
                            variable, structure or function *anywhere*. It's like having a finger
                            that is designed to point to a spot on a map. Point to anywhere on the
                            map and it will have some well defined coordinate (address), but if you
                            haven't decided where to point that finger quite yet... point that
                            finger upwards into space (null).

                            The null value for pointers is only useful for setting pointers that
                            haven't been initialised to a *real* address.

                            Null pointers *must* be assigned using the NULL macro (defined by
                            <stdio.h>). The reason is that, while most of the time NULL is defined
                            as 0, it may not be on every platform.

                            char *string = NULL; /* I'm not pointing to anything useful yet */

                            CALL FOR COMMENT HERE: I've never seen a compiler *not* use 0 as NULL,
                            so who has? Who can list an example where this is not the case? I've
                            seen so much code that relies on the NULL == 0 assumption.

                            Use of a null pointer?

                            Any pointer can be assigned the NULL value. I can be a char, int, long
                            or any structure or union pointer.

                            void PrintMyString(c har *string)
                            {
                            if (string != NULL) /* Check if this is a real address of a
                            possible string */
                            printf("This is the string: %s", string);
                            }

                            and my favourite is:

                            void ProcessTaxRetur n(struct TAX_RETURN *p_tax_return)
                            {
                            if (p_tax_return != NULL) /* Has a valid tax return structure been
                            received? */
                            {
                            SendLargeTaxBil l(p_tax_return) ;
                            }
                            }

                            I've been trying for years to send a null tax return, but it's never
                            worked. :-)


                            Void...

                            In the English language "void" means empty, useless or a vacuum.

                            In C it has quite a different meaning. My best explanation is a type
                            of zero length data.

                            Consider:

                            void DoSomthing(void );

                            The compiler knows what there is no return and no arguments to this
                            function.

                            void*

                            What does this mean? It's a pointer like any other pointer. It holds
                            the address of a memory location.

                            It simply means that "I've got the address of *something*, but I don't
                            know what it is".

                            void *pointers are generally used between software components that
                            expose an interface where the type of data is unknown.

                            Consider

                            void *malloc(size_t size);

                            This standard C library function returns a void * as it has no idea how
                            this memory block is to be used for (something addressed in C++ with
                            the new operator).

                            void* pointers is a convenient way of passing around data pointers when
                            types con't be conveyed. Use them sparingly because real types (and
                            typedefs) should be used whenever possible.

                            I hope this goes some way to explaining how void and null should be
                            used.

                            Lucien Kennedy-Lamb

                            Comment

                            • pete

                              #15
                              Re: CALL FOR COMMENT ON NULL: Re: what is typecasting a pointer to the type (void *)p mean?

                              Lucien Kennedy-Lamb wrote:
                              [color=blue]
                              > Null pointers *must* be assigned using the NULL macro (defined by
                              > <stdio.h>). The reason is that,
                              > while most of the time NULL is defined
                              > as 0, it may not be on every platform.
                              >
                              > char *string = NULL; /* I'm not pointing to anything useful yet */[/color]

                              There's nothing wrong with:
                              char *string = 0;
                              It means exactly the same thing thing as
                              char *string = NULL;
                              no matter how NULL is defined.

                              --
                              pete

                              Comment

                              Working...