const void * in structure

Discussion in 'C Programming' started by Skybuck Flying, Jun 12, 2011.

  1. Hello,

    I come across the following C/C++ code which is a bit unfamiliar to me:

    const void *SomeField2; // *

    typedef struct SomeStructureName1 {
    int SomeField1;
    const void *SomeField2; // *
    int SomeField3;
    } SomeStructureName2;

    It seems like a "constant void pointer type"

    How to translate this structure to Delphi ?

    My guess would be:

    type
    SomeStructureName1 = record
    SomeField1 : integer;
    SomeField2 : pointer;
    SomeField3 : integer;
    end;
    SomeStructureName2 = SomeStructureName1;

    Also is it just a "syntax" difference or is there a binary difference
    between

    1. void *
    vs
    2. const void *

    ?

    Bye,
    Skybuck.
    Skybuck Flying, Jun 12, 2011
    #1
    1. Advertising

  2. "Skybuck Flying" <> schrieb im Newsbeitrag
    news:3f4f3$4df4035d$5419acc3$1.nb.home.nl...
    > Hello,
    >
    > I come across the following C/C++ code which is a bit unfamiliar to me:
    >
    > const void *SomeField2; // *
    >
    > typedef struct SomeStructureName1 {
    > int SomeField1;
    > const void *SomeField2; // *
    > int SomeField3;
    > } SomeStructureName2;
    >
    > It seems like a "constant void pointer type"
    >
    > How to translate this structure to Delphi ?
    >
    > My guess would be:
    >
    > type
    > SomeStructureName1 = record
    > SomeField1 : integer;
    > SomeField2 : pointer;


    I agree.

    > SomeField3 : integer;
    > end;
    > SomeStructureName2 = SomeStructureName1;
    >
    > Also is it just a "syntax" difference or is there a binary difference
    > between
    >
    > 1. void *
    > vs
    > 2. const void *


    no binary difference, just read-only.

    > ?
    >
    > Bye,
    > Skybuck.
    Heinrich Wolf, Jun 12, 2011
    #2
    1. Advertising

  3. [most newsgroups trimmed from reply]

    On Sat, 11 Jun 2011 20:07:57 -0400, Skybuck Flying wrote:
    > Hello,
    >
    > I come across the following C/C++ code which is a bit unfamiliar to me:


    Which is it? C or C++? They're two different languages.
    My answer assumes that it's C code.

    >
    > const void *SomeField2; // *
    >
    > typedef struct SomeStructureName1 {
    > int SomeField1;
    > const void *SomeField2; // *
    > int SomeField3;
    > } SomeStructureName2;
    >
    > It seems like a "constant void pointer type"


    (void *) means a generic pointer to data, which can be converted to or
    from a pointer to any object type.
    (const void *) means the same kind of pointer, but it points to
    non-modifiable data. So if if SomeField2 actually points to an (int),
    then you can write

    const int *intp = SomeField2;

    giving you a pointer to an int which you can't modify, but you can't
    write

    int *intp = SomeField2;

    because that discards the "const" modifier. When the keyword "const"
    appears in the position shown above, it refers to the pointed-to data.
    If SomeField were itself a constant (unmodifiable) field, then the
    declaration would be written "void * const SomeField2;".
    --
    Morris Keesan --
    Morris Keesan, Jun 12, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    502
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    779
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    817
    S.Tobias
    Jul 22, 2005
  4. Javier
    Replies:
    2
    Views:
    549
    James Kanze
    Sep 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,095
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page