A member of a structure may be either a simple or compound type. A simple
member is one that is of any of the built-in data types, such as integer or character.
You have already seen one type of compound element: the character arrays used in
addr. Other compound data types include one-dimensional and multidimensional
arrays of the other data types and structures.
A member of a structure that is an array is treated as you might expect from
the earlier examples. For example, consider this structure:
struct x {
int a[10][10]; /* 10 x 10 array of ints */
float b;
} y;
To reference integer 3,7 in a of structure y, write
y.a[3][7]
When a structure is a member of another structure, it is called a nested structure.
For example, the structure address is nested inside emp in this example:
struct emp {
struct addr address; /* nested structure */
float wage;
} worker;
Here, structure emp has been defined as having two members. The first is a structure
of type addr, which contains an employee's address. The other is wage, which holds
the employee's wage. The following code fragment assigns 93456 to the zip element
of address.
worker.address.zip = 93456;
As you can see, the members of each structure are referenced from outermost to
innermost. Standard C specifies that structures may be nested to at least 15 levels.
Standard C++ suggests that at least 256 levels of nesting be allowed.
C C++ Source Codes, C C++ Practical examples ,C C++ tutorial ,Your best resource for Learning C and C++
Showing posts with label within. Show all posts
Showing posts with label within. Show all posts
Monday, October 4, 2010
Tuesday, September 21, 2010
Declaring Variables within Selection and Iteration Statements
In C++ (but not C), it is possible to declare a variable within the conditional expression
of an if or switch, within the conditional expression of a while loop, or within the
initialization portion of a for loop. A variable declared in one of these places has its
scope limited to the block of code controlled by that statement. For example, a variable
declared within a for loop will be local to that loop.
Here is an example that declares a variable within the initialization portion of a
for loop:
/* i is local to for loop; j is known outside loop. */
int j;
for(int i = 0; i<10; i++)
j = i * i;
/* i = 10; // *** Error *** -- i not known here! */
Here, i is declared within the initialization portion of the for and is used to control the
loop. Outside the loop, i is unknown.
Since often a loop control variable in a for is needed only by that loop, the
declaration of the variable in the initialization portion of the for is becoming common
practice. Remember, however, that this is not supported by C.
Whether a variable declared within the initialization portion of a for loop is local to
that loop has changed over time. Originally, the variable was available after the for.
However, Standard C++ restricts the variable to the scope of the for loop.
If your compiler fully complies with Standard C++, then you can also declare a
variable within any conditional expression, such as those used by the if or a while. For
example, this fragment,
if(int x = 20) {
x = x - y;
of an if or switch, within the conditional expression of a while loop, or within the
initialization portion of a for loop. A variable declared in one of these places has its
scope limited to the block of code controlled by that statement. For example, a variable
declared within a for loop will be local to that loop.
Here is an example that declares a variable within the initialization portion of a
for loop:
/* i is local to for loop; j is known outside loop. */
int j;
for(int i = 0; i<10; i++)
j = i * i;
/* i = 10; // *** Error *** -- i not known here! */
Here, i is declared within the initialization portion of the for and is used to control the
loop. Outside the loop, i is unknown.
Since often a loop control variable in a for is needed only by that loop, the
declaration of the variable in the initialization portion of the for is becoming common
practice. Remember, however, that this is not supported by C.
Whether a variable declared within the initialization portion of a for loop is local to
that loop has changed over time. Originally, the variable was available after the for.
However, Standard C++ restricts the variable to the scope of the for loop.
If your compiler fully complies with Standard C++, then you can also declare a
variable within any conditional expression, such as those used by the if or a while. For
example, this fragment,
if(int x = 20) {
x = x - y;
if(x>10) y = 0;
}
declares x and assigns it the value 20. Since this is a true value, the target of the if
executes. Variables declared within a conditional statement have their scope limited
to the block of code controlled by that statement. Thus, in this case, x is not known
outside the if. Frankly, not all programmers believe that declaring variables within
conditional statements is good practice, and this technique will not be used in
this book.
}
declares x and assigns it the value 20. Since this is a true value, the target of the if
executes. Variables declared within a conditional statement have their scope limited
to the block of code controlled by that statement. Thus, in this case, x is not known
outside the if. Frankly, not all programmers believe that declaring variables within
conditional statements is good practice, and this technique will not be used in
this book.
Subscribe to:
Posts (Atom)