Structures in c programming language

Structures in c programming language

Social Shares
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

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.

struct struct_Name
{
    //Structures in c programming language;
};
Suppose you are creating a Structures in c programming language to store a t-shirt’s record, then you can define it as follows.
struct tShirt
{
    int price;
};

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

Structure variables can be created in 2 ways.
  • With structure definition
  • Without structure definition

With Structure Definition

When you create variables in c programming of that type along with the structure definition, then before ending semicolon, you write variables separately from the comma. As has been done in the example below.
struct tShirt
{
   int price;
}t1,t2;

Without Structure Definition

When you create variables without the structure definition, then struct keyword uses. After the Struct keyword, the name of the structure is written. And then you can write as many variables as you want by separating them from the comma. An example of this is being given below.
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.

t1.price=1000;
If you want the variables of the t-Shirt structure to be printed as output, then you can do this as follows.
printf(“%d”,t1.price);

Example

#include <stdio.h>
struct tShirt
{
    int price;
};
int main()
{
      struct tShirt t1;
      t1.price=1000;
      printf(“Price of tShirt is : %d”,t1.price);
      return 0;
}

The above program produces the below-given output.

Price of tShirt is: 1000

A structure as Function Argument

You can also pass a Structures in c programming language as an argument in a function as well. You do not need any special operator for this. Just as you pass the normal variables as function arguments, so you pass the structure object to the function as well.

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.

#include<stdio.h>struct Person
{
int Id;
int Phone;
};void printPerson(struct Person p1);int main()
{
struct Person p1;p1.Id = 101;
p1.Phone = 237434839;

printPerson(p1);

return 0;
}

void printPerson(struct Person p1)
{
printf(“Name is : %d\n”,p1.Id);
printf(“Phone number is %d\n”,p1.Phone);
}

The above example produces the below given output.

Id: 101
Phone: 237434839

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.

#include<stdio.h>struct Employee
{
int Id;
int Phone;
};int main()
{
struct Employee e1;struct Employee *emp;emp = &e1;

emp->Id=101;
emp->Phone = 237434839;

printf(“Employee Id : %d\n”,emp->Id);
printf(“Employee Phone : %d\n”,emp->Phone);

return 0;
}

The above example produces the below given output.

Employee Id: 101
Employee Phone: 2374334839

BitFields

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.

struct struct-name
{
type variable-name : size;
};

Working with bitfields in C programming language is being explained by the example below.

#include<stdio.h>struct Person
{
int age : 5;
};
int main()
{
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

Social Shares
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *