Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > obj function hello()

Reply
Thread Tools

obj function hello()

 
 
gert
Guest
Posts: n/a
 
      02-09-2007
#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.data);
}

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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-09-2007
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.data);
> }
>
> 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.
 
Reply With Quote
 
 
 
 
gert
Guest
Posts: n/a
 
      02-09-2007
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 ?


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-09-2007
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.
 
Reply With Quote
 
gert
Guest
Posts: n/a
 
      02-09-2007
On Feb 9, 1:30 am, Ian Collins <ian-n...@hotmail.com> wrote:
> 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.data);
}



 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-09-2007
gert wrote:
> On Feb 9, 1:30 am, Ian Collins <ian-n...@hotmail.com> wrote:
>
>>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.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-09-2007
Ian Collins wrote:
> int main(void) {
> Obj* obj = malloc( sizeof obj );
>

Oops, malloc( sizeof *obj );


--
Ian Collins.
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      02-09-2007
On 9 Feb, 00:43, "gert" <gert.cuyk...@gmail.com> wrote:
> On Feb 9, 1:30 am, Ian Collins <ian-n...@hotmail.com> wrote:


<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.data);
> }


but the basic problem is you are trying to learn C by guessing.


--
Nick Keighley


 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      02-09-2007
gert <> wrote:

> 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",test.data);
return 0;
}

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
 
Reply With Quote
 
gert
Guest
Posts: n/a
 
      02-09-2007
On Feb 9, 2:41 am, Ian Collins <ian-n...@hotmail.com> wrote:
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
testing for valid reference: obj vs. None!=obs vs. obj is not None alf Python 9 12-09-2006 05:00 AM
Obj* ptr = new Obj(X) Phil Endecott C++ 5 06-03-2005 10:48 PM
How do you convert a string obj to a file obj? Matthew Thorley Python 7 05-04-2005 09:04 PM
Descriptors: why __get__(obj,typ=None) instead of __get__(obj,typ) Shalabh Chaturvedi Python 2 02-20-2004 08:26 PM
difference between Convert.ToString(obj) and CType(obj, String) Mark Kamoski ASP .Net 3 08-08-2003 11:09 PM



Advertisments