Session 6
Upcoming SlideShare
Loading in...5

Session 6



C Language Slides

C Language Slides



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds



Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Session 6 Session 6 Presentation Transcript

  • Structures• A structure is a collection of one or more variables types grouped under a single name for easy manipulation.• The variables in a structure, unlike those in an array, can be of different variable types.• A structure can contain any of Cs data types, including arrays and other structures.• Each variable within a structure is called a member of the structure.
  • Using a struct• By defining a structure you create a new data type.• Once a struct is defined, you can create variables of the new type. Student stu_variable;
  • Accessing Structure Members• Structure members are accessed using the structure member operator (.), also called the dot operator, between the structure name and the member name.• Thus variable s1 can be access in following way;”AMAR”; S1.per=80;• printf("%d,%s,%f",,,s1.per);
  • #include <stdio.h>struct student{ int id; char *name; float per;};void main(){ struct student st;;“ram”; st.per=90.5;}
  • Advantage Of Using structure• One major advantage is that you can copy information between structures of the same type with a simple equation statement.• Continuing with the preceding example, the statement S2 = S1; is equivalent to this statement:;; S2.per=S1.per;
  • Arrays of Structures#include <stdio.h>struct entry{ char fname[20];};
  • main() { struct entry list[4]; for (i = 0; i < 4; i++) { printf("nEnter first name: "); scanf("%s", list[i].fname); } for (i = 0; i < 4; i++) { printf("Name: %s ", list[i].fname,} }
  • Pointer To Structurestruct emp{ int empno; char nm[20]; };void main(){ struct emp e1={10,”RAJ”}; struct emp *p; p=&e1; printf(“%d %s ”, e1.empno, e1.nm); printf(“%d %s ”, p->empno, p->nm);}
  • UNIONS• A union is a variable which may hold members of different sizes and types.• The syntax for declaring a union is similar to that of a structure: union number { int number; double floatnumber; } anumber;
  • • This defines a union called "number" and a variable of it called "anumber".• Members of the union can be accessed in the following way:• printf("%f",anumber.floatnumber);• When the C compiler is allocating memory for unions it will always reserve enough room for the largest member.