Introduction to Structures in c programming language
You have already read about data types. Like Predefined data types (int, char, float, etc.) also provides C user-defined data types. The Structures in c programming language is also a type of a user-defined data type.
Structures in c programming language, you can create other predefined data types and create a record. As if you want someone to name his name, address and age store, then you can create a Structures in c programming language by the name of that person and in it, he can create three variables. By doing this, all the information will be in one place and accessible through the same name.
It is the same as an array in c programing language. The difference is that in the array, you can store the same type of data, but different types of data can be stored by the structure.
Once you create a Structures in c programming language, it becomes a data type. Now you can create any variables of this data type in c programming language. And you can also create an array of this data type. But when you initialize the value of this variable, you have to initialize the value of all the variables defined in that structure.
You can define the structure before the main method.
Defining a Structures in c programming language
Working with Structure is very easy. As I told you earlier, it is like an array. Let us now see how the structure is defined and used.
Struct keyword is used to define the Structures in c programming language. After this keyword, the structure is given the unique name. After this, variables are created in curly braces and after the ending curly bracket semicolon is applied. Below is the basic syntax for creating Structures in c programming language.
//Structures in c programming language;
Here a structure has been created by the name T-shirt. In this structure, a variable has been created by the name of price. You can also create multiple variables. You can not initialize these variables inside the Structures in c programming language. Because the first struct (tShirt) type of variable will be created, then through these variables, these variables are initialized for each record separately. These variables are called structure members. As I mentioned earlier, semicolon has been applied after ending curly braces.
Creating Structure Variables in C
- With structure definition
- Without structure definition
With Structure Definition
Without Structure Definition
struct tShirt t1, t2, t3;
Accessing Structure Members
You have access to Structure members because of 2 reasons. Either you access the members of the members to assign values to members or to print their values as output. Whenever you access any structure member, you do this (.) By dot operator.
Suppose you want to assign values to the variables of the tShirt structure, then you can do it like this.
struct tShirt t1;
printf(“Price of tShirt is : %d”,t1.price);
The above program produces the below-given output.
Price of tShirt is: 1000
A structure as Function Argument
But you have to define the parameter in the form declaration and definition with the struct keyword. Its syntax is being given below.
return-type function-name(struct-keyword struct-name obj-name)
//Function code here…
Passing the Structures in c programming language as an argument in the Function is being explained by the example below.
};void printPerson(struct Person p1);int main()
struct Person p1;p1.Id = 101;
p1.Phone = 237434839;
void printPerson(struct Person p1)
The above example produces the below given output.
Pointer to Structure
Pointers of Structure variables can also be created. These are created in the same way as you create pointers of a normal variable.
Structure variables are being given syntax to create pointers.
struct-keyword struct-name *struct-pointer-variable; //Creating pointer of structpointer-variable = &struct-variable; //Assigning address of struct variable to pointer
Creating and using pointers of Structures in c programming language is being explained by the example below.
struct Employee e1;struct Employee *emp;emp = &e1;
printf(“Employee Id : %d\n”,emp->Id);
The above example produces the below given output.
Employee Id: 101
Employee Phone: 2374334839
C language provides you with the capability to use memory correctly. If you are creating variables inside the structure that do not use the full memory then in this situation, you can define the size of those variables and tell how much memory should be assigned to that variable.
One thing you should keep in mind is that you can only do this with the structure and union members. This can not be done with any normal variable.
Its main purpose is the correct utilization of memory. When you know that the value of a struct member does not exceed the fixed size then you can do this.
The size of the variable you define in bits. This is the reason why such variables are called bitfields.
The general syntax of creating bit fields in C programming language is being given below.
type variable-name : size;
Working with bitfields in C programming language is being explained by the example below.
int age : 5;
struct Person p1;
p1.age = 5;printf(“Age is : %d”,p1.age);return 0;
In the above example, if you input more than 5 bit in the value of the variable, the value will be displayed in the minus. This example produces the below given output.
Age is 5