Struct and Pointers

T

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 all of them are wrong?

Thanx.
 
V

Vladimir S. Oka

Timur said:
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?

By "use" I assume you mean "dereference"...
So: (*MyStream).open .. ?
Or maybe so: MyStream->open?

These two would be used if `MyStream` were a pointer to your `struct`,
and then only to access its `open` member, not dereference it. It
dereferences `MyStream`, the second is just a shorthand for the first.
or so: *(MyStream.open)?

Yes. This dereferences `open` pointer member of the `MyStream`
structure.
 
T

Timur Ametov

typedef struct {
Yes. This dereferences `open` pointer member of the `MyStream`
structure.

Another question.
Do i understand it right?
cdSOpen would described so:

typedef void cdSTDCALL cdSOpen(cdContext contextH, cdPermission, cdError*
err);

Does it mean, that i should write:

cdContext cCont = ...;
cdPermission cPer= ...;
cdError* cErr;
*(MyStream.Open(cCont, cPer, cErr);

??
 
V

Vladimir S. Oka

Timur Ametov opined:
Another question.
Do i understand it right?
cdSOpen would described so:

typedef void cdSTDCALL cdSOpen(cdContext contextH, cdPermission,
cdError*
err);

Does it mean, that i should write:

cdContext cCont = ...;
cdPermission cPer= ...;
cdError* cErr;
*(MyStream.Open(cCont, cPer, cErr);

More like:

*(MyStream.open)(cCont, cPer, cErr);
^

C is case sensitive.

If I can double guess the purpose of `cErr`, I'd rather put:

cdError cErr;
...
*(MyStream.open)(cCont, cPer, &cErr);



--
Running Windows on a Pentium is like having a brand new Porsche but
only be able to drive backwards with the handbrake on.
(Unknown source)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
N

Naresh

Vladimir said:
*(MyStream.open)(cCont, cPer, cErr);
^

C is case sensitive.

If I can double guess the purpose of `cErr`, I'd rather put:

cdError cErr;
...
*(MyStream.open)(cCont, cPer, &cErr);

But How much it is different to use
cdError cErr;
.........
func( &cErr);

and

cdError *cErr;
.........
func(cErr);

I used to think both effecitively means same!!
 
V

Vladimir S. Oka

Naresh opined:
But How much it is different to use
cdError cErr;
........
func( &cErr);

and

cdError *cErr;
........
func(cErr);

I used to think both effecitively means same!!

I assumed `cErr` is used to return an error value to the caller. That
would require that the object of type `cdError` exists /prior/ to
calling `open()`. Hence my suggestion to declare a variable, and pass
its address.

Alternatively, you can declare and pass a pointer, but then you'd have
to make sure it points to something valid (i.e. something of type
`cdError`). Not doing that would make `open()` dereference an invalid
(or NULL) pointer -- BANG!

My suggestion just guarantees that if it compiles it works, whereas
leaving memory allocation to runtime runs the risk of there not being
enough (and you'd also need to code graceful recovery in that case).

--

"Whip me. Beat me. Make me maintain AIX."
(By Stephan Zielinski)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top