Full Transcript

UNIT VI Strings, Structure and Pointers C Structures Structure is a user-defined data type in C language which allows us to combine data of different types together. Structure helps to construct a complex data type which is more meaningful. It is somewhat similar to an Array, but an array...

UNIT VI Strings, Structure and Pointers C Structures Structure is a user-defined data type in C language which allows us to combine data of different types together. Structure helps to construct a complex data type which is more meaningful. It is somewhat similar to an Array, but an array holds data of similar type only. But structure on the other hand, can store data of any type, which is practical more useful For example: If I have to write a program to store Student information, which will have Student's name, age, branch, permanent address, father's name etc, which included string values, integer values etc, how can I use arrays for this problem, I will require something which can hold data of different types together. In structure, data is stored in form of records. Defining a structure struct keyword is used to define a structure. struct defines a new data type which is a collection of primary and derived datatypes. Syntax: struct [structure_tag] { //member variable 1 //member variable 2 //member variable 3... }[structure_variables]; As you can see in the syntax above, we start with the struct keyword, then it's optional to provide your structure a name, we suggest you to give it a name, then inside the curly braces, we have to mention all the member variables, which are nothing but normal C language variables of different types like int, float, array etc. After the closing curly brace, we can specify one or more structure variables, again this is optional. Note: The closing curly brace in the structure type declaration must be followed by a semicolon(;). Declaring Structure Variables It is possible to declare variables of a structure, either along with structure definition or after the structure is defined. Structure variable declaration is similar to the declaration of any normal variable of any other datatype. Structure variables can be declared in following two ways: Accessing Structure Members Structure members can be accessed and assigned values in a number of ways. Structure members have no meaning individually without the structure. In order to assign a value to any structure member, the member name must be linked with the structure variable using a dot(.) operator also called period or member access operator. s1.age = 18; strcpy(s1.name, "Viraj"); printf("Name of Student 1: %s\n", s1.name); printf("Age of Student 1: %d\n", s1.age); return 0; } Name of Student 1: Viraj Age of Student 1: 18 We can also use scanf() to give values to structure members through terminal. scanf(" %s ", s1.name); scanf(" %d ", &s1.age); Array of Structure : Array of structures in C can be defined as the collection of multiple structures variables where each variable contains information about different entities. The array of structures in C are used to store information about multiple entities of different data types. The array of structures is also known as the collection of structures Syntax Struct structurename objectname[size]; Example: #include #include struct student{ int rollno; char name; }; int main(){ int i; struct student st; printf("Enter Records of 5 students"); for(i=0;i

Use Quizgecko on...
Browser
Browser