Structures in C | C Language Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi everyone welcome to nourish technologies this is rain wash today we are going to discuss about a structures concept in a c language structures so here why we are using structures and what is structure is the first question so generally they will ask an Ender use so what is the structure why they introduce the concept of a structure how we are working with the structures these are the three main things right so today introduction part of structures concept so what is a structure what is structure structure is a user-defined data type structure is user-defined data type and using structure we can define a data type which holds which holds more than one element more than one element of different types of different types this is a definition simply a structure is nothing but it is a user-defined data type how we have a primitive data types like int easy character float double right using structures we can develop user data types so what a user data type holds means it holds n number of elements it holds more than one element of different data types more than one element of different data type if you see the example then you will get more clarity on this okay see the syntax and example the syntax is syntax how to define struct is a keyword we must use to define the structures followed by identity of the structure any name you can give to this structure after giving the name number of elements and all these elements at different data type this is data type element 1 this is data type element 2 and so on and remember one important point every structure should ends with a semicolon and along with the syntax now we will see the example struct suppose employees structure we are taking employee employee structure here employee number is of type word integer type employee name is of type what is a character array employee salary is of type word float type float so structure is a collection of elements of different data type this is the identity of the structure we can identify this structure right with the help of this name employee is the identity sir it is okay actually why they introduce the concept of a structure why they introduce the concept of a structure array versus structure array versus structure using arrays we can store n number of elements how many elements no matter but all the elements should be of type ER in teasers or float or double nothing but only homogeneous data elements it will accept either all the elements should be of type integer all the elements should be of type of float double and so on suppose if you take one string character write any string STR something like a 5 we are taking and here it is a string is a 4 here the elements is of type of character either all our character elements either all are integer elements float elements developments always homogeneous elements only allowed to store into the array so using arrays we cannot process information like employees data students data so many other data like in advanced algorithms write a single linkedlist double linkedlist trees nodes graphs such type of information you cannot store because the different types of information we should store like a employee number is an integer type employee name is a character array and employee salaries of float or double type so in that situation you must go for structures only we can't use arrays concept ok and one more important thing see here suppose I am defining the structure struct employee simply I am writing EMP employee number is an integer type employee name is a character array employee salary is a float type is OK data type is ready data type is ready a structure is ready now here the concept is sir by just defining the structure will it will get a memory allocation definitely not this is just a data type how we have a primitive data types this is data type sir data type is only single word 'no yes right only primitive data type single word like integer character float double but user-defined data type is that collection of words nothing but two words sir when it will get memory allocations or how we need to allocate the memory to this one how right we need to create a variable see for example in the main method in the main method I am writing only inter is there any proper meaning to this instruction definitely not reason into is a data type integer data type here it is you have written only just a form of data type where is a variable is missing right so when it will get a proper meaning int a now it is a statement in the same way integer is a data type in place of integer what we have to write struct EMP we have to write struct EMP combine leave this is a data type very very important don't think that only struct is a data type and only EMP is a data type two words you have to use very very important most of the students confusing here only primitive data type is a single word but user-defined data type two words here struct EMP and here variable and here it is a variable E so this is variable declaration whenever we declare variable now it gets memory allocation sir how many bytes memory will be allocated here here memory allocation for employee number to store employee number and to store employee name and to store employee salary this is a struct EMP type at some location base address is a random address only just like array this is called a base address structure variable always holds a base address only by holding the base address only it is pointing to that one sir what type of variables holding addresses pointer variables only but sir where is the pointer is not there this is E is called internal pointer variable is called internal pointer variable because it is holding address base address it is holding so that is why we can call it as internal pointer variable sir total how many bytes memory will be allocated sir how many bytes means integer occupies two bytes consider so two zero four six and two zero four seven next location address is it 2 0 4 8 and next each character occupies one byte in C language so total 20 characters so 20 bytes memory will be allocated to 0 4 8 2 2 2 0 6 8 I mean 6 7 and here it is a 2 0 6 8 and it is occupying 4 bytes so total 20 + 4 + 2 26 bytes memory will be allocated to this structure variable how much memory 26 bytes memory will be allocated okay so once we allocate the memory how can we access the elements of that structure so memory allocated I want to store the information or I want to retrieve the information sir how can we store the information and how can we retrieve the information tell me very simple structure variables we can access with the help of accessor accessor is a dot operator accessing accessing is very simple is very simple e dot e is a address dot operator is a accessor e dot e number e dot e name a dot e salary u dot employee number e dot employee name u dot employee salary this is how to access the members of the structure how to access the members of the structure ok so this is completely interaction part of a structures how to define the structure so what are the advantages of structures when compared with the arrays and next when an erect mean when a structure gets memory allocation how the memory will be allocated why structure variable is called internal pointer variable how to access the elements ok so now we will see how to find the size of the structure and next one global structures local structures and how to read and print the elements of the structure so first one how to find a size of the structure size of structure how to find it I am defining a structure same struct employee employee here it is employee number employee name and employee cell structure is ready now we are writing main method inside the main method I am declaring a variable for the structure struct EMP so variable name is a e how to find the size is a very simple we are writing a printf function and we are using sizeof function printf sighs of employee size of employee size is always representing inner integers only so here a format specifier is a percentage D and we are using a size of function sizeof sir what we have to pass to size of function two options you have either datatype or variable this is the datatype and this is a variable either you can pass simply a variable or you can pass a datatype also size of employee percentage D size of method or function we are passing the data type what is the data type here struct employee is the data type struct employee is the data type this is a simple program finding the size of the structure how to find the size of the structure means using sizeof function we already have the knowledge of a sizeof function what it will do means it is taking either variable or data type as an argument you can pass a variable or you can pass a data type it will return the size in the form of integer and of course the size is in the form of integer only so with the help of sizeof function right here it is we are passing either variable of the structure or data type of the structure data type means what again two words you have to pass very very important thing and here it is the size is always representing in integer integers means what a percentage D is a format specifier so it will print it will return the value 26 that 26 will print in this place because already we discussed that a size is a 26 bytes of this structure this is size of structure and next now discussion is a local structures and the global structures what type of structures we should declare it as a local and what type of structures we should declare it as a global the structure definition is common for all is called a global the structure definition is specific to a particular function is called a local just like a local variables and global variables only the Declaration of structure inside a particular function is called a local structure outside to all the functions is called a global structure right so parallel we will see you will get more clarity on this here this is a local structure and this is global structure so parallel you will see both first local structure one is the main method in the program along with the main method we are writing one more method suppose check method two methods we know that the C program is a collection of methods of course n number of methods you can write nothing but functions you can write here it is a inside the main I am declaring one structure structure name anything you can give suppose a concept name we are giving local structure inside I am declaring only two variables a and B this is a local structures or how can you say that defining inside the method local means definition definition inside method is nothing but a function definition inside the function C we are defining the structure inside the function only and the local variable we can access only within the block or method in which it has defined where you define so there only you can axis same story you can access this structure suppose I am creating variable for this structure struct local any name you can use suppose L it is allowed reason you are accessing that structure only within that function only not outside but whenever you are declaring outside and trying to access outside error message cannot find the symbol nothing but a undefined structure in a check function local structure you can access only within the function where you define write but suppose if it is a global structure see the global structure definition definition outside to all the functions outside to all the functions is called global is called global now structure we are defining struct name concept name we are giving is a global structure inside two integers we are declaring what is inside no matter that is not our concept our concept is accessing is allowed or not is our concept so here main method we are writing along with the main method check method is also we are writing check method so just like this and here it is a main and check is trying to access a global yes of course is allowed struct we are declaring the variable here global variable name is a G it is allowed and next one struct here it is a variable name is a global and it is a G it is also allowed because the structure is a global structure and you define out to all the functions like a global variable global variable you can access anywhere in the application no limitations so you can access here and you can access here but come to that local structures a local structure means a defining structure inside a particular function so once you defined inside a particular function that you can access only in that function from the outside it is impossible to access ok so this is about a local structure and as well as a global structure alright so now we will see how to initialize the structure directly and how to access the elements here generally how we are assigning a values to array directly a structure also we can initialize directly right the same structure we are taking in this program struct employee employee number employee named employee cell employee named employee cell structure is ready and as usual main method we are writing main method inside the main method in the process of declaration of a structure variable struct employee EMP e variable declaration he gets memory allocation we know already at some location employee number employee name employees cell gets memory allocation and of course he holds the base address it is pointing to this location location now in the process of declaration we can initialize directly but remember one thing in which order you defining the elements in the structure in the same order we should sign the values suppose 1001 1001 right is the employee number employee name for example Karthik is an employee named employee salary for example 50,000 is a employee salary employee number employee name strings how we are representing using double quotations in any programming language so first employee number employee named employee Sal we can't change the order of elements because we are defining in the same order so directly you can initialize like this then automatically employee number will go and store here employee name will go and store here and employee salary employee salary and next one here it is how to print printf employee details we are printing a message very clearly employee details and in the next line printf one by one you can print them suppose employee number employee number is an integer type how to access using accessor that is nothing but a dot operator e dot employee number next one printf employee name % is yes it is a string e dot employee name and next one printf employee salary is percentage f e dot employee salary this is accessing so I want to write all these things in your printf function that is your interest your skillset your capabilities ok if you want to print all the employee details right clearly I am printing right is every basic programmer can understand these formats that is why I have written this sir I do not want to use a four the functions you can use a single printer function and you can format okay output formatting is completely depends on the programmers interest and end-user requirement also right here we are writing employee number is an integer employee name is a string employee salary is a flow type so different format specifiers I am using accessing with the help of dot operator that is already we discussed in the introduction part of a structures concept okay so this is a simply how to initialize a structure variable directly and how to access the structure elements so in this concept we discussed how to find the size of the structure using sizeof function and second one what is the difference between a global structure variables and local structures and the last one how to initialize directly okay hope you will enjoy this session for more sessions so please log in to nourish technologies thank you all [Music]
Info
Channel: Naresh i Technologies
Views: 765,738
Rating: 4.9016528 out of 5
Keywords: C Language, Structures in C, Srinivas, Naresh IT, Hands on C Language Training, C Language Demo, Online C Language Training, C Language Tutorial Videos, C Language Overview, C Language Interview Questions, c language training, c Tutorials, C Training Videos
Id: Ranc3VvjI88
Channel Id: undefined
Length: 25min 44sec (1544 seconds)
Published: Mon Sep 12 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.