You mean you can not have the same name in the same scope.
It is routine to carry the same name such as i, j, count etc but they get restricted to
within a local scope as a local variable in a function or unit.
Not like there should be many but global variables obviously can't have the same name.
It is not a limitation. When C was designed there was not thought to be a need for such a feature. But you could still have variables and functions that were limited to specific source modules. And local variables inside functions were hidden from others automatically.
C is highly portable and simple language. But, because of some limitation of C, it is loosing fame.
The main reason behind is, it doesn't support object-oriented programming features. Means-
are not suported by C programming language, that's why C++ is developed.
In object oriented programming languages, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:
- A language mechanism for restricting direct access to some of the object's components.
- A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.
Opaque structs in C fit at least one of those definitions for abstraction. You could probably make the case that since you'd need to bundle the opaque struct with some subroutines to manipulate it that you're basically writing methods - the only difference is the class keyword and the lack of an implicit *this* pointer.
Sorry, I misread that. No you could not refer to the members if they are not defined in the header. But in most cases of writing pure C code this is not an issue. Both the caller and the provider need the definition of the struct in order to pass data between them.
Putting the definition in a header file only matters if you are trying to access the struct in separate source modules; and that has nothing to do with encapsulation. And yes, of course you could do what you suggest above, but it serves little purpose since you can still access the data directly, and thus break the pseudo encapsulation. In OOP languages the data can actually be hidden from the users of the class, in C it cannot.
Last Visit: 19-Oct-19 20:01 Last Update: 19-Oct-19 20:01