Sie sind auf Seite 1von 14

STRUCTURES

UNION
&
ENUMERATIONS
STRUCTURES
• Structure is user defined data type available in C that allows to combine data items of different
kinds.
• ‘struct’ keyword is used to create a structure.
• SYNTAX −
struct [structure tag] {
member definition;
member definition;
member definition;
} [one or more structure variables];
• Example of Book structure is as shown below −
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id; } book;
ACCESSING STRUCTURE MEMBERS
• Using (.) dot operator
• POINTERS TO STRUCTURES ARE ACCESSED Using (->) indirection operator

• Operations on struct variables in C ---- ASSIGNMENT


#include <stdio.h>
struct Point { OUTPUT :
int x; p2.x = 10, p2.y = 20
int y;
};
int main()
{
struct Point p1 = {10, 20};
struct Point p2 = p1; // contents of p1 are copied to p1
printf(" p2.x = %d, p2.y = %d", p2.x, p2.y);
getchar();
return 0;
}
EXAMPLE
#include<stdio.h>
struct student OUTPUT:
METHODS TO COPY A STRUCTURE IN C { 450 90.000000
int id; 450
float percentage; 450
• DIRECT ASSIGNMENT };
• C INBUILT FUNCTION MEMCPY() int main()
• COPY BY INDIVIDUAL STRUCTURE MEMBERS {
struct student rec1={450,90};
struct student
• NESTED STRUCTURE – accessed using normal & pointer variables rec2,*rec3,*ptr1,rec4;
struct student{ struct stud_detail{ rec2=rec1;
int id; int coll_id; printf("%d
char name[20]; int coll_name[20]; %f\n",rec2.id,rec2.percentage);
ptr1=&rec1;
}; struct student s;
memcpy(&rec3,&ptr1,sizeof(rec1)
}stu_data; );
struct stud_detail stu_data; printf("%d\n",rec3->id);
stu_data.s.id=500; rec4.id=rec1.id;
strcpy(stu_data.s.name,”SHRI”); printf("%d\n",rec4.id);
}
STRUCTURES AS FUNCTION ARGUMENTS
PASS BY VALUE PASS BY REFERENCE GLOBAL DECLARATION
#include <stdio.h> #include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h> #include <string.h>
struct Books { struct Books { struct Books {
char title[50]; char title[50]; char title[50];
int book_id;}; int book_id;}; int book_id;};
void printBook( struct Books book ); void printBook( struct Books *book struct Books Book1;
int main( ) { );
struct Books Book1; int main( ) { void printBook();
strcpy( Book1.title, "C struct Books Book1; int main( ) {
Programming"); strcpy( Book1.title, "C strcpy( Book1.title, "C
Book1.book_id = 6495407; Programming"); Programming");
printBook( Book1 ); Book1.book_id = 6495407; Book1.book_id = 6495407;
return 0;} printBook( &Book1 ); printBook( );
void printBook( struct Books book ) { return 0;} return 0;}
printf( "Book title : %s\n", void printBook( struct Books *book void printBook( )
book.title); ){ {
printf( "Bookid : printf( "Book title : %s\n", book- printf( "Book title : %s\n",
%d\n",book.book_id); >title); Book1.title);
} printf( "Bookid : %d\n",book- printf( “Bookid :
>book_id); %d\n",Book1.book_id);
} }
STRUCTURE POINTER / POINTERS TO STRUCTURE
struct Point
{
int x, y;
};
int main() OUTPUT : 1 2
{
struct Point p1 = {1, 2};
// p2 is a pointer to structure p1
struct Point *p2 = &p1;
// Accessing structure members using structure pointer
printf("%d %d", p2->x, p2->y);
return 0;
}
BIT FIELDS
• In C, we can specify size (in bits) of structure and union members
• SYNTAX –
struct { type [member_name] : width ; };
• Example -
struct packed_struct {
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int f4:1;
unsigned int type:4;
unsigned int my_int:9; } pack;
ALIGNMENT OF DATA IN STRUCTURES
• Compilers add “padding” to structs to ensure proper alignment of data.

struct {
char a; //1 byte + 3 bytes padding
int b; //4 bytes a
char c; //1 byte + 3 bytes padding b b b b
total = 12 bytes
}; c
Pad
struct{
int b;//4 bytes a
char a;//1 byte b b b b
char c;//1 byte
//2 bytes of padding total = 8 bytes c
};
UNION
• A union is a special data type available in C that allows to store different data types in the same
memory location.
• SYNTAX
union [union tag] {
member definition;
member definition;
member definition; }
[one or more union variables];
• Example union AnElt {
int i;
char c;
} elt1, elt2;

elt1.i = 4;
elt2.c = ’a’;
elt2.i = 0xDEADBEEF;
ACCESSING UNION MEMBERS
• Using (.) dot operator

• POINTERS TO UNION --- Using (->) indirection operator


union test{
int x;
char y;
};
int main() OUTPUT : 65 A
{
union test p1;
p1.x = 65;
// p2 is a pointer to union p1
union test *p2 = &p1;
// Accessing union members using pointer
printf("%d %c", p2->x, p2->y);
return 0;
}
STRUCTURE VS UNION

STRUCTURE UNION

Keyword struct defines a structure. Keyword union defines a union.

Compiler allocates memory for all members within Compiler allocates the memory for the largest of all
structure. members within union.

Within a structure all members gets memory allocated; While retrieving data from a union the type that is
therefore any member can be retrieved at any time. being retrieved must be the type most recently stored.

One or more members of a structure can be initialized A union may only be initialized with a value of the
at once. type of its first member
TYPEDEF
• The C programming language provides a keyword called typedef, which you can use to give a
type, a new name.
• Example :
typedef long int64_t;
typedef struct ADate {
int month;
int day;
int year;
} Date;

int64_t i = 100000000000;
Date d = {1, 21, 2016};
TYPEDEF VS MACRO

TYPEDEF MACRO
Typedef is used to create a new name to an already Macros [#define] are a direct substitution of the text
existing data type before compling the whole code
eg: #define chPointer char *
typedef unsigned int UINT32 #undef chPointer
#define chPointer int *
typedef interpretation is performed by the compiler #define statements are processed by the pre-processor
ENUMERATION (ENUM)

• An enumeration is a user defined data type in C used to assign names to integral constants
• To define an enumeration keyword ENUM is used
• SYNTAX --
enum identifier {value1, value2,.... Value n};
• Example :
enum State {Working = 1, Failed = 0};
enum State {Working = 1, Failed = 0, Freezed = 0};
enum month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC};
enum day {sunday = 1, monday, tuesday = 5, wednesday, thursday = 10, friday, saturday};

Das könnte Ihnen auch gefallen