By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,041 Members | 1,734 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,041 IT Pros & Developers. It's quick & easy.

Struct and Pointers

P: n/a
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.
Apr 20 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

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. 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.

Apr 20 '06 #2

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

or so: *(MyStream.open)?


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);

??
Apr 20 '06 #3

P: n/a
Timur Ametov opined:
> typedef struct {
> cdContext contextH;
> /* stream I/O function pointers */
> cdSOpen* open;
> cdSClose* close;
> cdSRead* read;
> cdSWrite* write;
> cdSSeek* seek;
> cdSTell* tell;
> } cdStream;
>

> or so: *(MyStream.open)?


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);


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>

Apr 20 '06 #4

P: n/a

Vladimir S. Oka wrote:
*(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!!

Apr 21 '06 #5

P: n/a
Naresh opined:

Vladimir S. Oka wrote:
*(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!!


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>

Apr 21 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.