I need help with these question please. 1. What are struct tags in C? How are th
ID: 3681971 • Letter: I
Question
I need help with these question please.
1. What are struct tags in C? How are they related to type names? How did they
Change in C++?
2. Summarize the distinction between records and tuples in ML. How do these
Compare to the records of languages like C and Ada?
3. What is packing? What are its advantages and disadvantages?
4. Why might a compiler reorder the fields of a record? What problems might
This cause?
5. What is an array slice? For what purposes are slices useful?
6. What is the shape of an array?
7. What is a dope vector? What purpose does it serve?
8. Explain the difference between row-major and column-major layout for contiguously
Allocated arrays. Why does a programmer need to know which layout?
The compiler uses? Why do most language designers consider row-major layout
To be better?
9-Name three languages that provide particularly extensive support for character
Strings.
10. Discuss the advantages and disadvantages of the interoperability of pointers
And arrays in C.
Explanation / Answer
1)
Structures are a way of storing many different values in variables of potentially different types under the same name. This makes it a more modular program, which is easier to modify because its design makes things more compact. Structs are generally useful whenever a lot of data needs to be grouped together--for instance, they can be used to hold records from a database or to store information about contacts in an address book. In the contacts example, a struct could be used that would hold all of the information about a single contact--name, address, phone number, and so forth.
For example:
struct example {
int x;
};
struct example an_example; //Treating it like a normal variable type
an_example.x = 33; //How to access its members
is a tag. A tag by itself is not a type name. If it were, then C compilers would accept declarations such as:
But they don't. You must write the declarations as:
The combination of struct and s-in that order-is called an elaborated type specifier.
The names of unions and enumerations are also tags rather than types. For example:
Here,enum day is the elaborated type specifier.
For the most part, C does not permit different entities to have the same name in the same scope. For example, when these two declarations appear in the same scope:
the compiler will flag the second one as an error. But C treats tags differently than it does other identifiers. C compilers hold tags in a symbol table that's conceptually, if not physically, separate from the table that holds all other identifiers. Thus, it's possible for a C program to have both a tag and an another identifier with the same spelling in the same scope.
For example, C compilers will accept both:
in the same scope. They will even accept:
struct s s;
which declares object s of type struct s. Such declarations may not be good practice, but they are C.
Many programmers (including yours truly) prefer to think of struct tags as type names, so they define an alias for the tag using a typedef. For example, defining:
lets you use T in place of struct s, as in:
A program cannot use T as the name of both a type and an object (or a function or enumeration constant), as in:
T T; // error
This is good.
The tag name in a struct, union, or enum definition is optional. Many programmers fold the struct definition into the typedef and dispense with the tag altogether, as in:
This works well, except in self-referential structures containing pointers to structures of the same type. For example:
defines a struct called list_node, which contains, among other things, a pointer to another list_node. If you wrap the struct definition in a typedef and omit the tag, as in:
the compiler will complain because the declaration for member next refers to list_nodebefore list_node is declared.
With a self-referential struct, you have no choice but to declare a tag for the struct. If you prefer to use a typedef name thereafter, you must declare both the tag and the typedef. Many programmers follow naming conventions.
they changed TREENODE to Treenode
In C++, tags act just like typedef names, except that a program can declare an object, function, or enumerator with the same name and the same scope as a tag. In that case, the object, function, or enumerator name hides the tag name and the program can refer to the tag name only by using the keyword class, struct, union, or enum (as appropriate) in front of the tag name. Thus, a C program that contains both.
3)
Packing:
Preparation of product or commodity for proper storage and/or transportation. It may entail blocking, bracing, cushioning, marking, sealing, strapping, weather proofing, wrapping, etc.
advantages of packaging:
.Rising Standards of Health and Sanitation:
As the people are becoming health conscious they like to buy packed goods. The reason is that the chances of adulteration in such goods are minimised.
.Self-service Outlets:
Nowadays self-service retail shops are becoming very popular, particularly in big cities. Because of this, the role of sales assistants has gone to packaging.
. Innovational Opportunity:
With the increasing use of packaging more innovational opportunity becomes available in this area for the researchers.
. Product Differentiation:
Packaging is helpful in creating product differentiation. The colour, material and size of the package makes difference in the perception of the buyers about the quality of the product.
Disadvantages of packing
Cost
Landfill Impact
Production Footprint
5)
slice() method extracts a section of an array and returns a new array.
Example
Output
7)
In computer programming, a dope vector is a data structure used to hold information about a data object, e.g. an array, especially its memory layout.
A dope vector typically contains information about the type of array element, rank of an array, the extents of an array, and the stride of an array as well as a pointer to the block in memory containing the array elements.
It is often used in compilers to pass entire arrays between procedures in a high level language like Fortran.
The dope vector includes an identifier, a length, a parent address, and a next child address. The identifier is an assigned name and may be unused. The length is the amount of allocated storage to this vector from the end of the dope vector that contains data of use to the internal processes of the computer. This length is called the offset, span, or vector length. The parent and child references are absolute memory addresses, or register and offset settings to the parent or child depending on the type of computer.
Dope vectors are often managed internally by the operating system and allow the processor to allocate and de-allocate storage in specific segments as needed.
Dope vectors may also have a status bit that tells the system if they are active; if it is not active it can be reallocated when needed. Using this technology the computer can perform a more granular memory management.
8)
Explain the difference between row major and column major layout for Contiguously allocated arrays
Ans-
Arrays in most language implementations are stored in contiguous locations in memory. For multidimensional arrays, it still makes sense to put the first element of the array in the arrays first memory location. But which element comes next? There are two reasonable answers, called row-major and column-major order. In row- major order, consecutive locations in memory hold elements that differ by one in the final subscript (except at the ends of rows). A[2, 4], for example, is fol- lowed by A[2, 5]. In column-major order, consecutive locations hold elements that differ by one in the initial subscript: A[2, 4] is followed by A[3, 4]. The difference between row- and column-major layout can be important for programs that use nested loops to access all the elements of a large, multidi- mensional array. On modern machines the speed of such loops is often lim- ited by memory system performance, which depends heavily on the effective- ness of caching.
10)
Pointers and arrays are closely linked in C.
Consider the following declarations. Array names and pointers in C
int n;
int *a;
/* pointer to integer */
int b[10];
advantages 1.a = b;
/* make a point to the initial element of b */
2. n = a[3];
3. n = *(a+3);
/* equivalent to previous line */
4. n = b[3]; 5. n = *(b+3);
An unsubscripted array name in C is automatically converted to a pointer to the arrays first element in line 1. Lines 3 and 5 illustrate pointer arithmetic: given a pointer to an element of an array, the addition of an integer k produces a pointer to the element k positions later in the array The prefix * is a pointer dereference operator.
Pointer arithmetic is valid only within the bounds of a single array, but C compilers are not required to check this.
C allows pointers to be subtracted from one another or compared for ordering, provided that they refer to elements of the same array. The comparison pØ< q, for example, tests to
see if p refers to an element closer to the beginning of the array than the one referred to by q. The expression p - q returns the number of array positions that separate the elements to which p and q refer.
All arithmetic operations on pointers ,scale,their results as appropriate, based on the size of the referenced objects.
Disadvantages
Programmers need to be aware that the two are not the same, particularly in the context of variable declarations, which need to allocate space when elaborated.
The declaration of a pointer variable allocates space to hold a pointer, while the declaration of an array variable allocates space to hold the whole array
In the case of an array the declaration must specify a size for each dimension. Thus int *a[n], when elab-orated, will allocate space for n row pointers; int a[n][m] will allocate space for a two-dimensional array with contiguous layout.
For a twodimensional array with contiguous layout, the formal parameter may be declared as int a[ ][m] or int (*a)[m].
The size of the first dimension is irrelevant; all that is passed is a pointer, and C performs no dynamic checks to ensure that references are within the bounds of the array.
In most cases, sizeof can be evaluated at compile time; the principal exception occurs for variable-length arrays, whose shape is not known until elaboration time.