473,806 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

obj function hello()

#include <stdio.h>

obj function hello(){
struct obj = { char *data = 'hello'}
obj.add = obj_add(obj);
return obj;
}

void function obj_add(obj){
obj function add(value){
obj.data += value;
return obj;
}
}

void main(){
test = hello();
test.add('world ');
printf(test.dat a);
}

I don't know much c and i was hoping the code above was pointing out
what i am trying to do ?

Feb 9 '07
40 2318
gert wrote:
>
One last sub question, is this a good way to make programs in C by
defining a struct and assign function pointers to it to make it look
like a oo. Or are there other recommended methods to make a program.
It's perfectly acceptable if an OO solution fits your problem. It is a
common style for drivers that have to implement a set of interfaces
defined by an operating system.

--
Ian Collins.
Feb 9 '07 #11
gert <ge**********@g mail.comwrote:
One last sub question, is this a good way to make programs in C by
defining a struct and assign function pointers to it to make it look
like a oo. Or are there other recommended methods to make a program.
IMHO, you'll do a lot better by adapting to C's paradigm rather than
trying to make it look and feel like Python. For simple programs
there's certainly no call for anything OO.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Feb 9 '07 #12
#include <stdio.h>
#include <stdlib.h>

typedef struct self{
int value;
int (*add)(struct self*,int);
} Obj;

int add(Obj* this, int val) {
this->value = val;
return this->value;
}

int main (int argc, char* argv[]){
Obj* myobj = malloc(sizeof *myobj);
myobj->value = 0;
myobj->add = add;
myobj->add(myobj, 42);
printf("%d\n", myobj->value);
return 0;
}

I modified the example a bit to give it a more oo look but i still
having trouble understanding the memory allocation part ?
Obj* myobj = malloc(sizeof *myobj);

for me it would make more sense if i would wright
Obj* myobj = malloc(sizeof myobj);

The thing is they both give the same output :)

Feb 9 '07 #13
gert <ge**********@g mail.comwrote:
I modified the example a bit to give it a more oo look but i still
having trouble understanding the memory allocation part ?
Obj* myobj = malloc(sizeof *myobj);
for me it would make more sense if i would wright
Obj* myobj = malloc(sizeof myobj);
Why? myobj is a *pointer* - you want enough memory for myobj to point
to an Obj, not however much memory the pointer takes up. This is a
crucial difference.
The thing is they both give the same output :)
Only because you seem to be lucky - the second version is wrong, and
either happens to allocate enough space for an Obj (implying
sizeof(Obj) < sizeof(Obj*)) or accesses memory it has no right to
access.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gma il.com | don't, I need to know. Flames welcome.
Feb 9 '07 #14
On Feb 9, 9:41 pm, Christopher Benson-Manica
<a...@norge.fre eshell.orgwrote :
gert <gert.cuyk...@g mail.comwrote:
I modified the example a bit to give it a more oo look but i still
having trouble understanding the memory allocation part ?
Obj* myobj = malloc(sizeof *myobj);
for me it would make more sense if i would wright
Obj* myobj = malloc(sizeof myobj);

Why? myobj is a *pointer* - you want enough memory for myobj to point
to an Obj, not however much memory the pointer takes up. This is a
crucial difference.
The thing is they both give the same output :)

Only because you seem to be lucky - the second version is wrong, and
either happens to allocate enough space for an Obj (implying
sizeof(Obj) < sizeof(Obj*)) or accesses memory it has no right to
access.
Ok i see, i have to allocate memory for myobj not the pointer it self
Feb 9 '07 #15
Christopher Benson-Manica wrote:
gert <ge**********@g mail.comwrote:

>>One last sub question, is this a good way to make programs in C by
defining a struct and assign function pointers to it to make it look
like a oo. Or are there other recommended methods to make a program.


IMHO, you'll do a lot better by adapting to C's paradigm rather than
trying to make it look and feel like Python. For simple programs
there's certainly no call for anything OO.
I agree, but there is a fairly large C domain, which includes drivers
and streams, where this 'pseudo OO' paradigm is idiomatic.

--
Ian Collins.
Feb 9 '07 #16
"gert" <ge**********@g mail.comwrote in message
>
One last sub question, is this a good way to make programs in C by
defining a struct and assign function pointers to it to make it look
like a oo. Or are there other recommended methods to make a program.
A C program should normally be a structured program.
That is to say, there should be one entry function at the top, as is
required by C and called "main", which calls subroutines which call further
subroutines in a roughly balanced way - "main" is not a long list of calls,
nor do subroutines call each other in a daisy chain.

We stretch the paradigm when we allow recursive functions, mutually
recursive functions, and indirect calls through function pointers.

Object-oriented design, on the other hand, relies very heavily on function
pointers to bind the call at a late stage, often at runtime. Though you can
do object-oriented design in C, the language provides very little support
for it. Just as a couple of Latin phrases won't turn an English text into a
Latin one, a spot of late binding through function pointers can be useful in
C programs. However if you are building the whole program around such
interfaces, really you ought to use C++ or another language.

Feb 9 '07 #17
On Feb 8, 5:02 pm, "gert" <gert.cuyk...@g mail.comwrote:
#include <stdio.h>

obj function hello(){
struct obj = { char *data = 'hello'}
obj.add = obj_add(obj);
return obj;

}

void function obj_add(obj){
obj function add(value){
obj.data += value;
return obj;
}

}

void main(){
test = hello();
test.add('world ');
printf(test.dat a);

}

I don't know much c and i was hoping the code above was pointing out
what i am trying to do ?
To write something like Java or C++ in C, you need to define a lot of
structure, maybe some #define and typedef. Why don't you use Java or C+
+ instead?

Feb 9 '07 #18
On Feb 9, 10:00 pm, "Malcolm McLean" <regniz...@btin ternet.comwrote :
A C program should normally be a structured program.
That is to say, there should be one entry function at the top, as is
required by C and called "main", which calls subroutines which call further
subroutines in a roughly balanced way - "main" is not a long list of calls,
nor do subroutines call each other in a daisy chain.

We stretch the paradigm when we allow recursive functions, mutually
recursive functions, and indirect calls through function pointers.

Object-oriented design, on the other hand, relies very heavily on function
pointers to bind the call at a late stage, often at runtime. Though you can
do object-oriented design in C, the language provides very little support
for it. Just as a couple of Latin phrases won't turn an English text into a
Latin one, a spot of late binding through function pointers can be useful in
C programs. However if you are building the whole program around such
interfaces, really you ought to use C++ or another language.
#include <stdio.h>
#include <stdlib.h>

typedef struct self{
int value;
int (*add)(struct self*,int);
} obj;

int add(obj* this, int val) {
this->value = val;
return this->value;
}

obj* Obj(void){
obj* new = malloc(sizeof *new);
new->value = 0;
new->add = add;
return new;
}

int main (int argc, char** argv){
obj* myobj=Obj();
myobj->add(myobj,7) ;
printf("%d\n", myobj->value);
return 0;
}

But you have to admit it looks nice this way not ?

Feb 9 '07 #19
On Feb 9, 10:30 pm, "fdmfdm...@gmai l.com" <fdmfdm...@gmai l.comwrote:
To write something like Java or C++ in C, you need to define a lot of
structure, maybe some #define and typedef. Why don't you use Java or C+
+ instead?
I started with php then went to javascript and python making me
realise you can do the same with more basic syntax, so why not try to
do it c. I think if you can pull it off in c you can pull it of in any
language :)
Feb 9 '07 #20

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

Similar topics

17
6531
by: George Sakkis | last post by:
Is there a general way of injecting code into a function, typically before and/or after the existing code ? I know that for most purposes, an OO solution, such as the template pattern, is a cleaner way to get the same effect, but it's not always applicable (e.g. if you have no control over the design and you are given a function to start with). In particular, I want to get access to the function's locals() just before it exits, i.e....
4
2058
by: veereshai | last post by:
i want to copy the functions from my source file into a new file...and convert each function into a new object file by compiling it. now, i want to invoke the function using the object file i have created and not by invoking the original function(i.e delete the part which has the function and then try to invoke the same function by including the object file into my source code). Is this possible?
5
8634
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file". Same file path containing special characters works fine in one machine, but doesn't work in other. I am using following API function to get short file path. Declare Auto Function GetShortPathName Lib "kernel32" (ByVal lpszLongPath As
5
1985
by: Ian Bicking | last post by:
I got a puzzler for y'all. I want to allow the editing of functions in-place. I won't go into the reason (it's for HTConsole -- http://blog.ianbicking.org/introducing-htconsole.html), except that I really want to edit it all in-process and in-memory. So I want the identity of the function to remain the same, even as I edit the body and hopefully the signature too. Well, the reason is that I want to edit any function object, without...
54
24548
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
27
3135
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1 Function 'Dec2hms' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
3
3665
by: John Turner | last post by:
typedef void (*vfp)(); typedef vfp (*fp)(); static fp hello() { printf("Hello.\n"); return (fp)&hello; } main(){
3
9291
pbmods
by: pbmods | last post by:
AN INTRODUCTION TO FUNCTION OBJECTS LEVEL: INTERMEDIATE PREREQS: OBJECTS You've seen it before. You're setting up an XMLHttpRequest call, and you need to execute a function when it returns, so you do something like this: http.onreadystatechange = myAwesomeFunction;
5
1499
by: robbiesmith79 | last post by:
Hey fellow nerds, Take this code for example: class hello { function world() { echo "Hello"; } }
1
2396
by: Hunter | last post by:
I am writing a script that needs to send some emails. And I've used smtplib in the past and it is pretty easy. But I thought, gee it would be easier if I could just call it as a function, passing the from, to, subject, and message text. So I wrote it up as a function and it sort of works, but I get a weird error. When it runs it inserts a "\t" tab character before each item during the send portion (which I can see when I turn on...
0
9598
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
10623
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...
1
10373
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10111
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
6877
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
5683
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3852
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.