A structure is a user-defined collection of one or more variables grouped under a single name. This allows a programmer to wrap related variables with different data types into a single entity making it easier to manipulate data in the program. The variables in a structure can be of different data types. Each variable within a structure is called a member of the structure.
The struct keyword is used to declare the beginning of a structure definition followed immediately by the name of the structure. Following the structure tag is an opening curly brace followed by a list of the structure’s member variables and a closing curly brace. Once the structure type has been defined, variables of that structure type can then be declared either along with the structure definition or after the structure is defined (see below).
struct account
{
int accountno;
char *lastname;
char *firstname;
unsigned int telephone;
} user1;//structure variable declaration defined with structure
struct account user2, user3;//structure variable defined outside of the structure;
Note: In C++, the struct keyword is optional before the declaration of a structure variable. In C, it is compulsory.
Structure members are accessed using the structure member operator ( . ), also called the dot operator, between the structure name and the member name. To have the structure user1 above set the firstname member use the following -
user1.firstname = "Joe";
When initialising a struct, the initialiser consists of a brace-enclosed, comma-separated list of values placed in the same order as the variables in the structure definition.
For instance to initialise a struct variable user4 using the structure above -
struct account user4={123,"john","doe",123456789};
Structure members cannot be initialised within the declaration because when a datatype is declared, no memory is allocated for that datatype. Memory is allocated only when variables are created.
Designated Initialisation - allows structure members to be initialised in any order. Each value is preceded by a designated initialiser which corresponds to the structure variable name. Initialisation then moves forward in order of declaration if no further designated initialisers are specified. This feature has been added to the C99 standard. For example -
struct account user5={.lastname="jill","doe",123456789};
Any remaining members not initialised with be set to Zero.
An array of structures is a sequential collection of structures. After the structure has been defined, an array of structures is declared as follows-
struct account list[100];
The above statement declares an array named list that contains 100 elements. Each element is a structure of type account and is identified by subscript like other array element types. The structure data can be manipulated by specifying the appropriate index value of the array. Like a standard array, an array of structures can be initialised at compile time.
Structures can be accessed and manipulated by pointers. Pointers to structures can be declared the same way as any other pointers: by preceding the variable name with an asterisk. The individual member variables can be accessed using the -> operator instead of the .(dot) operator
int main(void)
{
struct account{
unsigned int accountno;
char *firstname;
char *lastname;
unsigned int telephone;
} user1={123,"joe","blogs",123456789};//structure variable user1 defined and initiated
struct account *ptrStr;//create pointer of type struct account
ptrStr = &user1;//set pointer to address of user1
printf("%d %s %s %d", ptrStr->accountno,ptrStr->firstname, ptrStr->lastname,ptrStr->telephone);//access elements using ->
return 0;
}
An entire structure can be passed to a function as a parameter. This structure can be transferred to a function either using call by value or call by reference scheme.
struct account{ //declare stucture
unsigned int accountno;
char *firstname;
char *lastname;
unsigned int telephone;
};
void function1(struct account user1copy) //declare fuction with argument type struct account
{
printf("%d\n",user1copy.accountno);
printf("%s\n",user1copy.firstname);
printf("%s\n",user1copy.lastname) ;
printf("%d\n",user1copy.telephone);
}
int main(void)
{
struct account user1={123,"joe","blogs",123456789};//initialise structure
function1(user1);//call structure passing parameter user1
return 0;
}
Passing a structure parameter by value – To Pass a structure parameter by value declare the function to use the type of structure you’ve created. When the function is called, a copy of your structure is created and used by the function. Any modification to the structure made inside of the function, and won’t affect the original.
Passing a structure parameter by reference – In order to be able to change the structure inside of our function, then it must be passed it by reference. . Any changes made to the structure members are reflected on to the original structure. This is because pointers deal directly with the data stored in the memory.
In C, a structure declaration can be placed inside another structure (nesting). This declaration can either be an embedded structure declaration or by making two separate structure declarations.
Embedded Structure example
#include <stdio.h>
int main(void) {
struct outer {//declare outer stucture
int member1;
int member2;
int member3;
struct embedded{//declare inner structure
int member_1;
int member_2;
int member_3;
} embeddedvar;//declare inner struct variable
} outervar={3,4,5,{1,2,3}};//declare outer stuct variable and initialise outer values and then inner values
// structure embedded
printf("%d", outervar.embeddedvar.member_1);//output embedded member_1
printf("%d", outervar.member1);//output member1
return 0;
}
Separate nested Structure example
#include <stdio.h>
int main(void) {
struct embedded
{
int member_1;
int member_2;
int member_3;
};
struct outer
{
struct embedded embeddedvar; //declare embarred variable
int member1;
int member2;
int member3;
} outervar={{1,2,3},3,4,5};
printf("%d", outervar.embeddedvar.member_1);
printf("%d", outervar.member1);
return 0;
}
Last Updates:25 October 2022