Structures, Unions and
Pointers
Structure
A Structure is a collection of one or more variables possibly of different data types, grouped together
under a single name for convenient handling.
Structure is a type of data structure in which each individual element differs in type. The elements of a
structure are called members.
Important points to be noted while declaring a structure:
A semicolon must follow the closing brace in the structure declaration.
Usually, the structure type declaration appears at the top of the source code file, before any
variables or functions are defined.
Initialization is not allowed within a structure.
Declaring a structure variable:
• Structure variable is declared using the keyword ‘struct’ followed by structure name and
structure elements name.
• All structure elements name are separated by commas. Structure variable declaration is
terminated with semicolon (;).
Define the structure with a declaration, initialization with an example program.
• Structure Declaration
To declare a structure, use the struct keyword followed by the structure name and member variables.
• Structure Initialization
1.At the time of declaration:
1.By assigning values later:
Create a struct variable with the name "s1":
struct myStructure {
int myNum;
char myLetter;
};
int main() {
struct myStructure s1;
return 0;
}
// Create a structure called myStructure
struct myStructure {
int myNum;
char myLetter;
};
int main() {
// Create a structure variable of myStructure
called s1
struct myStructure s1;
// Assign values to members of s1
s1.myNum = 13;
s1.myLetter = 'B';
// Print values
printf("My number: %d\n", s1.myNum);
printf("My letter: %c\n", s1.myLetter);
return 0;
}
What About Strings in Structures?
However, there is a solution for this! You can use the strcpy() function and assign the value
to s1.myString, like this
• // 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;
}
Copy Structures
Modify Values
If you want to change/modify a value, you can use the dot syntax (.). And to modify a string value, the strcpy()
function is useful again:
Program-1
Programs
• C program to create student details using structures
• C program to create employee details using structures
• C program to create books details using structures
ARRAY OF STRUCTURE
• Array is a collection of similar data types. In the same way, we also
define an array of structures.
ARRAYS WITHIN STRUCTURES
STRUCTURE WITHIN STRUCTURE (NESTED STRUCTURE)
UNION
• Union is a derived data type and it is declared like structure.
• The difference between union and structure is in terms of storage.
• In structure each member has its own storage location, whereas all the members of union use the same
location, although a union may contain many members of different types.
• When we use union the compiler allocates a piece of storage that is large enough to hold.
• A union is also declared by using the keyword union.
POINTERS
A pointer is a variable that holds a memory address.
This address is the location of another object (typically another variable) in memory. For example, if one
variable contains the address of another variable, the first variable is said to point to the second.
Generally, computer uses memory for storing instructions and values of the variables within the program, but the
pointers have memory addresses as their values.
The memory address is the location where program instructions and data are stored, Pointers can be used to
access and manipulate data stored in the memory
Features of Pointers
• Pointers are efficient in handling data and are associated with an array.
• Pointers are used for saving memory space.
• Pointers reduce the length and complexity of the program.
• Use of a pointer assigns the memory space and also releases it. It helps to make better use of the available
memory ( dynamic memory allocation).
• Since the pointer data manipulation is done with an address, the execution time is faster
• The two-dimensional and multi-dimensional array representation is easy with pointers
• Pointers allow for references to functions, which may facilitate passing a function as an argument to other
functions.
Declaring a Pointer Variable:
POINTER OPERATOR
To manipulate data items directly from a Memory Location, two operators are used.
( i ) Address Operator (&) and
( ii ) Indirection Operator ( * ).
Address Operator acts upon variables, array elements, and not on Arithmetic Operators.
Indirection Operator is also called as “value at address” operator. It operates only on operands called
pointer variables.
Pointer Variable:
• Variables that hold memory addresses are called pointer variables.
• Pointer variable contains an address, which is the location of another variable in
the memory.
Accessing the Address of a variable:
• The address of a variable can be accessed using the ‘&’ operator.
• The address operator is specified together, followed by a variable name. It can
also be used together with arrays and in expressions.
Swapping of two Numbers using pointer
Uses /
Applications