Submitted by Anonymous (not verified) on Sun, 03/10/2013 - 21:25

    Q Why do you need structures?

    A In practice, you need to collect and group data items that are relevant but of different types. The structure data type provides a convenient way to aggregate those differently typed data items.

    Q Can you declare a structure and define a structure variable in a single statement?

    A Yes. You can put the struct keyword, a tag name, a list of declarations of structure members, and a variable name into a single statement to declare a structure and define a structure variable. Then, the structure can be identified by the tag name; the variable is of the struct data type of the tag name.

    Q How do you reference a structure member?

    A You can reference a structure member by prefixing the structure member's name with the structure variable name and a dot operator (.). If the structure is pointed to by a pointer, you can use the arrow operator (->), followed by the pointer name, to reference the structure member.

    Q Why is it more efficient to pass a pointer that refers to a structure to a function?

    A When an entire structure is passed to a function, a copy of the structure is made and saved in a temporary block of memory called the stack. After the copy is modified by the function, it has to be returned and written back to the storage that holds the original content of the structure. Passing a function with a pointer that points to a structure, on the other hand, simply passes the address of the structure to the function, not the entire copy of the structure. The function can then access the original memory location of the structure and modify the content held by the structure without duplicating the structure on the stack. Therefore, it's more efficient to pass a pointer of a structure than to

pass the structure itself to a function.
 

Related Items

Question and Answer

Question and Answer

    Q Which bit can be used as the sign bit in an integer?

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Measuring Data Sizes

Measuring Data Sizes

What Does x?y:z Mean?

What Does x?y:z Mean?

In C, ?: is called the conditional operator, which is the only operator that takes three operands. The general form of the conditional operator is

Using Shift Operators

Using Shift Operators

There are two shift operators in C. The >> operator shifts the bits of an operand to the right; the << operator shifts the bits to the left.

The general forms of the two shift operators are

x >> y