What is structure in C || What is a structure in C Programming Language

What is structure in C || What is a structure in C Programming Language


Hello guys, what’s up? I have found that every month people are searching about what is a structure in c. So, today I am going to discuss about it. Let’s start—

What is structure in c?


A structure is a user defined data type in C. Each element of a structure is called a member. A structure creates a data type that can be used to group items of possibly different types into a single type. Structure members can be accessed by any function, anywhere in the scope of the Structure

  • Structure variable should be declared, if we want to access structure members in C,
  • We can declare many structure variables for same structure & there memory will be allocated for each separately.
  • If we don’t want to assign any values to structure members while declaring, It is a best practice to initialize a structure to null.
Difference between C structure, C array & C variable
  • Structure: hold group of data of different data types & Data types can be int, char, float, double and long double etc.
  • Array: hold group of data of same data type.
  • Normal C variable: hold only 1 data of 1 data type at a time.

Syntax of what is structure in c:

struct tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};

Example of what is structure in c:

struct programiz
{
int  mark;
char name[10];
float average;
};
How to declare a struct in c?

// A variable declaration with structure declaration. 
struct Point 
{ 
int x, y; 
} p1; // The variable p1 is declared with 'Point' 


// A variable declaration like basic data types 
struct Point 
{ 
int x, y; 
}; 

int main() 
{ 
struct Point p1; // The variable p1 is declared like a normal variable 
}


Declaring structure using normal variable:  struct programiz report;
Declaring structure using pointer variable:  struct programiz *report, rep;
To know about how to declare a struct in c click Here.
Initializing structure in c
struct Point 
{ 
int x = 0; // COMPILER ERROR: cannot initialize members here 
int y = 0; // COMPILER ERROR: cannot initialize members here 
};
When a datatype is declared, no memory is allocated for it. Memory is allocated only when variables are created. So, there COMPILER ERROR is occurred
Valid Example of initializing structure in c
{ 
int x, y; 
}; 

int main() 
{ 
// A valid initialization. member x gets value 0 and y 
// gets value 1. The order of declaration is followed. 
struct Point p1 = {0, 1}; 
}


Initializing structure using normal variable: struct programiz report = {100, “Mani”, 99.5};
Initializing structure using pointer variable: struct programiz rep = {100, “Mani”, 99.5};
                 report = &rep

If you want to learn more about initializing structure in c, just click Here.

How to access the members of a structure in C?


Accessing structure members using normal variable:
report.mark;
report.name;
report.average;


Accessing structure members using pointer variable:
report  -> mark;
report -> name;
report -> average;

What is designated Initialization?


DesignatedInitialization feature has been added in C99 standard, which allows structure members to be initialized in any order.

#include<stdio.h> 

struct Point 
{ 
int x, y, z; 
}; 

int main() 
{ 
// Examples of initializtion using designated initialization 
struct Point p1 = {.y = 0, .z = 1, .x = 2}; 
struct Point p2 = {.x = 20}; 

printf ("x = %d, y = %d, z = %dn", p1.x, p1.y, p1.z); 
printf ("x = %d", p2.x); 
return 0; 
} 

After executing above program, our output look like this:

x = 2, y = 0, z = 1
x = 20
Array of structures in C


Array of structures called as structure array in C. This is nothing but collection of structures. 

#include <stdio.h>
#include <string.h>

struct programiz
{
     int id;
     char name[30];
     float percentage;
};

int main() 
{
     int i;
     struct programiz record[2];

     // 1st programiz's record
     record[0].id=1;
     strcpy(record[0].name, "John");
     record[0].percentage = 86.5;

     // 2nd programiz's record         
     record[1].id=2;
     strcpy(record[1].name, "Himu");
     record[1].percentage = 90.5;

     // 3rd programiz's record
     record[2].id=3;
     strcpy(record[2].name, "TranVan");
     record[2].percentage = 81.5;

     for(i=0; i<3; i++)
     {
         printf("     Records of Programiz : %d n", i+1);
         printf(" Id is: %d n", record[i].id);
         printf(" Name is: %s n", record[i].name);
         printf(" Percentage is: %fnn",record[i].percentage);
     }
     return 0;
}

After executing above program, our output look like this:

Records of STUDENT : 1
Id is: 1
Name is: John
Percentage is: 86.500000
 
Records of STUDENT : 2
Id is: 2
Name is: Himu
Percentage is: 90.500000
 
Records of STUDENT : 3
Id is: 3
Name is: TranVan
Percentage is: 81.500000
Structure with pointer in c


If we have a pointer to structure, members are accessed using arrow ( -> ) operator.

#include<stdio.h> 

struct Point 
{ 
int x, y; 
}; 

int main() 
{ 
struct Point p1 = {1, 2}; 

// p2 is a pointer to structure p1 
struct Point *p2 = &p1; 

// Accessing structure members using structure pointer 
printf("%d %d", p2->x, p2->y); 
return 0; 
}
Output:
1 2
typedef struct in c


Typedefsallow us to define types with a different name – which can come in handy when dealing with structs and pointers. typedef makes the code short and improves readability. By the help of  typedef we can get rid of the long definition of a point structure. It is like an alias of struct.


Code without typedef struct c:
struct programiz {
  int local_street;
  char *town;
  char *my_city;
  char *my_country;
};
...
struct programiz var; 
var.town = "Agra";

Code using tyepdef struct c:

typedef struct programiz{
  int local_street;
  char *town;
  char *my_city;
  char *my_country;
}addr;
..
..
addr var1;
var.town = "Agra";
Instead of using the struct programiz every time you need to declare struct variable, you can simply use addr, the typedef that we have defined.

 If you want to learn more about typedef struct in c, just click Here.

Uses of structures in C
  1. Structures act as a database. C Structures can be used to store huge data.
  2. C Structures can be used to send data to the printer, check computer’s memory size, clear output screen contents
  3. C Structures can interact with keyboard and mouse to store the data.
  4. It can be used in drawing and floppy formatting.
Limitations of structures in C


A Structure is a helpful tool to handle a group of logically related data items. However, structure in C has some limitations.

  • C structure does not allow the struct data type to be treated like built-in data types: 
  • On Structure variables, we cannot use operators like +,- etc.
struct number 
{ 
 float x; 
}; 
int main() 
{ 
 struct number n1,n2,n3; 
 n1.x=4; 
 n2.x=3; 
 n3=n1+n2; 
 return 0; 
} 

/*Output: 

prog.c: In function 'main': 
prog.c:10:7: error: 
invalid operands to binary + (have 'struct number' and 'struct number') 
n3=n1+n2; 

*/
  • Structures in C cannot have constructor inside Structures.
  • C structures do not permit functions inside Structure 
  •  C Structures cannot have static members inside their body 
  • C Programming languages do not support access modifiers. So they cannot be used in C Structures. 

Today we have read many topics. Those are what is structure in c, syntax of what is a structure in c, how to declare a struct in c, initializing structure in c, arrays of structures in c, typedef struct in c and many more this. I hope today you guys have learned many more things about structures in c. So, guys, that’s all for today. Later we will discuss another tutorial on c programming.

Post a Comment