UNIT-IV (C Programming I BSC)
UNIT-IV (C Programming I BSC)
X 10
5000
p 5000 5048
Advantages of pointers:
1. Pointers are more efficient in handling the data tables.
2. Pointers reduce the length and complexity of a program.
3. They increase the execution speed.
Disadvantages of Pointer
• Pointers are little confusing and difficult to understand for beginner.
• Compiler may not detect the error when there is wrong usage of pointers. In
such cases program is likely to produce unexpected results.
Declaring a Pointer
Initializing Pointers
The process of assigning the address of a variable to a pointer is known as pointer
initialization.
Once a pointer variable has been declared, it can be initialized using assignment
operator and address operator
Example: p=&x;
which causes p to point to x. Now p contains the address of x.
• Before a pointer is initialized, it should not be used.
Pointer expressions
Pointer variables can be used in expressions.
Example: if p1 and p2 are properly declared and initialized pointers then the
following statements are valid.
X= *p1 * *p2;
sum=sum+ *p1;
*p2=*p2+10;
• Can add integers to or subtract integers from pointers as well as to subtract one
pointer from another.
p1+4,p2-2 and p1-p2 are all allowed.
• also use short-hand operators with pointers.
p1++;
--p2;
sum+=*p2;
• Pointers can also be compared using relational operators.
p1>p2, p1==p2,p1!=p2 are allowed.
• Cannot use pointers in multiplication, addition or division.
p1/p2, p1+p2, p1*p2 are not allowed.
When handling arrays, instead of using array indexing, we can use pointers to access
array elements.
Example: *(p+3) gives the value of x[3].
• Pointers can be used to manipulate two-dimensional array
1. The program may become too large and complex so that debugging becomes
difficult.
2. Testing & maintenance becomes difficult.
3. For programs, which involve several repetitions of the same task, main() function
grows lengthy and leads to confusion.
4. More comment lines are required to be written to make the program
understandable.
To overcome all these difficulties, the program can be divided into a number
of user-defined functions & then each of them may be independently coded and later
combined into a single unit.
i. return type (or function type): It denotes the data type of the value returned by
the function.
• Return type can be any data type such as int , float, char or void.
• If the return type is omitted (skipped) in the declaration then the function is
assumed to return integer data type.
• void data type is used when the function does not return any value to the calling
function.
ii. Function Name: Function name can be any valid C identifier that is used to
uniquely identify the user defined function.
iii. Argument list with declaration (Parameter list): The argument list indicates the
set of variables that will receive the values sent by the calling function. Like every
other variables, the argument list should be declared along with their data type.
i). Local variable declaration that specify the variables needed by the function.
These variables are local to the function and are not available outside this
function.
ii). Executable statements are the statements that are required to perform the actual
task.
iii). A return statement is used to return the resultant value of the function to the
calling program.
When the compiler encounters a function call, the values of actual parameters
are copied to the formal parameters and the control is transferred from the calling
function to the called function.
All functions in C program must be declared, before they are invoked. The
declaration of a function before using it, is called as a function prototype.
OR
return_type function_name( type1 Arg1, type2 Arg2, ....);
Function declaration consists of four parts:
• return-type
• function name
• Parameter list
• Terminating semicolon
Example: a function that is used to add two integer numbers and return their sum
can have a typical prototype as
Following points are considered while defining the formal and actual parameters.
• The number of formal parameters should be equal to the number of actual
arguments.
Return Statement
The return statement is used to send back values from the called function to
the calling function. A function can return none or at most one value per call. The
general form of return statement is,
return;
Or
return (expression);
The first return statement does not return any value to the calling function. It
is used to exit from the function and return the control to the point from where it is
called. The second statement returns a single value of the expression to the calling
function.
Example:
#include<stdio.h>
void main()
{
void sum ( ); //Function Prototype
printf(“Finding Sum:”);
sum( ); //Function Call
}
void sum ( )
{
int a, b,s; // Local Variable
printf(“Enter two numbers:”);
scanf(“%d %d”, &a, &b);
s = a+b;
printf(“Sum of two numbers =%d”,s);
}
In the above example the calling function “main()” calls the function “sum()”.
Example:
#include<stdio.h>
void main()
{
int s1;
int sum ( ); //Function Prototype
int sum ( )
{
int a,b.s;
printf(“Enter two numbers:”);
scanf(“%d %d”, &a, &b);
s = a+b;
return(s);
}
Example:
#include<stdio.h>
void main()
{
int a, b, s1;
int sum (int, int); //Function Prototype
printf(“Enter two numbers:”);
scanf(“%d %d”, &a, &b);
s1 = sum(a, b); //Function Call
Nesting Of Functions
STRUCTURE
Structure is a collection of logically related data items of same or different
data types.
Structure is a user defined data type.
Advantages Of Structure
• Structures helps to organize complex data in a more meaningful way because
each variable can represent one piece of information and array can store same
type information.
• Structure is a convenient tool for handling a group of logically related data
items as accessing and manipulating structure members is easy and time saving.
Defining A Structure
Structure is defined with a keyword struct, followed by the name of the
structure.
The general format of a structure definition:
struct tag_name
{
data-type member1;
data-type member2;
___
___
data-type Membern;
};
● Tag_name is the name of the structure.
● The Members refers to the data elements of the structure. The list of all
structure members is called as a template. The template informs the compiler
about the amount of memory to reserve for each of structure variable.
● Structure members should be grouped together in a flower bracket.
● a semicolon is used to terminate the structure definition.
Example: A structure to hold students information
struct student
{ int regno;
char name[15];
float fees;
};
This declaration creates two structure variables S1, and S2 of type student.
This declares the student as an array of three elements student[0], student[1], and
student [2] and initializes their members as follows:
Table of Records
student[0].m1 = 45; m1 m2 m3
student[0].m2 =68;
Student[0]
.......
Student[1]
.......
Student[2]
student[2].m3 = 71;
Here, the member subject contains three elements subject[0], subject[1] and
subject[2]. These elements can be accessed using appropriate subscripts.
#include <stdio.h>
struct student
{
char sname[20];
int marks[3];
}s1;
void main()
{
printf("\nEnter the Name of Student : ");
gets(s1.sname);
struct student
{ int regno;
char name[20];
struct date dob;
};
Note: The embedded structure ’date’ (in case 2) must be defined before the outer
structure ‘Student’.
For example:
struct Book
{ char title[20];
float price;
int pages;
};
struct Book Update (struct book); /* Function Prototype */
UNIT-IV C PROGRAMMING (I BSC) 25
void main()
{ struct Bok B1={ “Let Us C”, 220.00, 550 };
printf(“The details of book B1 before reprinting\n”);
printf(“Name : %s Price: %.2f Pages: %d\n”,B1.title, B1.price, B1.pages);
B1 = Update(B1); /* Function Call – Passing structure B1 as parameter */
printf(“\nThe details of book B1 after reprinting\n”);
printf(“Name : %s Price: %.2f Pages: %d\n”,B1.title, B1.price,B1.pages);
}
Structure Array
Structures uses dot operator to access its Array uses index or subscript to access
members. its elements.
In structure there is no such thing as one Array can be one dimensional or multi
dimensional or multi dimensional dimensional.
Two structure variables can be assigned to Assigning one array variable to another
simultaneously copy the contents of will not copy the contents of one array
individual members within the structures. onto another.
Structures are user-defined data types. Array behaves like a built in data type.
Example:
union Item
{
int m;
float p;
char c;
};
Where variable list is a list of any valid identifier that are separated by comma.
Example: union Item purchased;
void main()
{ union item a;
a.m = 100;
printf (“m = %d\n”, a.m); OUTPUT:
a.x = 315.27; m = 100
printf ( “x = %.2f\n”, a.x);
x = 315.27
a.m = 215;
Now m = 215
printf(“Now m = %d\n”, a.m);
}
STRUCTURE UNION