pointer to *this

L

liorlew

Hello,

I am currently programming with C++, but now I will have to do some
things in C. my question is: "can I get a pointer to a struct?
something like *this". I am using pointer to functions but it wont do
me any good if I need to add a pointer to the struct on each call.

thanks
Lior
 
J

jacob navia

(e-mail address removed) a écrit :
Hello,

I am currently programming with C++, but now I will have to do some
things in C. my question is: "can I get a pointer to a struct?
something like *this". I am using pointer to functions but it wont do
me any good if I need to add a pointer to the struct on each call.

thanks
Lior

You will have to do it anyway since C doesn't support the
implicit "this" pointer and method calls.
 
J

Jens Marder

Hello,

I am currently programming with C++, but now I will have to do some
things in C. my question is: "can I get a pointer to a struct?
something like *this". I am using pointer to functions but it wont do
me any good if I need to add a pointer to the struct on each call.

thanks
Lior
In C++: Yes. In C: No. No objects, no *this
 
F

Frederick Gotham

I am currently programming with C++, but now I will have to do some
things in C. my question is: "can I get a pointer to a struct?
something like *this". I am using pointer to functions but it wont do
me any good if I need to add a pointer to the struct on each call.


C++:

class Arb {
public:

int i;

void SomeFunc() { i = 7; }

void OtherFunc() const {}
};

int main()
{
Arb obj;

obj.SomeFunc();
obj.OtherFunc();

Arb const cobj;

cobj.SomeFunc();
/* Compile ERROR: const violation */

cobj.OtherFunc();
}


C:

struct Arb {
int i;
};

typedef struct Arb Arb;
/* So we don't have to prepend "struct" */

void SomeFunc(Arb *const this) { this->i = 7; }

void OtherFunc(Arb const *const this) {}

int main(void)
{
Arb obj;

SomeFunc(&obj);
OtherFunc(&obj);

Arb const cobj;

SomeFunc(&cobj);
/* Compile ERROR: const violation */

OtherFunc(&cobj);
}
 
K

Keith Thompson

Jens Marder said:
In C++: Yes. In C: No. No objects, no *this

A point of terminology: Both C and C++ have objects, and both
standards define the word "object" in pretty much the same way. (I
haven't checked the C++ standard to see if its definition is
identical.)

An "object" is simply a "region of data storage in the execution
environment, the contents of which can represent values"; for example,
any declared variable is an "object".

C is not directly "object-oriented", so it doesn't have "objects" in
that sense (though you can simulate "object-oriented" programming in
C).
 
K

Keith Thompson

I am currently programming with C++, but now I will have to do some
things in C. my question is: "can I get a pointer to a struct?
something like *this". I am using pointer to functions but it wont do
me any good if I need to add a pointer to the struct on each call.

Yes, you can get a pointer to a struct, simply by taking its address:

struct foo obj;
struct foo *ptr = &obj;

Any arguments to a C function have to be explicit. If you want your
function to be able to see a particular struct object, you need to
pass either the value or the address of the object to the function.

You can have a function pointer as a member of a struct, but calling
the function via that function pointer doesn't give the function any
special access to the struct object.

More briefly: there is no "this" in C, unless you do it yourself.
 
J

Jens Marder

Keith Thompson said:
A point of terminology: Both C and C++ have objects, and both
standards define the word "object" in pretty much the same way. (I
haven't checked the C++ standard to see if its definition is
identical.)

An "object" is simply a "region of data storage in the execution
environment, the contents of which can represent values"; for example,
any declared variable is an "object".

C is not directly "object-oriented", so it doesn't have "objects" in
that sense (though you can simulate "object-oriented" programming in
C).

--
Keith Thompson (The_Other_Keith) (e-mail address removed)
<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.

Yes. you could simulate OOP with

void pointers
functions pointers
and structs with build-in "class-identifiers"

Did so in the early 90s with Modula-2.
That was horrible.
 
L

liorlew

hank you all for your answers, sorry for not replying earlier but I did
not have access to the net
 
L

liorlew

THanks for your answers, sorry for not replying earlier but I did not
had access to the net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,214
Latest member
JFrancisDavis

Latest Threads

Top