Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: Structures C Programming
Description: Structures - C Programming, Structures and union - C Programming

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


STRUCTURES
Structures - C Programming
Array
Single name that contains a collection of data items of same data type
...

No Keyword
Members of an array are stored in sequence of memory locations
...

Individual entries in a structure are called members
...


STRUCTURES
Structure is a collection of variables under the single name, which can be
of different data type
...


A structure is a derived data type, The scope of the name of a structure
member is limited to the structure itself and also to any variable declared
to be of the structure's type
...

Syntax: In general terms, the composition of a structure may be defined
as:
struct
{
member 1;
member 2;

member m;


where, struct is a keyword, is a structure name
...
we can declare the structure
variable in two ways:
i) within the structure definition itself
...

i]within the structure definition itself
...

struct tag
{
member 1;
member 2;
——

member m; };
void main()
{

struct tag var1, var2,
...


Note: Structure variable can be any of three types: normal variable,
array_variable, pointer_variable
...
struct student
{
char name [80]; int roll_no; float marks ;
} s1={“Saravana”,34,469};
II
...
name={“Saravana”};

s1
...
marks=500;
It is also possible to define an array of structure, that is an array in which
each element is a structure
...
It means each
element of st represents an individual student record
...

Therefore, we must be able to access the individual structure members
...
member_name //[normal variable]
Eg:

struct student
{
char name [80]; int roll_no ; float marks ; }st;
e
...
if we want to get the detail of a member of a structure then we can
write as scanf(“%s”,st
...
roll_no) and so on
...
name); or printf(“%d”, st
...

The use of the period operator can be extended to arrays of structure by
writing:
array [expression]
...
g
...
name); or scanf(“%d”, &st[i]
...

if we want to print the detail of a member of a structure then we can write
as
printf(“%s”,st[i]
...
roll_no) and so on
...
g
...


if we want to print the detail of a member of a structure then we can write
as
printf(“%s”,st>name); or printf(“%d”, st>roll_no) and so on
...

PROGRAM’S USING STRUCTURE
Example 1://To print the student details// normal structure variable
#include ...
name);

printf(“\n Enter the student register number::”); scanf(“%s”,&s1
...
branch);
printf(“\n
Student
Name::”,s1
...
branch); printf(“\n Student reg_no::”,s1
...
h>
struct name{
int a;
float b;

int main()
{
struct name *ptr,p;
ptr=&p; /* Referencing pointer to memory address of p */

printf("Enter integer: "); scanf("%d",&(*ptr)
...
b);
printf("Displaying:
");
printf("%d%f",(*ptr)
...
b); return 0;
}
Example 3://To print the Mark sheet for a student//
#include ...
name);
printf(“\n Enter the student register number::”);
scanf(“%s”,&s1
...
branch);
printf(“\n Enter the 3 subjects Marks:”);
scanf(“%d%d%d”, &s1
...
m2,&s1
...
total=s1
...
m2+s1
...
%d”, s1
...
avg=a1
...
%f”, s1
...
h"

#include"conio
...
name, &sa[i]
...
s1,
&sa[i]
...
s3, &sa[i]
...
s5);
sa[i]
...
s1+sa[i]
...
s3+sa[i]
...
s5;
sa[i]
...
total/5;
if((sa[i]
...
s2<50)||(sa[i]
...
s4<50)||(sa[i]
...
grade='U';
printf("Ur grade is %c", sa[i]
...
avg==100)
{
sa[i]
...
grade);

}
if((sa[i]
...
avg<=99))
{
sa[i]
...
grade);
}

Structures and Unions - C Programming
We studied earlier that array is a data structure whose element are all of
the same data type
...
Thus a single
structure might contain integer elements, floating– point elements and
character elements
...
The individual structure elements
are referred to as members
...
We will see how structures are defined,
and how their individual members are accessed and processed within a
program
...
Closely associated with the structure is
the union, which also contains multiple members
...


The individual members can be ordinary variables, pointers, arrays or
other structures
...
A storage class, however,
cannot be assigned to an individual member, and individual members
cannot be initialized within a struc-ture-type declaration
...

It is possible to combine the declaration of the structure com-position
with that of the structure variable as shown below
...

struct student {
char name [80];
int roll_no;
float marks;
}s1,s2;
The s1, s2, are structure variables of type student
...
As a result, the above
declaration can also be written as

struct{
char name [80];
int roll_no;
float marks ;
} s1, s2, ;
A structure may be defined as a member of another structure
...
The members of a structure variable
can be assigned initial values in much the same manner as the elements of
an array
...
The general form is

storage-class struct tag variable = { value1, value 2,-------, value m};
A structure variable, like an array can be initialized only if its stor-age
class is either external or static
...
g
...


struct dob
{ int month; int day;

int year; };
struct student
{ char name [80]; int roll_no;
float marks; struct dob d1; };
static struct student st = { “ param”, 2, 99
...
The procedure is shown in the following
example:
struct student{
char name [80];
int roll_no ;
float marks ;
} st [100];
In this declaration st is a 100- element array of structures
...

4 PROCESSING A STRUCTURE

The members of a structure are usually processed individually, as
separate entities
...
A structure member can be accessed by writing

variable
...

This period (
...

e
...
if we want to print the detail of a member of a structure then we can
write as
printf(“%s”,st
...
roll_no) and so on
...
For example, if a structure member is itself a structure,
then a member of the embedded structure can be ac-cessed by writing
...
member
...

Thus in the case of student and dob structure, to access the month of date
of birth of a student, we would write
st
...
month
The use of the period operator can be extended to arrays of struc-ture, by
writing
array [expression]
...
Single-valued structure mem-bers can

appear in expressions
...

e
...
suppose that s1 and s2 are structure variables having the same
composition as described earlier
...
Let us consider
an example of structure:
#include ...
marks <80) st[i]
...
grade='A'+;
}
for (i= ; i}
void readinput (int i)
{
printf(“\n student no % \n”, i+1); printf(“Name:”);
scanf(“%[^\n]”,
st[i]
...
name);

printf(“Roll number”);

printf(“Address:”);

scanf(“%[^\n]",

scanf(“%d”, &st[i]
...
marks);
printf(“Date of Birth {mm/dd/yyyy)”);
scanf(“%d%d%d”, & st[i]
...
month & st[i]
...
day, & st[i]
...
year);
return;
}
void writeoutput(int i)
{
printf(“\n Name:%s”,st[i]
...
address);
printf(“Marks % f \n”, st[i]
...
roll_no);
printf(“Grade %c\n”,st[i]
...
This information can be obtained through the use of
the sizeof operator
...
Once a user-defined data type has been
established, then new variables, arrays, structure and so on, can be
declared in terms of this new data type
...

e
...
typedef int age;
In this declaration, age is user- defined data type equivalent to type int
...
As a result, the structure can be referenced more
concisely
...

6 STRUCTURES AND POINTERS
The beginning address of a structure can be accessed in the same manner
as any other address, through the use of the address (&) operator
...
We can declare a pointer
variable for a structure by writing

type *ptr;
Where type is a data type that identities the composition of the structure,
and ptr represents the name of the pointer variable
...
Thus,
the beginning address of s1 can be assigned to ps by writing
...
) operator
...

The operator → can be combined with the period operator (
...
Hence, a submember can be accessed by
writing
ptr → member
...
Structure member can be transferred indi-vidually , or
entire structure can be transferred
...
To do so, each
structure member is treated the same way as an ordinary, single- valued
variable
...
It should be understood that a
structure passed in this manner will be passed by reference rather than by
value
...
Let us consider the
following example:
# include ...
9};
printf(“%s%d%f\n”, student
...
roll_no,student
...
name, student
...
marks);
}
void adj(record*ptr)
{
ptr → name=”Tanishq”;

ptr → roll_no=3;
ptr → marks=98
...

# include ...
9};
printf(“%s%d%f\n”, student
...
roll_no,student
...
name,student
...
marks);
}

void adj(record stud) /*function definition */
{
stud
...
roll_no=3; stud
...
0; return;
}

8 UNIONS
Union, like structures, contain members whose individual data types may
differ from one another
...
Thus, unions are used to conserve memory
...
Individual union vari-ables can then
be declared as storage-class union tag variable1, variable2, -----, variable

n; where storage-class is an optional storage class specifier, union is a
required keyword, tag is the name that appeared in the union definition
and variable 1, variable 2, variable n are union variables of type tag
...
Thus, we can write
...
, variable n;
The tag is optional in this type of declaration
...

Each variable can represent either a 5–character string (color) or an
integer quantity (size) of any one time
...

An individual union member can be accessed in the same manner as an
individual structure members, using the operators (→) and
...
member refers to a
member of the union
...

Let us consider the following C program:
# include ...
detail
...
detail
...
detail
...
detail
...
detail
...
detail
...
In line two, the first data item [B] is
meaningful, but the second is not
...
A union
variable can be initialized, provided its storage class is either external or
static
...
Unions are processed in the same manner, and with the same
restrictions as structures
...



Title: Structures C Programming
Description: Structures - C Programming, Structures and union - C Programming