Need help on pointer to VOID

This is a discussion on Need help on pointer to VOID within the C Programming forums, part of the General Programming Boards category; >how to access and dereference a VOID pointer? Just cast it into the correct data type and dereference it: Code: ...

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how to access and dereference a VOID pointer?
    Just cast it into the correct data type and dereference it:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      void *vptr;
      int a = 5;
      int b;
    
      vptr = &a;
      printf("vprt is %d\n", *(int *)vptr);
    
      b = *(int *)vptr;
      *(int *)vptr = 10;
      printf("b is %d\n", b);
      printf("vprt is %d\n", *(int *)vptr);
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Back to the question I posted, can you guys show
    how to access and dereference a VOID pointer?
    Have you read what's been written here? The second post starts with
    The only difference when it comes to void* pointers is that you can't dereference a void pointer
    Then quzah later said
    You actually can dereference a void pointer. You just have to type cast it to whatever type you're dereferencing it as. If it points to an int, dereference it as if you were using a pointer to an integer by type casting.
    >No one has illustrate this point clearly so far!
    Are your trolling or what?!

    In your code:
    >printf("vprt is %d", *((int*)vptr));
    and
    >*(int *)vptr = 10;

    [edit]Goddammit... beaten by miles![/edit]
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Re: Re: Re: Re: Re: abt VOID *

    Originally posted by quzah

    My reply was not some personal attack on unknown. I was trying to clarify the issue.
    Sorry for being so grumpy


    Originally posted by quzah

    What I was trying to clarify is that exact line of thought. And for the record, there is a type of 'void'. You just cannot create instances of it. If there weren't 'void', how could you use it as return values and parameters?

    Quzah.
    void is an incomplete type , like a structure which has been declared but whose members have not been still defined , but unlike other incomplete types a void cannot be completed ,so you can never have objects of type void . sizeof cannot be applied to any incomplete type .
    I think (I might be wrong) a function taking in a void parameters nothing to do with it being a type , because a function cannot have more than one parameter if it has a void parameter and it does not have to declare a variable for the void type , which clearly marks out something like f(void) as a special syntax to indicate no parameters.
    The one who says it cannot be done should never interrupt the one who is doing it.

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >sizeof cannot be applied to any incomplete type
    I don't have my copy of the standard with me, is this a constraint violation? Because my compiler compiles and runs

    printf ( "%u\n", (unsigned)sizeof ( void ) );

    With nothing more than a warning that the result is 0. Lint returns no warnings.

    -Prelude
    My best code is written with the delete key.

  5. #20
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    I got a warning and a different size of void , I used gcc , but I cant complain , I was warned

    $> cat test.c
    #include <stdio.h>
    int
    main(void)
    {
    printf("sizeof(void)=%lu",(unsigned long) sizeof(void) ) ;
    getchar();
    return 0;
    }

    $> gcc -ansi -pedantic -Wall test.c
    test.c: In function `main':
    test.c:6: warning: sizeof applied to a void type
    $>./a.out
    sizeof(void)=1
    The one who says it cannot be done should never interrupt the one who is doing it.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. saying hello and 1st question
    By darksys in forum C Programming
    Replies: 12
    Last Post: 10-31-2008, 02:58 PM
  2. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM

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