0

Can we use the word "object" for data / functions in e.g. C (or Pascal) which doesn't really have objects? In C, what is an object? A data structure? A named memory area? I spoke to my collegue about "objects" in C and he said that there weren't any but in the text I study about C the word object is used as a generalization of either a function or data.

For example, if you in C declare a union you can say that you have declared place for an object that can be either a function or data.

The ADT defined as "Object" looks like this in C

typedef enum { Integral, Real } Kind;
typedef struct {
  Kind type;
  union {
    double rvalue;
    int ivalue;
  } data;
} Object;

Thanks for any reply

2
  • 2
    A C object cannot contain a function (though it can contain a pointer to a function). May 28, 2012 at 23:24
  • Can you force it? Sure, but the real question is why would you want to? If you want an Object Oriented Language then there are a lot to pick from (C++, Java, C#, Smalltalk, Ruby etc), but programming in against the model of the language is just going to drive you crazy
    – Zachary K
    May 29, 2012 at 7:08

3 Answers 3

13

The C standard defines an "object" as a "region of data storage in the execution environment, the contents of which can represent values" (N1570 3.15).

The C++ standard similarly defines an "object" as "a region of storage" (N3337 1.8p1).

The Pascal standard doesn't appear to have a definition of "object", but it does use the word once, in a sense that appears to be similar to the C and C++ definitions.

Other languages may or may not have their own definitions.

None of the definitions I've cited is related to concepts of "object-oriented" programming; C++ has object-oriented features, but the C++ standard doesn't use the word "object" to describe them. Of course it's very common to use the word "object" in an "object-oriented" sense -- and there's nothing wrong with that, as long as you're clear about which meaning you're using.

"Object" is a common English word; see, for example, http://dictionary.reference.com/browse/object. Certain technical fields have established more specific meanings for it, for convenience in discussing certain topics. These meanings are not necessarily consistent with each other.

1
  • 1
    Instant +1 for giving a link to the standard. Argue on this one!
    – K.Steff
    May 28, 2012 at 23:25
4

An object consist of a mixture of code and data, which in C can be represented by using a struct and add a function pointer for each method in the object (which in turn also needs to have a reference to the object itself (this) passed in as one of the parameters).

The hard part is then setting up the struct as the language doesn't help much, so you need some kind of convention to do it manually. That said, when set up properly you can emulate all the fancy stuff like inheritance and polymorphism as it eventually just end up with a set of function pointers in each struct anyway, and then you actually have objects.

To clearly communicate to others that this is a construction not present in the core language, I would suggest calling it "emulated objects".

2
  • Except that the C standard already has a consistent definition of the word "object" that has nothing to do with object-oriented programming. May 28, 2012 at 23:26
  • @KeithThompson did not know that. Been 15 years since I wrote programs in C.
    – user1249
    May 28, 2012 at 23:29
-5

EDIT:

As I see that nobody understood my answer, let me clarify. I don't say you can't have objects in C, as I explained below, you can have objects in even Assembly.

I said "You can't call structs or unions as objects. For example structs and unions does not support Inheritance or Polymorphism." to answer the original question.

There is nothing wrong in this answer. Objects are generally associated with Object Oriented Programming, so an object needs to support OOP principles. That's as simple as that.

Saying that "I'm smart enough to create objects even in C" does not help as an answer.

11
  • 4
    -1 : You can write OO code in C, it is entirely possible write objects that exhibit Inheritance or Polymorphism characteristics. The original C++ was a pre-processor that generated C code. I am no way suggesting you should -hand crafted jump tables and such like are not for the faint of heart.
    – mattnz
    May 28, 2012 at 22:47
  • 1
    @mattnz: That is just meaningless. Then you can call Assembly an Object Oriented language since C++ and C is converted to Assembly code anyway. Then there is no reason to say that a language supports OOP. May 28, 2012 at 22:58
  • 1
    @Mert I don't think you're right: you are assuming objects exist only in the object-oriented world. Gosh, objects, you're not the center of the universe
    – K.Steff
    May 28, 2012 at 23:23
  • 2
    @mattnz It's a fun exercise to take a simple C++ program and rewrite it as "object oriented C". You do by hand what C++ does for you. This will give you a deeper appreciation of how C++ actually works internally. It is not as hard as you think. A "Class" is just a struct that has function pointers laid out a certain way, pointing to functions that all take a pointer to the struct called "this". May 28, 2012 at 23:34
  • 3
    @Mert It's true C is not object-oriented, but that's not the same as claiming "you can't" have objects in it. You can have objects in a non-OO language; whether it's a good idea is another story!
    – Andres F.
    May 29, 2012 at 0:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.