In C, we use a struct to define a data type that represents the aggregate of two or more named variables known as members. The members of a struct need not be of the same type:
struct T {
int x;
double y;
char* z;
};
Once we have defined a structure, we can use it to create an instance of the type:
T obj;
We can access the members of a named object using the dot operator (.):
obj.x = 42;
obj.y = 3.14;
obj.z = "Hello world!";
If we hold a pointer to an object of the type, we use the pointer-to-member operator (->) instead:
void f (T* ptr) {
ptr->x = 42;
ptr->y = 3.14;
ptr->z = "Hello world!";
}
true or false
(*ptr).field or ptr->field
we can access and assign values of the members of a structure in a number of ways. as mentioned earlier, the members themselves are not variables .they should be linked to the structure variables in order to make them meaningful members . for example the word title , has no meaning whereas the phrase 'title of book3' .the link between a members and a variable is established using the members operator '.' which is also known as 'dot operator ' or ' period operator '.for example , book.priceis the variable representing the price of book1 and can be treated like any other ordinary variable
we can access and assign values of the members of a structure in a number of ways. as mentioned earlier, the members themselves are not variables .they should be linked to the structure variables in order to make them meaningful members . for example the word title , has no meaning whereas the phrase 'title of book3' .the link between a members and a variable is established using the members operator '.' which is also known as 'dot operator ' or ' period operator '.for example , book.priceis the variable representing the price of book1 and can be treated like any other ordinary variable
In class default members are private and in structure default members are public ,When ever you want to hide data from outside functions then you can use class.But in ANSI C we can hide data by using private access specifier.
All is very similar to the declaration on data structures, except that we can now include also functions and members, but also this new thing called access specifier. An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights that the members following them acquire
Structure padding is used to align the data members in a structure to memory addresses that are multiples of their size or the machine's word size. This helps in optimizing memory access and improving performance by reducing memory fragmentation and making data retrieval more efficient.
Public data members are akin to structure data members (which are public by default). Since they are public, they are fully exposed outside of the class. Data validation becomes impossible. Even if you define an interface, there's no requirement to use it since the members are readily available. If the members must be validated, do not assign them public access.
no, Parent class can not access the members of child class ,but child class can access members of parent class
Data encapsulation is enforced by restricting access to the class members. Access can be specified on a per-member basis, defaulting to private access for a class and public access for a struct. Private members are accessible to class members and to friends of the class. Protected members are the same as private members but are also accessible to derived class members. Public members are fully-accessible. Data members are typically declared private while interfaces are typically declared public or protected.
Non-static members are only accessible by instantiating an object of the class. However, access to those members is determined by the access specifier applied to each member. Private members are visible to all instances of the class (including other instances) and to friends of the class. Protected members are the same as private members but are also visible to derived classes. Public members are unrestricted.
In programming languages, the arrow symbol (->) typically denotes an access operator, used to access members of a structure or class. In some contexts, such as the arrow function syntax in JavaScript, it signifies a concise way to write anonymous functions.