They both do entirely different things so the preferred option is the one that fulfils your needs.
A struct is used to combine two or more data types (members) into a single entity, where each member is stored and accessed separately within a contiguous block of memory. The size of a struct is equal to the total size of all its members, plus any padding for alignment purposes. A struct is not unlike a database record with two or more fields. However, to conserve memory and minimise padding, members must be declared in order of size, from largest to smallest.
A union is used to store a single data type that can be re-interpreted, not unlike a static cast. The largest member determines the overall size of the union and the individual members determine how the memory is to be interpreted. Since all members effectively refer to the same memory address, only one member of a union may be accessed at any one time. Changing the value of any one member affects all members.
Structs and unions can be combined both ways, so a struct may contain a union (allowing the same struct member to be re-interpreted) and a union may contain a struct. The latter form makes it possible for a union to contain a primitive data type, such as a 32-bit integer, as well as a struct member that contains two or more members that have a combined size of 32-bits, such as four separate 8-bit members (bytes). Changing the value of any one byte in the struct affects a different byte within the 32-bit integer because both the integer and the struct are mapped to the same address space.
12th one is more preferrable
Actually, there is a third step, call definition. Declaration is a statement to the compiler of what type an identifier is, definition is the allocation of memory for that identifier, and initialization is the assignment of an initial value to that identifier. Usually, declaration and definition are done together, but you can also add initialization in that step if desired. int a; /* declaration and definition */ a = 1; /* initialization */ int a = 1; /* declaration, definition, and initialization */ For the case of seperate declaration and definition, consider the struct... struct _mystruct { int a; }; /*declaration */ struct _mystruct mystruct; /* definition */ struct _mystruct { int a; } mystruct; /*declaration and definition */ Note: To be more precise: struct _mystruct; /* struct declaration */ struct _mystruct { int a; }; /* struct definition */ typedef struct _mystruct MYTYPE; /* type definition */ extern struct _mystruct mystructvar; /* variable declaration */ struct _mystruct mystructvar; /* variable definition */ struct _mystruct mystructvar = {7} ; /* variable definition with initialization */ struct _mystruct { int a; } mystruct; /* struct definition and variable definition */ extern struct _mystruct { int a; } mystruct; /* struct definition and variable declaration */
Yes, most preferable is the superlative form of the adjective preferable; the comparative is more preferable.
unions conserves memory space..The amount of space allocated for union is based on the member which requires largest memory space...so unions are useful for applications involving number of variables,where values need not be assigned to all elements at one time.AnswerIts used to save on space. For eg. you have a struct for storing information about insurance policy holders. But there are three types of insurances: Home, Life, Auto. Now basic details about policyholders such as name, address, etc is the same no matter what insurance you are talking about. But certain details are different. So by making a union policyinfo in this struct which is a diff internal struct for each of these policies you can save on space. When you are entering info about a Home policy, then you use the common fields AND only the struct for home insurance. You do not even fill/care about the other structs in the union. Upon storage only the Home struct will be stored, and the others will be discarded. Keep a variable in the main struct which indicates the type of insurance policy and so, which of the structs in the union you want to use. This way, you have just one big structure for all policyholders, instead of three -- it provides a common interface and saves on storage.===============The above answers are not correct. Unions do not save memory or storage space. Unions allow the same area of memory to be accessed as different data types. The insurance example above would not work.Unions are useful (often in embedded programming) when it is needed to refer to a data item as two or more different types: e.g. it may be used as an integer for math, but an array of bytes for a serial transmission or a checksum calculation.For memory preservation, since the space allocated for the union is as big as its biggest member.
Yes, it is preferable to use "preferable" in the afternoons as per language conventions. Remember to use it when expressing a choice or option that is more desirable or favorable.
Yes rear disc brakes is preferable because it has smoother, more controlled, more effective braking capabilities.
no they are not always preferable because sometimes you need more calories or someting
Some words that contain the letters 'struct' are:constructdeconstructdestructionindestructibleinfrastructureinstructobstructpreconstructedreconstructstructureunconstructeduninstructedunobstructedunstructured
In my opinion, iPhone is more preferable.
why is batching of aggregate by weight is preferable than batching by volume
It would be a user-defined type (UDT). In C, you would define a struct for it: typedef struct { char *name; unsigned int students; /* More fields */ } school;
The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color. Although it is possible to represent a point as a class, a struct is more efficient in some scenarios. For example, if you declare an array of 1000 Point objects, you will allocate additional memory for referencing each object. In this case, the struct is less expensive. When we create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized. It is an error to declare a default (parameterless) constructor for a struct. A default constructor is always provided to initialize the struct members to their default values. It is an error to initialize an instance field in a struct. There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class Object. A struct can implement interfaces, and it does that exactly as classes do. A struct is a value type, while a class is a reference type.