473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Struct and Pointers

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
5 1436

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
4
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup }...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
5
by: pete142 | last post by:
Hi folks -- I have a 4-long array t of of struct Targets. And a table of int * in p. I need to set up the int * entries in p such that each can reference an int in any member of the Targets t...
3
by: vikas talwar | last post by:
Hi All, Can you please explain me how the 'C' compiler allocate memory to 'struct'. Please go thu the example below and pls suggest me the solution for my problem. Here is my structure...
21
by: heavyz | last post by:
In C, if i want to declare a struct, there are 3 methods to do so: 1: struct dummy { ... }; 2: typedef struct { ... } dummy; 3: typedef struct _dummy { ... } dummy; Usually i use the first...
4
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.