0

All of my initial programming experience has been in object-oriented languages (Java, Python). I am now learning C, and it seems that there are still things that resemble objects.

Say for example, a FILE pointer created with the standard C library. This is just a pointer to the memory location of a struct. Is this essentially the same thing as an object in OO languages?

There are existing questions asking about the difference between a struct and a class, but I'm asking about how a program uses those things. A struct is used or accessed via a pointer, while an object is a particular instance of a class. In this sense, it seems that a class is more general than a struct. Though I really only added this paragraph to prevent this questions from being marked as a duplicate, and it strays from my original question.

If a FILE pointer is, indeed, comparable to some FILE object in a different language, then where is the key difference between how that "thing" called FILE will be handled in a object-oriented language vs a non-object-oriented language. Seems like the line starts to blur.

pretzlstyle
  • 2,774
  • 5
  • 23
  • 40
  • A `struct` and an `object` are definitely different things. If they weren't, Objective-C and C++ would not be needed. ;) The answer would be far too long for here, though. – David Hoelzer Jan 19 '16 at 00:56
  • Look here: http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c Also, I'm supposing you meant `C++` instead of `C`, since C doesn't have classes. – AntonH Jan 19 '16 at 00:59
  • Answered here: http://stackoverflow.com/questions/4514582/whats-the-difference-between-an-object-and-a-struct-in-oop – David Hoelzer Jan 19 '16 at 00:59
  • "In General", well every language has a slightly different semantical meaning to what a struct or an object is. We could very easily get into the weeds just on that topic alone. Pick which language you care about. Is it C++? If so, we've answered that in a few places (linked above). If you're talking about C, now you're mixing apples and oranges, since C doesn't have classes. If you're talking about C's "Struct" vs. a higher level language's object, now you're really going to be confused, because you're jumping boundaries. – George Stocker Jan 19 '16 at 01:03
  • @jphollowed Okay, then I would like at the link I provided and David provided, which give a general answer but a lot of C and C++ specifics, but also echo George Stocker's answer that each language has nuances that we do not have the space to explain, and that are outside the purview of what Stack Overflow is for. That might be a better question for SuperUser website. – AntonH Jan 19 '16 at 01:07
  • Read the standard: http://port70.net/~nsz/c/c11/n1570.html#3.15 – too honest for this site Jan 19 '16 at 01:15
  • 1
    And to answer "Are structs objects?" you'll need to define what an object is. Otherwise this question is purely opinion-based. – user253751 Jan 19 '16 at 01:15
  • @jphollowed I mean the C standard *defines* the word "object" to basically mean any variable. So in C, any variable is an object. But like I said already twice now, that's probably not what you mean. – user253751 Jan 19 '16 at 01:29
  • 1
    @immibis: As the question is just tagged C, I'd say we should restrict it to the definition of the C standard. Otherwise it will become too broad. C does not support OOP, so any OO semantics are useless. I think FuZxxI provided an appropriate answer. – too honest for this site Jan 19 '16 at 01:32
  • Deleted comments so we could focus on the issue with why this question is on hold: Focus on a particular language's semantics; You can't use `object` the same way in C that you can in another language. You have to define precisely what you mean by object; you also have to define which language you care about. It gets way too broad to give you any meaningful answer once we start jumping from C to higher level languages. – George Stocker Jan 19 '16 at 01:36

1 Answers1

1

In the C programming language, an object is a “region of data storage in the execution environment, the contents of which can represent values” (cf ISO 9899:2011 §3.15). Almost everything is an object, including pointers, arrays, structures, and integers (but not functions).

This notion however is different from what you understand as an “object” in most object-oriented languages. Notably, objects in C don't have behaviour associated with them, don't have classes and don't have any guarantees whatsoever. There isn't even a guarantee that an object may represent any value at all. An object is only a bit of memory.

The typical API design pattern in procedural programming languages like C is that there is a set of functions (like fopen, fprintf, fclose, fwrite, etc.) and a set of structure types (like FILE) that collect data required for these functions. The association between these structures and the corresponding behaviour is made by passing structures to functions.

You can build all the things you have in an object-oriented language like that, including virtual function calls and classes, you just have to build all of this manually. I believe this is a strength of C as you are not forced into a certain program structure, you can apply the design pattern of object-orientation where appropriate and use other approaches where not.

fuz
  • 88,405
  • 25
  • 200
  • 352