Programming in C
Programming in C
};
struct is a keyword.
structure_name is a tag name of a structure.
member1, member2 are members of structure.
Example:
#include<stdio.h>
#include<conio.h>
struct book
{
char title[100];
char author[50];
int pages;
float price;
};
void main()
{
struct book book1;
printf("enter title, author name, pages and price of
book");
scanf(“%s”,book1.title);
scanf(“%s”, book1.author);
scanf("%d",&book1.pages);
scanf("%f",&book1.price);
printf("\n detail of the book");
printf(“%s”,book1.title);
printf(“%s”,book1.author);
printf("%d",book1.pages);
printf("%f",book1.price);
getch();
}
book is structure whose members are title, author, pages and price.
book1 is a structure variable.
2 How do we declare and access structure variables?
Declaration of structure:
A structure variable declaration is similar to the declaration of variables of any other
data type. It includes the following elements:
1) The keyword struct.
2) The structure tag name.
3) List of variable names separated by commas.
4) A terminating semicolon.
Example:
struct book
{
char title[100];
char author[50];
int pages;
float price;
} book1;
struct book book2;
We can declare structure variable in two ways:
1) Just after the structure body like book1.
2) With struct keyword and structure tag name like book2.
Accessing structure members:
The following syntax is used to access the member of structure.
structure_variable.member_name
structure_variable is a variable of structure and member_name is the name of variable
which is a member of a structure.
The “.”(dot) operator or ‘period operator’ connects the member name to structure name.
Example:
book1.price represents price of book1.
We can assign values to the member of the structure variable book1 as
below, strcpy(book1.title,”ANSI C”);
strcpy(book1.author,”Balagurusamy”);
book1.pages=250;
book1.price=120.50;
We can also use scanf function to assign value through a keyboard.
scanf(“%s”,book1.title);
scanf(“%d”,&book1.pages);
3 What is Union?
Union is user defined data type just like structure.
Each member in structure is assigned its own unique storage area where as in Union, all
the members share common storage area.
All members share the common area so only one member can be active at a time.
Unions are used when all the members are not assigned value at the same time.
Example:
union book
{
char title[100];
char author[50];
int pages;
float price;
};
4 Difference between Structure and Array
Array Structure
o An array behave like a built-in o First we have to design and declare a
datatype all we have to do is to data structure before the variables of
declare an array variable and use it. that type are declared and use.
o An array is a collection of related o Structure can have elements of
data element of same type. different type.
o An array is derived datatype. o Structure is programmer defined.
5 Difference between Structure and Union
Structure Union
o Each member is assigned its own o All members share the same storage
unique area.
storage area.
o Total memory required by all members o Maximum memory required by the
is member is allocated.
allocated.
o All members are active at a time. o Only one member is active a time.
o All members can be initialized. o Only the first member can be initialized.
o Requires more memory. o Requires less memory.
Example: Example:
struct SS union UU
{ {
int a; int a;
float b; float b;
1 byte for c char c; char c;
};for a
2 bytes };
4 bytes for b 4 bytes for c,b,a a c
for(i=0;i<66;i++)
{
printf("\nenter name,roll no,cpi");
scanf(“%s”,r[i].name); scanf("%d
%f",&r[i].rollno,&r[i].cpi);
}
printf("\n detail of student:\n");
for(i=0;i<66;i++)
{
printf(“%s”,r[i].name);
printf("\t%d",r[i].rollno);
printf("\t%f\n",r[i].cpi);
}
getch();
}
8 Explain Pointers to Structure with example.
We can define pointers to structure in very similar way as you define pointer to
any other variable.
Syntax:
struct structure_name
{
Member 1;
Member 2;
.
.
};
struct structure_name *structure_pointer;
Example:
struct books
{
char title[100];
};
struct books *struct_pointer,book1;
Now we can store the address of a structure variable in the above defined pointer
variable.
To find the address of a structure variable, place the ‘&’ operator before the structure ‘s
name
as follow:
struct_pointer = &book1;
To access the member of a structure using a pointer to that structure you must
use the -> operator as follow:
structure_pointer->title;
#include<stdio.h>
#include<conio.h>
struct name
{
int a;
float b;
};
void main()
{
struct name *ptr, p;
ptr = &p; /* Referencing pointer to memory
address of p */
printf(“Enter Integer: ”);
scanf(“%d”,&(*ptr).a);
printf(“Enter number: ”);
scanf(“%f”,&(*ptr).b);
printf(“Displaying: ”);
printf(“%d \n %f”,(*ptr).a,(*ptr).b);
getch();
}
Structure pointer member can also be accessed using -> operator.
(*ptr).a is same as ptr-> a.
(*ptr).b is same as ptr-> b.