Computer Programming
ES 409
Pankaj Debbarma, Assistant Professor
Deptt. of Computer Science & Engg.,
TIT, Narsingarh
Structure Initialization
main()
{
struct
{
int weight;
float height;
}
student = (60, 180.75);
. . . . .
. . . . .
}
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
2
A structure variable
can be initialized at
compile time
Structure Initialization
main()
{
struct
{
int weight;
float height;
}
student = (60, 180.75);
. . . . .
. . . . .
}
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
3
This assigns 60 to
student.weight and
180.75 to
student.height
Structure Initialization
main()
{
struct st_record
{
int weight;
float height;
};
struct st_record student1 = (60, 180.75);
struct st_record student2 = (53, 170.60);
. . . . .
. . . . .
}
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
4
Two structure
variables can be also
initialized
Structure Initialization
struct st_record
{
int weight;
float height;
} student1 = (60, 180.75);
main()
{
struct st_record student2 = (53, 170.60);
. . . . .
. . . . .
}
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
5
Another method is to
initialize a structure
variable outside the
function
Structure Initialization
1. We cannot initialize individual members inside
the structure template.
2. The order of values enclosed in braces must
match the order of members in the structure
definition.
3. It is permitted to have a partial initialization.
4. The uninitialized members will be assigned
default values as follows:
• Zero for integer and floating point numbers
• ‘0’ for characters and strings
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
6
Structure Initialization
• Two variables of the same structure type can
be copied the same way as ordinary variables.
person1 = person2;
person2 = person1;
• C does not permit any logical operations on
structure variables.
person1 == person2
person1 != person2
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
7
Accessing members
Consider the following structure:
typedef struct
{
int x;
int y;
} VECTOR;
VECTOR v, *ptr;
ptr = &v;
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
8
Accessing members
The identifier ptr is known as pointer that has
been assigned the address of the structure
variable v. Now the members can be accessed in
the following three ways:
• using dot notation : v.x
• using indirection notation : (*ptr).x
• using selection notation : ptr  x
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
9
Pointers and Structures
struct inventory
{
char name[30];
int number;
float price;
} product[2], *ptr;
This statement declares product as an array of two
elements, each of the type struct inventory and ptr as a
pointer to data objects of the type struct inventory.
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
10
Pointers and Structures
The assignment
ptr = product;
would assign the address of the zeroth element
of product to ptr. That is, the pointer ptr will
now point to product[0]. Its member can be
accessed using
ptr  name
ptr  number
ptr  price
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
11
Structures and Functions
There are three methods by which values of a
structure can be transferred from one function
to another.
1. Pass each member of the structure as an
actual argument of the function call.
2. Passing a copy of the entire structure to the
called function.
3. The address location of the structure is
passed to the called function.
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
12
Unions
union item
{
int m;
float x;
char c;
} code;
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
13
Unions
During accessing, we should make sure that we
are accessing the member whose value is
currently stored. For example, the statements
such as
code.m = 379;
code.x = 7859.36
printf(“%d”, code.m)
would produce erroneous output
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
14
Unions
Unions may be initialized when the variable is
declared. But, unlike structures, it can be
initialized only with a value of the same type as
the first union member.
union item abc = {100};
is valid but the declaration
union item abc = {10.75};
is invalid
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
15
Size of Structures
The expression
sizeof(struct x)
will evaluate the number of bytes required to hold
all the members of the structure x. If y is a simple
structure variable of type struct x, then
sizeof(y);
would also give the same answer. However, if y is an
array variable of type struct x, then it would give
the total number of bytes the array y requires.
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
16
Size of Structures
This kind of information would be useful to
determine the number of records in a database.
For example, the expression
sizeof(y) / sizeof(x)
would give the number of elements in the array
y.
Computer Programming –
Pankaj Debbarma, TIT, Narsingarh
17
CP02-Structure and Union.pptx

CP02-Structure and Union.pptx

  • 1.
    Computer Programming ES 409 PankajDebbarma, Assistant Professor Deptt. of Computer Science & Engg., TIT, Narsingarh
  • 2.
    Structure Initialization main() { struct { int weight; floatheight; } student = (60, 180.75); . . . . . . . . . . } Computer Programming – Pankaj Debbarma, TIT, Narsingarh 2 A structure variable can be initialized at compile time
  • 3.
    Structure Initialization main() { struct { int weight; floatheight; } student = (60, 180.75); . . . . . . . . . . } Computer Programming – Pankaj Debbarma, TIT, Narsingarh 3 This assigns 60 to student.weight and 180.75 to student.height
  • 4.
    Structure Initialization main() { struct st_record { intweight; float height; }; struct st_record student1 = (60, 180.75); struct st_record student2 = (53, 170.60); . . . . . . . . . . } Computer Programming – Pankaj Debbarma, TIT, Narsingarh 4 Two structure variables can be also initialized
  • 5.
    Structure Initialization struct st_record { intweight; float height; } student1 = (60, 180.75); main() { struct st_record student2 = (53, 170.60); . . . . . . . . . . } Computer Programming – Pankaj Debbarma, TIT, Narsingarh 5 Another method is to initialize a structure variable outside the function
  • 6.
    Structure Initialization 1. Wecannot initialize individual members inside the structure template. 2. The order of values enclosed in braces must match the order of members in the structure definition. 3. It is permitted to have a partial initialization. 4. The uninitialized members will be assigned default values as follows: • Zero for integer and floating point numbers • ‘0’ for characters and strings Computer Programming – Pankaj Debbarma, TIT, Narsingarh 6
  • 7.
    Structure Initialization • Twovariables of the same structure type can be copied the same way as ordinary variables. person1 = person2; person2 = person1; • C does not permit any logical operations on structure variables. person1 == person2 person1 != person2 Computer Programming – Pankaj Debbarma, TIT, Narsingarh 7
  • 8.
    Accessing members Consider thefollowing structure: typedef struct { int x; int y; } VECTOR; VECTOR v, *ptr; ptr = &v; Computer Programming – Pankaj Debbarma, TIT, Narsingarh 8
  • 9.
    Accessing members The identifierptr is known as pointer that has been assigned the address of the structure variable v. Now the members can be accessed in the following three ways: • using dot notation : v.x • using indirection notation : (*ptr).x • using selection notation : ptr  x Computer Programming – Pankaj Debbarma, TIT, Narsingarh 9
  • 10.
    Pointers and Structures structinventory { char name[30]; int number; float price; } product[2], *ptr; This statement declares product as an array of two elements, each of the type struct inventory and ptr as a pointer to data objects of the type struct inventory. Computer Programming – Pankaj Debbarma, TIT, Narsingarh 10
  • 11.
    Pointers and Structures Theassignment ptr = product; would assign the address of the zeroth element of product to ptr. That is, the pointer ptr will now point to product[0]. Its member can be accessed using ptr  name ptr  number ptr  price Computer Programming – Pankaj Debbarma, TIT, Narsingarh 11
  • 12.
    Structures and Functions Thereare three methods by which values of a structure can be transferred from one function to another. 1. Pass each member of the structure as an actual argument of the function call. 2. Passing a copy of the entire structure to the called function. 3. The address location of the structure is passed to the called function. Computer Programming – Pankaj Debbarma, TIT, Narsingarh 12
  • 13.
    Unions union item { int m; floatx; char c; } code; Computer Programming – Pankaj Debbarma, TIT, Narsingarh 13
  • 14.
    Unions During accessing, weshould make sure that we are accessing the member whose value is currently stored. For example, the statements such as code.m = 379; code.x = 7859.36 printf(“%d”, code.m) would produce erroneous output Computer Programming – Pankaj Debbarma, TIT, Narsingarh 14
  • 15.
    Unions Unions may beinitialized when the variable is declared. But, unlike structures, it can be initialized only with a value of the same type as the first union member. union item abc = {100}; is valid but the declaration union item abc = {10.75}; is invalid Computer Programming – Pankaj Debbarma, TIT, Narsingarh 15
  • 16.
    Size of Structures Theexpression sizeof(struct x) will evaluate the number of bytes required to hold all the members of the structure x. If y is a simple structure variable of type struct x, then sizeof(y); would also give the same answer. However, if y is an array variable of type struct x, then it would give the total number of bytes the array y requires. Computer Programming – Pankaj Debbarma, TIT, Narsingarh 16
  • 17.
    Size of Structures Thiskind of information would be useful to determine the number of records in a database. For example, the expression sizeof(y) / sizeof(x) would give the number of elements in the array y. Computer Programming – Pankaj Debbarma, TIT, Narsingarh 17