C programming | structure | Fundamentals of Structure

 Programming codester 

C programming 

Structure 


on user’s demand I bring the tutorial of structure in c language! 

So first find the error from this code:

#include<stdio.h>

void main()

{

printf("hello");

return 0;

}


If you want to see my other tutorial of c programming I will mention all link at the end of this tutorial. So, stay tuned! 

So guys lets start structure with C! 

If you want to learn structure, you have to knowledge of datatypes! 

So, first we learn about datatypes. 


DATATYPES: 

There are 2 types of data type: 

  1. 1.Primary datatype: 

In this datatype float, int, char, void, etc. Are the best examples. 

2. Secondary Data type: 

In secondary data type there are 2 types of secondary data types!                

 

1] Derived Data type 

(Array, pointer) 

2] User defined Data type 

  (Structure, union, Enum) 

C language has built-in datatypes like primary and derived data types. 

But still not all real-world problems can be solved using those data types. 

We need custom datatype for different situations. 

 

What is the importance of structure? 

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, float, char, etc.). 

What is Structure? 

  • Structure is a collection of logically related data items of different datatypes grouped together under single name. 
  • Structure is a user defined datatype. 
  • Structure helps to build a complex datatype which is more meaningful than an array. 
  • But an array holds similar datatype record, when structure holds different datatypes records. 

Two fundamental aspects of Structure: 

  • Declaration of Structure Variable 
  • Accessing of Structure Member 
  • Syntax to Define Structure 

To define a structure, we need to use struct keywords. 

This keyword is reserved word in C language. We can only use it for structure and its object declaration. 

Syntax: 

 

  • structure_name is name of custom type 
  • memberN_declaration is individual member declaration 
  • Members can be normal variables, pointers, arrays or other structures. 
  • Member names within the particular structure must be distinct from one another. 

 

You must terminate structure definition with semicolon. 

You cannot assign value to members inside the structure definition, it will cause 

compilation error. 

 

Create Structure variable 

  • A data type defines various properties of data stored in memory. 

  • To use any type, we must declare its variable. 

  • Hence, let us learn how to create our custom structure type objects also known as structure variable. 

  • In C programming, there are two ways to declare a structure variable 

Along with structure definition  

After structure definition 

Declaration along with the structure definition 

 

Access Structure member (data) 

  • Structure is a complex data type; we cannot assign any value directly to it 

using assignment operator. 

  • We must assign data to individual structure members separately. 

  • C supports two operators to access structure members, using a structure variable.  

  1. Dot/period operator (.) 
  2. Arrow operator (->)  

Dot/period operator (.) 

It is known as a member access operator. We use dot operator to access members of simple structure variable.  

Arrow operator (->) 

In C language it is illegal to access a structure member from a pointer to structure variable using dot operator. 

We use an arrow operator to access structure member from pointer to structure. 


 So, people I hope I considered all points of structure. 


Now I will give one example of a program of structure! 

 


 

So, I hope you understood the concept of structure! 

So, if you didn’t read my previous vlog, I will mention all links. Watch and learn! 

 

Learn and explore!!a

Stay tuned for next!! 

Comments

Popular posts from this blog

C programming | Printf and scanf ||

C programming | Fundamentals & Basic information

Basic question related programming || c programming || embedded programming

C++ || Operators with c++

C++ || string with c++ || Part - 1

Compiler & Interpreter | difference and explanation

c++|operators with c++|variables||

C programming | Nested structure | Array, function and pointer of structure|| structure Part-II

C++(oop)| Fundamentals | Concepts||