Skip to main content

Structures and Unions

A structure is a user-defined data type available in C that allows to combining data items of different kinds. Structures are used to represent a record.

Defining a structure: To define a structure, you must use the struct keyword followed by an optional struct tag followed by the body of the struct. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows:
   struct [structure name]
   {
       member definition;
       member definition;
       ...
       member definition;
   };
 
Eg:
struct  student
{
Int rno;
char name[50];
int mark;
char sex;
float height;
} S;
Here S is a structure variable.
Structure member can be any valid data type(primitive, arrays) including other structures and pointers. A structure may not, for obvious reasons, contain instances of itself but may contain pointers to instances of itself.
We can also create array of structures or pointer to a struct.
Accessing the members of a structure:
The members of a structure can be accessed using the structure variable with .(dot) operator.The dot operator selects a particular member from the structure.
Eg:
S.mark=45
S.sex=’M’

Initializing Structure

A structure can be initialized in much the same way as any other data type. This consist of assigning some constants to the members of structure.
Eg:
struct  student
{
int rno;
char name[50];
int mark;
} S={101,”akshay”,45};

struct  student
{
int rno;
char name[50];
int mark;
} S[2]={{101,”akshay”,45},{102,”devi”,47}};

Copying and Comparing Structures

A structure can be assigned to another structure of the same type. But we cannot compare two structure variables.
Eg:
struct  student
{
int rno;
char name[50];
int mark;
} S1,S2;

S1=S2;

Union
A union is a special data type available in C that allows storing different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple purposes.The amount of storage allocated to a union is sufficient to hold its largest member.
Defining a Union: To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows:
    union [union name]
    {
       member definition;
       member definition;
       ...
       member definition;
    };

Defining and accessing the members of a union is similar to structure.
Eg:
#include <stdio.h>
#include <conio.h>
union test
{
int i;
char c;
}var;
int main()
{
var.i=65;
printf(“var.i=%d\n”,var.i);
printf(“var.c=%c”,var.c);
return 0;
}
Similarities between Structure and Union
Both are user-defined data types used to store data of different types as a single unit.
Their members can be objects of any type, including other structures and unions or arrays. A member can also consist of a bit field.
Both structures and unions support only assignment = and sizeof operators. The two structures or unions in the assignment must have the same members and member types.
A structure or a union can be passed by value to functions and returned by value by functions. The argument must have the same type as the function parameter. A structure or union is passed by value just like a scalar variable as a corresponding parameter.
‘.’ operator is used for accessing members.

Differences between structure and union

Sample Program

Reading the admission number and name of N students and printing the roll list in the order of name.

#include <stdio.h>
#include <stdlib.h>
int main()
{
int N,i,j;
struct stud
{
 int adno;
 char name[50];
} s[100],t;
printf("enter no of students..N <100::");
scanf("%d",&N);
printf("Enter Admission no and Name of %d students\n",N);
for(i=0;i<N;i++)
scanf("%d %s",&s[i].adno,s[i].name);
//sorting the list according to name
for(i=0;i<N-1;i++)
for(j=i+1;j<N;j++)
 if(strcmp(s[i].name,s[j].name)>0)
    {
       t=s[i];
       s[i]=s[j];
       s[j]=t;
    }
//printing the roll list in the order of name
printf("Name....Admission Number\n");
for( i=0;i<N;i++)
printf("%-20s%5d\n",s[i].name,s[i].adno);
}

Comments

Popular posts from this blog

Files in C

A file is a repository of data that is stored in a permanent storage media, mainly in secondary memory. In order to use the files we should learn how to read information from a file and how to write information into a file. A very important concept in C is the stream.The stream is a common logical interface to the various devices( files).A stream is linked to a file while using an open operation. A stream is disassociated from a file while using a close operation. The current location, also referred to as the current position, is the location in a file where the next fie access will occur.There are two types of streams text and binary. The following are some difference between text and binary files ·Text file is human readable because everything is stored in terms of text. In binary file everything is written in terms of 0 and 1, therefore binary file is not human readable. ·A newline(\n) character is converted into the carriage return-linefeed combination before being written to the d…

KTU C programming question paper and evaluation scheme

APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY SECOND SEMESTER B.TECH DEGREE EXAMINATION, MAY 2017 CS 100 COMPUTER PROGRAMMING (CS, IT) SCHEME OF EVALUATION
PART A 1 An identifier is a sequence of characters invented by the programmer or to identify or name a specific object. The sequence of characters may be letters, digits, and special character ‘_’known as an underscore Rules: i)Identifiers should start with alphabets. ii)Identifiers are case sensitive iii)A numeric digit should not be the first character iv)Identifier name should not be a keyword v)Identifier may be of any reasonable length 1mark






2mark 2 Associativity defines the direction, left to right or right to left in which operator act upon its operands Unary operators have associativity is from right to left. For examplein the expression &--x, pre decrement works first and then address of operator works Direction + example<

Linear and Binary Search in C

Searching is one of the most common problems that arise in computing. Searching is the algorithmic process of finding a particular item in a collection of items. A search typically answers either True or False as to whether the item is present. On occasion it may be modified to return where the item is found. Search operations are usually carried out on a key field. Consider searching for a given value k in an array A of size n. There are 2 basic approaches: sequential search and binary search.
Linear (Sequential) Search When data items are stored in a collection such as a list or array , we say that they have a linear or sequential relationship. Each data item is stored in a position relative to the others. In C array, these relative positions are the index values of the individual items. Since these index values are ordered, it is possible for us to visit them in sequence. This process gives rise to our first searching technique, the sequential search. Starting at the first item in …