Structures
Structures
• Structures (also called structs) are a way to group several related variables
into one place. Each variable in the structure is known as a member of the
structure.
• Unlike an array, a structure can contain many different data types (int,
float, char, etc.).
Heterogeneous Structures
Collection of values of possibly different types.
Name the collection.
Name the components.
Example : Student record
Singhal
name "V Singhal"
rollno "00CS1001"
classtest 14
midterm 78
final 73
grade ‘B
Structure : terminology
A struct is a group of items (variables) which may be of
different types.
Each item is identified by its own identifier, each of
which is known as a member or field of the structure.
A struct is sometimes called a record or structure.
Structs are the basis of classes in C++ and Java.
#include <stdio.h> // Assign values to members of s1
s1.myNum = 13;
// Create a structure called myStructure s1.myLetter = 'B';
struct myStructure {
// Print values
int myNum; printf("My number: %d\n", s1.myNum);
char myLetter; printf("My letter: %c\n", s1.myLetter);
};
return 0;
int main() { }
// Create a structure variable of myStructure called s1
struct myStructure s1;
// Create different struct variables
struct myStructure s1;
struct myStructure s2;
s2.myNum = 20;
s2.myLetter = 'C';
#include <stdio.h>
#include <string.h>
struct myStructure {
int myNum;
char myLetter;
char myString[30]; // String
};
int main() {
struct myStructure s1;
return 0;
}
Simpler Syntax
// Create a structure
struct myStructure {
int myNum;
char myLetter;
char myString[30];
};
int main() {
// Create a structure variable and assign values to it
struct myStructure s1 = {13, 'B', "Some text"};
// Print values
printf("%d %c %s", s1.myNum, s1.myLetter, s1.myString);
return 0;
}
Typedef in C
• The typedef is a keyword used in C programming to provide some
meaningful names to the already existing variable in the C program.
• It behaves similarly as we define the alias for the commands. In short, we
can say that this keyword is used to redefine the name of an already existing
variable.
Syntax of typedef
typedef <existing_name> <alias_name>
By convention, uppercase letters are used for these definitions to remind the user that the type
BYTE b1, b2; name is really a symbolic abbreviation, but you can use lowercase, as follows −
typedef unsigned char byte;
Pointers to Structures
You can define pointers to structures in the same way as you define pointer to any other variable
−
struct Books *struct_pointer;
Now, you can store the address of a structure variable in the above defined pointer variable. To
find the address of a structure variable, place the '&'; operator before the structure's name as
follows −
struct_pointer = &Book1;
To access the members of a structure using a pointer to that structure, you must use the →
operator as follows −
struct_pointer->title;
Bit Fields
• Bit Fields are used to specify the length of the structure members in
bits.
• When we know the maximum length of the member, we can use bit
fields to specify the size and reduce memory consumption.
• The structure can be used to define the custom data types that
can be used to create some complex data types such as dates,
time, complex numbers, etc. which are not present in the
language.
• It can also be used in data organization where a large amount of
data can be stored in different fields.
• Structures are used to create data structures such as trees, linked
lists, etc.
• They can also be used for returning multiple values from a
function.
Limitations of C Structures