0% found this document useful (0 votes)
15 views17 pages

Unit 3

module 3 contains pointers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views17 pages

Unit 3

module 3 contains pointers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

{

int num;

printf(“enter a number”);

scanf(“%d”,&num);

f=fact(num);

printf(“factorial is =%d\n”f);

fact (int num)

If (num==0||num==1)

return 1;

else

return(num*fact(num-1));

Lecture Note: 18

Monolithic Programming

The program which contains a single function for the large program is called
monolithic program. In monolithic program not divided the program, it is huge
long pieces of code that jump back and forth doing all the tasks like single thread
of execution, the program requires. Problem arise in monolithic program is that,
when the program size increases it leads inconvenience and difficult to maintain

81 *Under revision
such as testing, debugging etc. Many disadvantages of monolithic programming
are:

1. Difficult to check error on large programs size.

2. Difficult to maintain because of huge size.

3. Code can be specific to a particular problem. i.e. it cannot be reused.

Many early languages (FORTRAN, COBOL, BASIC, C) required one huge


workspace with labelled areas that may does specific tasks but are not isolated.

Modular Programming

The process of subdividing a computer program into separate sub-programs such


as functions and subroutines is called Modular programming. Modular
programming sometimes also called as structured programming. It
enables multiple programmers to divide up the large program and debug
pieces of program independently and tested.
. Then the linker will link all these modules to form the complete program. This
principle dividing software up into parts, or modules, where a module can be
changed, replaced, or removed, with minimal effect on the other software it works
with. Segmenting the program into modules clearly defined functions, it can
determine the source of program errors more easily. Breaking down program
functions into modules, where each of which accomplishes one function and
contains all the source code and variables needed to accomplish that function.
Modular program is the solution to the problem of very large program that are
difficult to debug, test and maintain. A program module may be rewritten while its
inputs and outputs remain the same. The person making a change may only
understand a small portion of the original program.

Object-oriented programming (OOP) is compatible with the modular programming


concept to a large extent.
. , Less code has to be written that makes shorter.

82 *Under revision
• A single procedure can be developed for reuse, eliminating the need to
retype the code many times.
• Programs can be designed more easily because a small team deals with only
a small part of the entire code.
• Modular programming allows many programmers to collaborate on the same
application.
• The code is stored across multiple files.
• Code is short, simple and easy to understand and modify, make simple to
figure out how the program is operate and reduce likely hood of bugs.
• Errors can easily be identified, as they are localized to a subroutine or
function or isolated to specific module.
• The same code can be reused in many applications.
• The scoping of variables and functions can easily be controlled.

Disadvantages
However it may takes longer to develop the program using this technique.

Storage Classes

Storage class in c language is a specifier which tells the compiler where and how to
store variables, its initial value and scope of the variables in a program. Or
attributes of variable is known as storage class or in compiler point of view a
variable identify some physical location within a computer where its string of bits
value can be stored is known as storage class.

The kind of location in the computer, where value can be stored is either in the
memory or in the register. There are various storage class which determined, in
which of the two location value would be stored.

Syntax of declaring storage classes is:-

storageclass datatype variable name;

There are four types of storage classes and all are keywords:-

1 ) Automatic (auto)

83 *Under revision
2 ) Register (register)

3) Static (static)

4 ) External (extern)

Examples:-

auto float x; or float x;

extern int x;

register char c;

static int y;

Compiler assume different storage class based on:-

1 ) Storage class:- tells us about storage place(where variable would be stored).

2) Intial value :-what would be the initial value of the variable.

If initial value not assigned, then what value taken by uninitialized variable.

3) Scope of the variable:-what would be the value of the variable of the program.

4) Life time :- It is the time between the creation and distribution of a variable
or how long would variable exists.

1. Automatic storage class

The keyword used to declare automatic storage class is auto.

Its features:-

Storage-memory location

Default initial value:-unpredictable value or garbage value.

84 *Under revision
Scope:-local to the block or function in which variable is defined.

Life time:-Till the control remains within function or block in which it is defined.
It terminates when function is released.

The variable without any storage class specifier is called automatic variable.

Example:-

main( )

auto int i;

printf(“i=”,i);

Lecture Note: 19

2. Register storage class

The keyword used to declare this storage class is register.

The features are:-

Storage:-CPU register.

Default initial value :-garbage value

Scope :-local to the function or block in which it is defined.

Life time :-till controls remains within function or blocks in which it is defined.

Register variable don’t have memory address so we can’t apply address operator
on it. CPU register generally of 16 bits or 2 bytes. So we can apply storage classes
only for integers, characters, pointer type.

85 *Under revision
Variable stored in register storage class always access faster than,which is always
stored in the memory. But to store all variable in the CPU register is not possible
because of limitation of the register pair.

And when variable is used at many places like loop counter, then it is better to
declare it as register class.

Example:-

main( )

register int i;

for(i=1;i<=12;i++)

printf(“%d”,i);

3 Static storage class

The keyword used to declare static storage class is static.

Its feature are:-

Storage:-memory location

Default initial value:- zero

Scope :- local to the block or function in which it is defined.

Life time:- value of the variable persist or remain between different function call.

Example:-

main( )

86 *Under revision
{

reduce( );

reduce( );

reduce ( );

reduce( )

static int x=10;

printf(“%d”,x);

x++;

Output:-10,11,12

External storage classes

The keyword used for this class is extern.

Features are:-

Storage:- memory area

Default initial value:-zero

Scope :- global

Life time:-as long as program execution remains it retains.

87 *Under revision
Declaration does not create variables, only it refer that already been created at
somewhere else. So, memory is not allocated at a time of declaration and the
external variables are declared at outside of all the function.

Example:-

int i,j;

void main( )

printf( “i=%d”,i );

receive( );

receive ( );

reduce( );

reduce( );

receive( )

i=i+2;

printf(“on increase i=%d”,i);

reduce( )

i=i-1;

printf(“on reduce i=%d”,i);

88 *Under revision
Output:-i=0,2,4,3,2.

When there is large program i.e divided into several files, then external variable
should be preferred. External variable extend the scope of variable.

Lecture Note: 20

POINTER

A pointer is a variable that store memory address or that contains address of


another variable where addresses are the location number always contains whole
number. So, pointer contain always the whole number. It is called pointer because
it points to a particular location in memory by storing address of that location.

Syntax-

Data type *pointer name;

Here * before pointer indicate the compiler that variable declared as a pointer.

e.g.

int *p1; //pointer to integer type

float *p2; //pointer to float type

char *p3; //pointer to character type

When pointer declared, it contains garbage value i.e. it may point any value in the
memory.

89 *Under revision
Two operators are used in the pointer i.e. address operator(&) and indirection
operator or dereference operator (*).

Indirection operator gives the values stored at a particular address.

Address operator cannot be used in any constant or any expression.

Example:

void main()

int i=105;

int *p;

p=&i;

printf(“value of i=%d”,*p);

printf(“value of i=%d”,*/&i);

printf(“address of i=%d”,&i);

printf(“address of i=%d”,p);

printf(“address of p=%u”,&p);

Pointer Expression

Pointer assignment

int i=10;

int *p=&i;//value assigning to the pointer

90 *Under revision
Here declaration tells the compiler that P will be used to store the address of
integer value or in other word P is a pointer to an integer and *p reads the value at
the address contain in p.

P++;

printf(“value of p=%d”);

We can assign value of 1 pointer variable to other when their base type and data
type is same or both the pointer points to the same variable as in the array.

Int *p1,*p2;

P1=&a[1];

P2=&a[3];

We can assign constant 0 to a pointer of any type for that symbolic constant
‘NULL’ is used such as

*p=NULL;

It means pointer doesn’t point to any valid memory location.

Pointer Arithmetic

Pointer arithmetic is different from ordinary arithmetic and it is perform relative to


the data type(base type of a pointer).

Example:-

If integer pointer contain address of 2000 on incrementing we get address of 2002


instead of 2001, because, size of the integer is of 2 bytes.

Note:-

When we move a pointer, somewhere else in memory by incrementing or


decrement or adding or subtracting integer, it is not necessary that, pointer still
pointer to a variable of same data, because, memory allocation to the variable are
done by the compiler.

91 *Under revision
But in case of array it is possible, since there data are stored in a consecutive
manner.

Ex:-

void main( )

static int a[ ]={20,30,105,82,97,72,66,102};

int *p,*p1;

P=&a[1];

P1=&a[6];

printf(“%d”,*p1-*p);

printf(“%d”,p1-p);

Arithmetic operation never perform on pointer are:

addition, multiplication and division of two pointer.

multiplication between the pointer by any number.

division of pointer by any number

-add of float or double value to the pointer.

Operation performed in pointer are:-

/* Addition of a number through pointer */

Example

int i=100;

int *p;

92 *Under revision
p=&i;

p=p+2;

p=p+3;

p=p+9;

ii /* Subtraction of a number from a pointer’*/

Ex:-

int i=22;

*p1=&a;

p1=p1-10;

p1=p1-2;

iii- Subtraction of one pointer to another is possible when pointer variable point to
an element of same type such as an array.

Ex:-

in tar[ ]={2,3,4,5,6,7};

int *ptr1,*ptr1;

ptr1=&a[3]; //2000+4

ptr2=&a[6]; //2000+6

Lecture Note: 21

93 *Under revision
Precedence of dereference (*) Operator and increment operator and
decrement operator

The precedence level of difference operator increment or decrement operator


is same and their associatively from right to left.

Example :-

int x=25;

int *p=&x;

Let us calculate int y=*p++;

Equivalent to *(p++)

Since the operator associate from right to left, increment operator will applied to
the pointer p.

i) int y=*p++; equivalent to *(p++)

p =p++ or p=p+1

ii) *++p;→*(++p)→p=p+1

y=*p

iii) int y=++*p

equivalent to ++(*p)

p=p+1 then *p

iv) y=(*p)++→equivalent to *p++

y=*p then

P=p+1 ;

Since it is postfix increment the value of p.

Pointer Comparison

94 *Under revision
Pointer variable can be compared when both variable, object of same data type
and it is useful when both pointers variable points to element of same array.

Moreover pointer variable are compared with zero which is usually expressed as
null, so several operators are used for comparison like the relational operator.

==,!=,<=,<,>,>=, can be used with pointer. Equal and not equal operators used to
compare two pointer should finding whether they contain same address or not and
they will equal only if are null or contains address of same variable.

Ex:-

void main()

static int arr[]={20,25,15,27,105,96}

int *x,*y;

x=&a[5];

y=&(a+5);

if(x==y)

printf(“same”);

else

printf(“not”);

Lecture Note: 22

95 *Under revision
Pointer to pointer

Addition of pointer variable stored in some other variable is called pointer to


pointer variable.

Or

Pointer within another pointer is called pointer to pointer.

Syntax:-

Data type **p;

int x=22;

int *p=&x;

int **p1=&p;

printf(“value of x=%d”,x);

printf(“value of x=%d”,*p);

printf(“value of x=%d”,*&x);

printf(“value of x=%d”,**p1);

printf(“value of p=%u”,&p);

printf(“address of p=%u”,p1);

printf(“address of x=%u”,p);

printf(“address of p1=%u”,&p1);

printf(“value of p=%u”,p);

printf(“value of p=%u”,&x);

P 2000

X 1000

p1 22

96 *Under revision
3000

Pointer vs array

Example :-

void main()

static char arr[]=”Rama”;

char*p=”Rama”;

printf(“%s%s”, arr, p);

In the above example, at the first time printf( ), print the same value array and
pointer.

Here array arr, as pointer to character and p act as a pointer to array of


character . When we are trying to increase the value of arr it would give the error
because its known to compiler about an array and its base address which is always
printed to base address is known as constant pointer and the base address of array
which is not allowed by the compiler.

printf(“size of (p)”,size of (ar));

size of (p) 2/4 bytes

size of(ar) 5 byes

97 *Under revision

You might also like