Structs and pointers

Discussion in 'C++' started by Timur Ametov, Apr 20, 2006.

  1. Timur Ametov

    Timur Ametov Guest

    Hi,

    i know, it would be very stupid question, but i'm c-beginner and i haven't
    found the answer in books. Ok, here it is.

    I have:
    typedef struct {
    cdContext contextH;
    /* stream I/O function pointers */
    cdSOpen* open;
    cdSClose* close;
    cdSRead* read;
    cdSWrite* write;
    cdSSeek* seek;
    cdSTell* tell;
    } cdStream;

    If i declare MyStream:
    cdStream MyStream;
    ... how can i use its properties, that are pointers?

    So: (*MyStream).open .. ?
    Or maybe so: MyStream->open?
    or so: *(MyStream.open)?
    or so:MyStream.(*open)?
    Or all of them are wrong?

    Thanx.
    Timur Ametov, Apr 20, 2006
    #1
    1. Advertising

  2. Timur Ametov

    Guest

    Timur Ametov wrote:
    > Hi,
    >
    > i know, it would be very stupid question, but i'm c-beginner and i haven't
    > found the answer in books.


    You're probably looking for our neighbours, comp.lang.c because C and
    C++
    are different languages.
    The code that you included is C code, and while C++ understands it,
    it's
    not how you'd write these things in C++ at all.

    Do check the C Faq, though, it's good.

    Michiel.
    , Apr 20, 2006
    #2
    1. Advertising

  3. Timur Ametov

    Kaliven Lee Guest

    Timur Ametov дµÀ:
    > Hi,
    >
    > i know, it would be very stupid question, but i'm c-beginner and i haven't
    > found the answer in books. Ok, here it is.
    >
    > I have:
    > typedef struct {
    > cdContext contextH;
    > /* stream I/O function pointers */
    > cdSOpen* open;
    > cdSClose* close;
    > cdSRead* read;
    > cdSWrite* write;
    > cdSSeek* seek;
    > cdSTell* tell;
    > } cdStream;
    >
    > If i declare MyStream:
    > cdStream MyStream;
    > .. how can i use its properties, that are pointers?
    >
    > So: (*MyStream).open .. ?
    > Or maybe so: MyStream->open?
    > or so: *(MyStream.open)?
    > or so:MyStream.(*open)?
    > Or all of them are wrong?
    >
    > Thanx.
    >
    >
    >

    My point of view , the usage is as below:
    (Mystream.open)()
    Mystream is a object of the struct.
    open is one of its variable.
    Kaliven Lee, Apr 20, 2006
    #3
  4. Timur Ametov

    Bari Guest

    The way to use it dependce on the object you have:
    If you defined cdStream as pointer the way to use its properties is:
    cdStream->open
    If cdStream is not a pointer the way to use its properties is:
    cdStream.open

    because open is a function to a pointer you should use it exactly like
    it was a function:
    cdStream->open(), or
    cdStream.open().

    Regards
    Bari, Apr 20, 2006
    #4
    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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    621
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    547
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    630
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,370
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    462
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page