473,797 Members | 3,183 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 #1
40 2313
gert wrote:
#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 ?
You are attempting to write something almost, but not entirely, unlike
C. It is more like JavaScript.

You can't bind functions (or anything else) to structs at runtime in C.

--
Ian Collins.
Feb 9 '07 #2
i started from python actually. I was trying to figure out how to make
a struct that has data and functions defined in the struct it self ?
Feb 9 '07 #3
gert wrote:
i started from python actually. I was trying to figure out how to make
a struct that has data and functions defined in the struct it self ?
Please keep the context you are replying to.

You can't have functions in structs in C. What yo can have is function
pointers:

struct Obj {
int someInt;
int (*someFn)(void) ;
};

But you have to assign a function address to the pointer for each
instance of Obj.

--
Ian Collins.
Feb 9 '07 #4
On Feb 9, 1:30 am, Ian Collins <ian-n...@hotmail.co mwrote:
You can't have functions in structs in C. What yo can have is function
pointers:

struct Obj {
int someInt;
int (*someFn)(void) ;

};

But you have to assign a function address to the pointer for each
instance of Obj.
So this means i have to do something like this then ?

#include <stdio.h>

struct obj {
char *data = ''
int (*add)(void);
}

function add(char *value){
obj.data += value;
}

obj function hello(){
obj.data = 'hello';
return obj;
}

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

Feb 9 '07 #5
gert wrote:
On Feb 9, 1:30 am, Ian Collins <ian-n...@hotmail.co mwrote:
>>You can't have functions in structs in C. What yo can have is function
pointers:

struct Obj {
int someInt;
int (*someFn)(void) ;

};

But you have to assign a function address to the pointer for each
instance of Obj.


So this means i have to do something like this then ?

#include <stdio.h>

struct obj {
char *data = ''
int (*add)(void);
}
No, you are still assuming C is an OO language. In C you have to be
explicit:

#include <stdio.h>
#include <stdlib.h>

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

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

int main(void) {
Obj* obj = malloc( sizeof obj );

obj->value = 0;
obj->add = add;

obj->add( obj, 42 );

printf( "%d\n", obj->value );

return 0;
}

--
Ian Collins.
Feb 9 '07 #6
Ian Collins wrote:
int main(void) {
Obj* obj = malloc( sizeof obj );
Oops, malloc( sizeof *obj );
--
Ian Collins.
Feb 9 '07 #7
On 9 Feb, 00:43, "gert" <gert.cuyk...@g mail.comwrote:
On Feb 9, 1:30 am, Ian Collins <ian-n...@hotmail.co mwrote:
<snip>

So this means i have to do something like this then ?
you can't write programs in *any* language by guessing the syntax.
Get a good book (eg. http://cm.bell-labs.com/cm/cs/cbook/index.html).
#include <stdio.h>

struct obj {
char *data = ''
string (aka char*) constancts are delimited by " (double quote)
not ' (single quote)
int (*add)(void);

}

function add(char *value){
C has no keyword "function". A function must return a value.
obj.data += value;
obj hasn't been defined. Is obj a type or a variable?
>
}

obj function hello(){
obj.data = 'hello';
return obj;

}

void main(){
int main(void)
test = hello();
test is not defined. This is the wrong syntax for function pointer
assignment.
Or is it a struct assignment? C is strongly typed. You need to decide
on the
type for everything.
test.add('world ');
printf(test.dat a);
}
but the basic problem is you are trying to learn C by guessing.
--
Nick Keighley
Feb 9 '07 #8
gert <ge**********@g mail.comwrote:
So this means i have to do something like this then ?
(snip not-C)
No, not really. I've taken the liberty of translating your pseudo-C
into a real C program - which is syntactically correct but *will* fail
(produce undefined behavior) at run time. After considering the vast
differences between your conception of C and what C is, I suggest you
take Mr. Tobin's advice and pick up a C textbook before trying
anything else.

#include <stdio.h>
#include <string.h>

struct obj {
char *data;
void (*add)(struct obj *,char *);
};

void add(struct obj *o, char *value){
strcat(o->data, value); /* assume o->data is large enough */
}

struct obj *hello(struct obj *o){
o->data="hello" ;
return o;
}

int main(void){
struct obj test;
hello( &test );

test.add=add;
test.add(&test, "world"); /* Syntatically correct but WRONG */
printf("%s\n",t est.data);
return 0;
}

--
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 #9
On Feb 9, 2:41 am, Ian Collins <ian-n...@hotmail.co mwrote:
No, you are still assuming C is an OO language. In C you have to be
explicit:

#include <stdio.h>
#include <stdlib.h>

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

} Obj;

int add( Obj* obj, int val ) {
obj->value += val;
return obj->value;

}

int main(void) {
Obj* obj = malloc( sizeof obj );

obj->value = 0;
obj->add = add;

obj->add( obj, 42 );

printf( "%d\n", obj->value );

return 0;

}
Thanks this is exactly what i was guessing for :) I always start from
this example to learn a language. It defines for me a basic learning
path. So if something doesn't work i have this example to figure out
why a other example doesnt work. I can read about pointers, structs
and functions but that doesnt mean i understand it.

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.
Feb 9 '07 #10

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

Similar topics

17
6529
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
8633
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
1983
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
24546
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
3131
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
9289
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
1498
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
9685
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
9537
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
10469
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
10023
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
9066
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...
0
6803
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
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
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

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.