473,785 Members | 2,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding the ability to add functions into structures?

So structures are useful to group variables, so you can to refer to a
collection as a single entity. Wouldn't it be useful to also have the
ability to collect variable and functions?

Ask K&R say, C programs consist of variables to store the input and
functions to manipulate them.

This would make C object-oriented - how cool would that be?

Are there any problems with adding the ability to have functions
encapsulated in structures?

Dec 31 '05
47 3896
Richard Heathfield wrote:
Chuck F. said:
Jack Klein wrote:

... snip ...

I know I'll get flamed for this, but with the exception of
inheritance this is really nothing but syntactical sugar.
You can write object oriented programs in C right now.

A perfect example is the FILE data type, declared an
<stdio.h>. It has a creator, fopen(), a destructor,
fclose(), and all sorts of methods you can invoke on it via
its pointer, such as fprintf(), fscanf(), fread(), fwrite(),
between its successful creation and its destruction.


I see no reason for any fires. The only problem with your
example is that you can't write (in general) that type in C.


I must have misunderstood what you mean, because I see no reason
whatsoever why one could not write (in general) that type in C.


I meant that to implement FILE you have to access such things as
ports, disks, etc. that take you out of pure C.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Dec 31 '05 #11
jacob navia wrote:
[...]
For instance:

1) I define a forward declaration to the object
typedef struct _ArrayList ArrayList;


Have you forgotten that the identifier `_ArrayList'
is off-limits?

"All identifiers that begin with an underscore and
either an uppercase letter or another underscore
are always reserved for any use."
-- ISO/IEC 9899:1999 section 7.1.3 paragraph 1

--
Eric Sosman
es*****@acm-dot-org.invalid

Dec 31 '05 #12
Chuck F. said:
Richard Heathfield wrote:
Chuck F. said:
The only problem with your
example is that you can't write (in general) that type in C.


I must have misunderstood what you mean, because I see no reason
whatsoever why one could not write (in general) that type in C.


I meant that to implement FILE you have to access such things as
ports, disks, etc. that take you out of pure C.


Oh. I thought you meant opaque types were impossible, which they clearly
aren't.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 31 '05 #13
jacob navia wrote:
[...]
2) I define the interface functions:
typedef struct {
// Returns the number of elements stored
int (*GetCount)(Arr ayList &AL);


Whatever this language is, I doubt it's topical on
comp.lang.c.

--
Eric Sosman
es*****@acm-dot-org.invalid
Dec 31 '05 #14
Eric Sosman a écrit :
jacob navia wrote:
[...]
2) I define the interface functions:
typedef struct {
// Returns the number of elements stored
int (*GetCount)(Arr ayList &AL);

Whatever this language is, I doubt it's topical on
comp.lang.c.

Yes, replace the "&" by "*".
lcc-win32 supports "&" as in C++, but it can be
replaced by "*". References are just pointers that
can never be NULL. A pointer will do too.
Dec 31 '05 #15
Eric Sosman a écrit :
jacob navia wrote:
[...]
For instance:

1) I define a forward declaration to the object
typedef struct _ArrayList ArrayList;

Have you forgotten that the identifier `_ArrayList'
is off-limits?

"All identifiers that begin with an underscore and
either an uppercase letter or another underscore
are always reserved for any use."
-- ISO/IEC 9899:1999 section 7.1.3 paragraph 1

Yes, you are right. Just put
typedef struct tagArrayList

Sometimes I use _ because is shorter
Dec 31 '05 #16
Eric Sosman wrote:
jacob navia wrote:
[...]
For instance:

1) I define a forward declaration to the object
typedef struct _ArrayList ArrayList;


Have you forgotten that the identifier `_ArrayList'
is off-limits?

"All identifiers that begin with an underscore and
either an uppercase letter or another underscore
are always reserved for any use."
-- ISO/IEC 9899:1999 section 7.1.3 paragraph 1


In this case I think you have forgotten that Jacob is the
implementor, and thus correctly avoiding the users namespace.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Dec 31 '05 #17
On 2005-12-31 09:50:09 -0500, jacob navia <ja***@jacob.re mcomp.fr> said:
Eric Sosman a écrit :
jacob navia wrote:
[...]
2) I define the interface functions:
typedef struct {
// Returns the number of elements stored
int (*GetCount)(Arr ayList &AL);

Whatever this language is, I doubt it's topical on
comp.lang.c.

Yes, replace the "&" by "*".
lcc-win32 supports "&" as in C++, but it can be
replaced by "*". References are just pointers that
can never be NULL. A pointer will do too.


Why do you keep posting code that, while almost C, will only work on a
single compiler, on a single platform?

--
Clark S. Cox, III
cl*******@gmail .com

Dec 31 '05 #18
jacob navia <ja***@jacob.re mcomp.fr> writes:
Eric Sosman a écrit :
jacob navia wrote:
[...]
For instance:

1) I define a forward declaration to the object
typedef struct _ArrayList ArrayList;

Have you forgotten that the identifier `_ArrayList'
is off-limits?
"All identifiers that begin with an underscore and
either an uppercase letter or another underscore
are always reserved for any use."
-- ISO/IEC 9899:1999 section 7.1.3 paragraph 1

Yes, you are right. Just put
typedef struct tagArrayList

Sometimes I use _ because is shorter


Why not just use

struct ArrayList {
...
};

and then consistently refer to the type as "struct ArrayList"? What
benefit does the typedef give you? (If you think not having to type
the word "struct" is enough of a benefit, I disagree but I won't argue
about it.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 31 '05 #19
Clark S. Cox III a écrit :
On 2005-12-31 09:50:09 -0500, jacob navia <ja***@jacob.re mcomp.fr> said:
Eric Sosman a écrit :
jacob navia wrote:

[...]
2) I define the interface functions:
typedef struct {
// Returns the number of elements stored
int (*GetCount)(Arr ayList &AL);

Whatever this language is, I doubt it's topical on
comp.lang.c.

Yes, replace the "&" by "*".
lcc-win32 supports "&" as in C++, but it can be
replaced by "*". References are just pointers that
can never be NULL. A pointer will do too.

Why do you keep posting code that, while almost C, will only work on a
single compiler, on a single platform?


Well, I forgot that. It is a single compiler yes, and I do not have
the means of porting it. But what's your point?

Changing "&" by "*" will make it work in any compiler in any platform.

Dec 31 '05 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
741
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
5
28675
by: Angel | last post by:
Is there some way to add a C-header file into a C# project? The problem is that this .h file contains several complex structures (over 20) that are used by some unmanaged code. These functions receive these defined structs as parms and modify them during execution. Basically, it'd be something siomple like this (but in C#): #include <parms.h> W32_PARM parm;
11
2583
by: ruffiano | last post by:
A colleague of mine who is a C developer wrote several functions in C which I now need to invoke in my C++ application. This is normally not a problem except that the header file that he wrote contains also several structures and #defines, which I need to use in my C++ application. What is the best way to use the C functions, structures and defines in my code without upsetting the compiler / linker? For the C functions I know I need...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10153
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9952
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.