473,395 Members | 1,578 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,395 software developers and data experts.

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

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 2245
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.
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.comwrote:
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);
}

Feb 9 '07 #5
gert wrote:
On Feb 9, 1:30 am, Ian Collins <ian-n...@hotmail.comwrote:
>>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...@gmail.comwrote:
On Feb 9, 1:30 am, Ian Collins <ian-n...@hotmail.comwrote:
<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
Feb 9 '07 #8
gert <ge**********@gmail.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",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.
Feb 9 '07 #9
On Feb 9, 2:41 am, Ian Collins <ian-n...@hotmail.comwrote:
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
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**********@gmail.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)gmail.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**********@gmail.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)gmail.com | don't, I need to know. Flames welcome.
Feb 9 '07 #14
On Feb 9, 9:41 pm, Christopher Benson-Manica
<a...@norge.freeshell.orgwrote:
gert <gert.cuyk...@gmail.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**********@gmail.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**********@gmail.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...@gmail.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.data);

}

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...@btinternet.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...@gmail.com" <fdmfdm...@gmail.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
On 9 Feb 2007 08:48:12 -0800, in comp.lang.c , "gert"
<ge**********@gmail.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.
No. C has a different paradigm, trying to write python using C syntax
as bad an idea as trying using German sentence structure English to
write is.
>Or are there other recommended methods to make a program.
I recommend learning C as a language on its own, and trying to forget
how you might do something in a different language. It will only
confuse you and lead to very inefficient code.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 9 '07 #21
On 9 Feb 2007 13:30:25 -0800, in comp.lang.c , "gert"
<ge**********@gmail.comwrote:
>But you have to admit it looks nice this way not ?
Personally, I think it looks messy, overcomplicated and prone to
errors. C is not C++ or python or Java. I strongly recommend you write
C like C.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 10 '07 #22
On Fri, 09 Feb 2007 14:43:45 +1300, Ian Collins wrote:
>Ian Collins wrote:
>int main(void) {
Obj* obj = malloc( sizeof obj );
Oops, malloc( sizeof *obj );
Why malloc? Why a memory leak?

Best regards,
Roland Pibinger
Feb 10 '07 #23
On Feb 10, 12:55 pm, rpbg...@yahoo.com (Roland Pibinger) wrote:
Why malloc? Why a memory leak?

Best regards,
Roland Pibinger
Can you collaborated on the memory leak part please ?
How would you do it ?
Feb 10 '07 #24
On Sat, 10 Feb 2007 11:55:04 GMT, in comp.lang.c , rp*****@yahoo.com
(Roland Pibinger) wrote:
>On Fri, 09 Feb 2007 14:43:45 +1300, Ian Collins wrote:
>>Ian Collins wrote:
>>int main(void) {
Obj* obj = malloc( sizeof obj );
Oops, malloc( sizeof *obj );

Why malloc?
In C, what else would you use to allocate memory than one of the
*alloc family?
>Why a memory leak?
What memory leak?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 10 '07 #25
gert wrote:
rpbg...@yahoo.com (Roland Pibinger) wrote:
>Why malloc? Why a memory leak?

Can you collaborated on the memory leak part please ?
How would you do it ?
void leak(size_t howmuch) {
malloc(howmuch);
}

will do it nicely, provided you #include <stdlib.h>

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 10 '07 #26
On Sat, 10 Feb 2007 18:55:17 +0000, Mark McIntyre wrote:
>On Sat, 10 Feb 2007 11:55:04 GMT, in comp.lang.c , rp*****@yahoo.com
(Roland Pibinger) wrote:
>>Why malloc?

In C, what else would you use to allocate memory than one of the
*alloc family?
What about putting the object on the stack? In C you do not
dynamically allocate memory unnecessarily.

Best wishes,
Roland Pibinger
Feb 10 '07 #27
Roland Pibinger wrote:
On Sat, 10 Feb 2007 18:55:17 +0000, Mark McIntyre wrote:
>rp*****@yahoo.com (Roland Pibinger) wrote:
>>Why malloc?

In C, what else would you use to allocate memory than one of the
*alloc family?

What about putting the object on the stack? In C you do not
dynamically allocate memory unnecessarily.
In standard C there is no such thing as a stack, and no mechanism
for placing run-time sized objects in automatic storage space.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 10 '07 #28
On Sat, 10 Feb 2007 21:55:56 GMT, in comp.lang.c , rp*****@yahoo.com
(Roland Pibinger) wrote:
>On Sat, 10 Feb 2007 18:55:17 +0000, Mark McIntyre wrote:
>>On Sat, 10 Feb 2007 11:55:04 GMT, in comp.lang.c , rp*****@yahoo.com
(Roland Pibinger) wrote:
>>>Why malloc?

In C, what else would you use to allocate memory than one of the
*alloc family?

What about putting the object on the stack?
I was referring to memory allocation, as opposed to automatics. The
latter aren't dynamic.
In C you do not dynamically allocate memory unnecessarily.
Why not?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 10 '07 #29
CBFalconer wrote, On 10/02/07 22:49:
Roland Pibinger wrote:
>On Sat, 10 Feb 2007 18:55:17 +0000, Mark McIntyre wrote:
>>rp*****@yahoo.com (Roland Pibinger) wrote:

Why malloc?
In C, what else would you use to allocate memory than one of the
*alloc family?
What about putting the object on the stack? In C you do not
dynamically allocate memory unnecessarily.

In standard C there is no such thing as a stack, and no mechanism
for placing run-time sized objects in automatic storage space.
Unless you can find a suitable C99 implementation and use VLAs.
--
Flash Gordon
Feb 11 '07 #30
CBFalconer <cb********@yahoo.comwrites:
Roland Pibinger wrote:
[...]
>What about putting the object on the stack? In C you do not
dynamically allocate memory unnecessarily.

In standard C there is no such thing as a stack,
True.
and no mechanism
for placing run-time sized objects in automatic storage space.
VLAs (new in C99).

--
Keith Thompson (The_Other_Keith) 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.
Feb 11 '07 #31
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>Roland Pibinger wrote:
[...]
>>What about putting the object on the stack? In C you do not
dynamically allocate memory unnecessarily.

In standard C there is no such thing as a stack,

True.
> and no mechanism
for placing run-time sized objects in automatic storage space.

VLAs (new in C99).
Always forget about those. I have yet to use them.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 11 '07 #32
On Sat, 10 Feb 2007 17:49:18 -0500, CBFalconer wrote:
>Roland Pibinger wrote:
>What about putting the object on the stack? In C you do not
dynamically allocate memory unnecessarily.

In standard C there is no such thing as a stack,
Ok, then put them on the "stack" and everything becomes clear.
>and no mechanism
for placing run-time sized objects in automatic storage space.
except that there is no run-time sized object in the above example.

Best regards,
Roland Pibinger
Feb 11 '07 #33
On Feb 10, 9:21 am, "gert" <gert.cuyk...@gmail.comwrote:
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 ?
Not sure why anyone suggested that code. Better is:
Obj myobj; /* right amount of memory is allocated */
myobj.value = 0;
myobj.add = add;
myobj.add(&myobj, 42);
printf("%d\n", myobj.value);
return 0;

In fact you could even replace the first 3 lines with:
Obj myobj = { 0, &add };

Feb 11 '07 #34
On Sun, 11 Feb 2007 11:17:03 GMT, in comp.lang.c , rp*****@yahoo.com
(Roland Pibinger) wrote:
>On Sat, 10 Feb 2007 17:49:18 -0500, CBFalconer wrote:
>>Roland Pibinger wrote:
>>What about putting the object on the stack? In C you do not
dynamically allocate memory unnecessarily.

In standard C there is no such thing as a stack,

Ok, then put them on the "stack" and everything becomes clear.
Not really, you just put the word in quote marks. CBF's point was that
C doesn't require a stack. It would be more accurate to say one was
putting the object into automatic storage.
>>and no mechanism
for placing run-time sized objects in automatic storage space.

except that there is no run-time sized object in the above example.
Sure, in that specific instance the size was a constant, but it need
not have been.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 11 '07 #35
On Feb 11, 1:15 pm, "Old Wolf" <oldw...@inspire.net.nzwrote:
On Feb 10, 9:21 am, "gert" <gert.cuyk...@gmail.comwrote:
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 ?

Not sure why anyone suggested that code. Better is:
Obj myobj; /* right amount of memory is allocated */
myobj.value = 0;
myobj.add = add;
myobj.add(&myobj, 42);
printf("%d\n", myobj.value);
return 0;

In fact you could even replace the first 3 lines with:
Obj myobj = { 0, &add };
Thanks :) I modified again this time i a cgi example

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

//////////////////atributes//////////////
typedef struct self{
char* xml;
int (*open)(struct self*);
} obj;

//////////////////methodes/////////////////
int session_read(obj* this){
this->xml="<xml>server,text,localhost,user,text,root,pa ssword,text,</
xml>";
return 0;
}

///////////////////constructer/////////////////
obj Obj(void){
obj new = { 0, &session_read };
return new;
}

int main (int argc, char** argv){
obj doc=Obj();
doc.open(&h);
printf("Content-Type: text/xml; charset=utf-8"
"\r\n"
"\r\n"
"%s"
"\n"
,doc.xml);
return 0;
}

Any memory leaks in this example ?
Feb 11 '07 #36
On Feb 11, 4:19 pm, "gert" <gert.cuyk...@gmail.comwrote:
doc.open(&h);
needs to be doc.open(&doc)
Feb 11 '07 #37
On 11 Feb 2007 07:19:37 -0800, in comp.lang.c , "gert"
<ge**********@gmail.comwrote:
>Thanks :) I modified again this time i a cgi example
If you want an OO language, why not use C++ ? C is not designed for
this, and while you can indeed do it, why reinvent the wheel?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 11 '07 #38
On Feb 11, 10:49 pm, Mark McIntyre <markmcint...@spamcop.netwrote:
On 11 Feb 2007 07:19:37 -0800, in comp.lang.c , "gert"

<gert.cuyk...@gmail.comwrote:
Thanks :) I modified again this time i a cgi example

If you want an OO language, why not use C++ ? C is not designed for
this, and while you can indeed do it, why reinvent the wheel?
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Because then i know how the wheel works, trying to do everything in c
is frustrating but you defenatly learn allot about how your pc works.
If the sky was the limit , c would be the sky.

Feb 12 '07 #39
On Feb 12, 4:19 am, "gert" <gert.cuyk...@gmail.comwrote:
#include <stdio.h>
#include <stdlib.h>

//////////////////atributes//////////////
typedef struct self{
char* xml;
int (*open)(struct self*);

} obj;

//////////////////methodes/////////////////
int session_read(obj* this){
this->xml="<xml>server,text,localhost,user,text,root,pa ssword,text,</
xml>";
return 0;

}

///////////////////constructer/////////////////
obj Obj(void){
obj new = { 0, &session_read };
return new;

}

int main (int argc, char** argv){
obj doc=Obj();
doc.open(&h);
printf("Content-Type: text/xml; charset=utf-8"
"\r\n"
"\r\n"
"%s"
"\n"
,doc.xml);
return 0;

}

Any memory leaks in this example ?
Looks good to me, apart from 'h' being 'doc' of course.

But I would give better names to 'obj' and 'self' and 'Obj',
as well as 'xml'. You should be able to have some idea of
what an identifier is identifying, based on its name. In fact
it is good to have a casing-convention too - "obj doc" looks
odd. Personally I use all-lower-case for object names and
function names, and all-upper-case for typedef names; another
popular style is to use lower case with "_t" on the end to
indicate typedef names.

Also, don't program this way just for the sake of it -- if
your runtime structure does not require various instances
of 'obj' that have different 'open' functions then it would
be much simpler to not have the 'open' function pointer. You
can't encapsulate in C like you can in OO languages.

Finally, if you ever port this code to Windows then it will
malfunction because the standard output stream is opened
in text mode, which means that "\n" characters will all be
translated to "\r\n" as they are printed. You might like to
make a note in the source code to that effect.

Feb 12 '07 #40
On 11 Feb 2007 16:16:55 -0800, in comp.lang.c , "gert"
<ge**********@gmail.comwrote:
>On Feb 11, 10:49 pm, Mark McIntyre <markmcint...@spamcop.netwrote:
>On 11 Feb 2007 07:19:37 -0800, in comp.lang.c , "gert"

<gert.cuyk...@gmail.comwrote:
>Thanks :) I modified again this time i a cgi example

If you want an OO language, why not use C++ ? C is not designed for
this, and while you can indeed do it, why reinvent the wheel?

Because then i know how the wheel works, trying to do everything in c
is frustrating but you defenatly learn allot about how your pc works.
Fine, but before you try programming complicated stuff in C, why not
learn to programme easy stuff first? When you're learning to drive, do
you jump into a huge lorry and just fiddle with the controls?


--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Feb 12 '07 #41

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

Similar topics

17
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...
4
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...
5
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"....
5
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...
54
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
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...
3
by: John Turner | last post by:
typedef void (*vfp)(); typedef vfp (*fp)(); static fp hello() { printf("Hello.\n"); return (fp)&hello; } main(){
3
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...
5
by: robbiesmith79 | last post by:
Hey fellow nerds, Take this code for example: class hello { function world() { echo "Hello"; } }
1
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...
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: 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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...

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.