• No results found

ARRAYS WITH IN STRUCTURES :

In document C Notes (Page 49-54)

C permits the use of arrays as structure members. We have already used arrays of

characters inside a structure. Similarly, we can use single or multi-dimentional arrays of type int or float. struct marks { int number; float subject[3]; }student[2];

Here, the member subject contains three elements, subject[0], subject[1], and subject[2]. These elements can be accessed using approprite subscripts.

For example, the name student[1].subject[2];

would refer to the marks obtained in the third subject by the second student. STRUCTURES WITHIN STRUCTURES

Structures within a structrue means nesting of structures. Nesting of structures is permitted in C. Let us consider the following strucutrue defined to store information about the salary of employees. struct salary { char name[20]; char dept[10]; int basic; int da; int ca; }employee;

This structure defnes name, department, basic pay and three kinds ofallowances. We can group all the items related to allowance together anddeclare them under a sub structure as shown below:

struct salary {

char dept[10]; struct { int da; int hra; int ca; } allowance; } employee;

The salary structure contians a member named allowance which itself is a structure with three members. The members contained in the inner structurenamely da, hra, and ca can be referred to as

employee.allowance.da employee.allowace.hra employee.allowance.ca.

An innner-most member in a nested structure can be accessed by chaining all the concerned structure variables with the member using dot operator.

/* Example for Structure with in structure */ #include<stdio.h> #include<conio.h> void main() { struct address { char phone[10]; char city[25]; int pin; }; struct emp { char name[20]; struct address a; };

struct emp e={"sunilkumar","531046","nagpur",1238}; clrscr();

printf("\n name = %s phone=%s ",e.name,e.a.phone); printf("\n city = %s pin=%d ",e.a.city,e.a.pin);

getch(); } Structure Pointer: #include<stdio.h> #include<conio.h> void main() { struct book {

char name[10]; float price; int pages; };

struct book b1={"basic",130.00,550}; struct book *b2;

clrscr(); b2=&b1;

printf("\n name1 = %s",b1.name); printf("\n price1 = %f",b1.price); printf("\n pages1 = %d",b1.pages); printf("\n name2 = %s",b2->name); printf("\n price2 = %f",b2->price); printf("\n pages2 = %d",b2->pages); getch();

}

UNIONS:

Unions are a concept borrowed from sturectures and therefore follow the same syntax as structures. However, there is major distinction between them in terms of storage. In structures, each member has its own storage

location, whereas all the members of a union use the same location. This implies that, although a union may contain many members of different types, it can handle only one member at a time. Like structures, a union can be

declared using the keyword union as follows: union item { int m; float x; char c; } code;

This declares a variable code of type union item. The union contains three members, each with a different data type. However, we can use only one of them at a time. This is due to fact that only one location is allocated

for a union variable, irrespective of its size. storage of 4 bytes 1000 1001 1002 1003 --c --

--- m ---

--- x ---

In declaration the member x require 4 bytes which is the largest among the members. All the three variables share the same address.To access a union member, we can use the same syntax that we use for

structure members. That is,

code.m code.x code.c

are all valid member variable. In union any one of its members at a time can be used. #include<stdio.h>

#include<conio.h> void main()

{

{ char name[10]; float price; int pages; }; union book b1,b2; clrscr();

printf("\n enter name :"); scanf("%s",b1.name);

printf("\n address of name1 = %u",&b1.name); printf("\n name1 = %s",b1.name);

printf("\n enter price :"); scanf("%f",&b1.price);

printf("\n address of price1 = %u",&b1.price); printf("\n price1 = %f",b1.price);

printf("\n enter pages :"); scanf("%d",&b1.pages);

printf("\n address of pages1 = %u",&b1.pages); printf("\n pages1 = %d",b1.pages);

printf("\n\n enter name :"); scanf("%s",b2.name);

printf("\n address of name2 = %u",&b2.name); printf("\n name2 = %s",b2.name);

printf("\n enter price :"); scanf("%f",&b2.price);

printf("\n address of price2 = %u",&b2.price); printf("\n price2 = %f",b2.price);

printf("\n enter pages :"); scanf("%d",&b2.pages);

printf("\n address of pages2 = %u",&b2.pages); printf("\n pages2 = %d",b2.pages);

getch(); }

Enum

It gives you an opportunity to invent your own data type and define what values the variable of this datatype can take.

The format of the enum definition is similar to that of a structure. Example:

Enum mar_status{

Single,married,divorced,widowed; };

Enum mar_status person1,person2;

The first part declares the data type and specifies it’s possible values. These values are called “enumerators”

#include<stdio.h> #include<conio.h>

#include<string.h> void main()

{

enum emp_dept {

assembly, manufacturing, accounts, stores }; struct employee { char name[15]; int age; float bs;

enum emp_dept dept; };

struct employee e1; clrscr();

strcpy(e1.name,"poonam bajwa"); e1.age=22;

e1.bs=18765.00;

e1.dept=manufacturing;

printf("\n name = %s ",e1.name); printf("\n age= %d ",e1.age); printf("\n basic salary = %f",e1.bs); printf("\n department = %d",e1.dept); getch();

}

POINTERS

INTRODUCTION:

Pointers are another important feature of C language. Although they may appear a little

confusing for beginner, but they are a powerful tool and handy to use once they are mastered. There are a number of reasons for using pointers.

1. A pointer enables us to access a variable that is defined outside thefunction. 2. Pointers are more efficient in handling the data tables.

3. Pointers reduce the length and complexity of a program. 4. They increase the execution speed.

5. The use of a pointer array to character strings results in saving of data storage space in memory.

The real power of C lies in the proper use of pointers. UNDERSTANDING POINTERS:

We know that computers use their memory for storing the instructions of a program, as well as the values of a the variables that are associated with it. The computer’s memory is a sequential collection of ‘storage cells’. Each cell, commonly known as a byte, has a number called address associated with it. Typically, the addresses are numbered consecutively, starting from zero. The last address depends on the memory size. A computer system

having 1KB memory will have its last address as (1024-1) 1023. Whenever we declare a variable, the system allocates, somewhere in the memory, an appropriate location to hold the value of the variable. Since,

every byte has a unique address number, this location will have its own address number. This statement instructs the system to find a location for the integer variable quantity and puts the value 179 in that location. Let us assume that the system has chosen the address location 786 for quantity. Since memory addresses are simply numbers, they can be assigned to some variables which can be stored in memory, like any other variable. Such variables that hold memory addresses are called pointers. A pointer is, nothing but a variable in which address of another variable is stored.

In document C Notes (Page 49-54)

Related documents