KEMBAR78
Module 4 Pointers | PDF | Pointer (Computer Programming) | Computer Program
0% found this document useful (0 votes)
13 views7 pages

Module 4 Pointers

This document covers the principles of programming using C, specifically focusing on pointers and memory management. It explains the different types of memory (stack, heap, global), the concept of pointers, their advantages, and how to declare and initialize them. Additionally, it discusses pointer arithmetic, comparison, and provides examples of using pointers in functions.

Uploaded by

vsheelavictor1
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)
13 views7 pages

Module 4 Pointers

This document covers the principles of programming using C, specifically focusing on pointers and memory management. It explains the different types of memory (stack, heap, global), the concept of pointers, their advantages, and how to declare and initialize them. Additionally, it discusses pointer arithmetic, comparison, and provides examples of using pointers in functions.

Uploaded by

vsheelavictor1
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/ 7

Principles of Programming Using C Module 4

Pointers

Memory
Program instructions and data are stored in the primary memory (RAM).
In general, the computer has three areas of memory each which is used for a
specific task. These include – stack, heap, and global memory.

Stack: Stack is the section of memory that is allocated for automatic


variables with functions. Stack follows LIFO (Last In First Out) approach of data
saving. When a program begins execution with the main() function, all variables
declared within main() are allocated space on the stack. Moreover, all the
parameters passed to a called function are stored on the stack.
Heap: It is a contiguous block of memory that is available for use by
programs when need arises. Compared to stack, heaps are slower but larger and
cheap.
Global Memory: The block of code that is in the main() program (along
with other functions I the program) is stored in the global memory. All global
variables in the program are also stored in the global memory area.

Introduction

➢ A pointer is a derived data type. This is the one which stores the address of data in memory, we will
be in position to access the data directly and do calculations over it.
➢ The standard concept is, access the data from memory using variable name it gets the data and
operations are done over them.
➢ But the pointer is different that the accessing is done by address the data is stored so that it will be
advantage of decreasing the instructions and overheads of standard usage.

Definition:

A pointer is a variable which contains the address of another variable.


Advantages of pointer

➢ Enables us to access a variable that is defined outside the function.


➢ Can be used to pass information back and forth between a function and its reference point.
➢ More efficient in handling data tables.
➢ Reduces the length and complexity of a program.
➢ Sometimes pointers also increases the execution speed.

Declaring of a pointer variable

General form:
data_type *pointer_name;
where,
Principles of Programming Using C Module 4
• The asterisk (*) tells that the variable pointer_name is a pointer variable.
• Pointer_name is a identifier.
• pointer_name needs a memory location.
• pointer_name points to a variable of type data_type which may be int, float, double etc..
Example:

where
➢ ptr is not an integer variable but ptr can hold the address of the integer variable i.e. it is a pointer to an
integer variable’ ,but the declaration of pointer variable does not make them point to any location .
➢ We can also declare the pointer variables of any other data types .
For example:
double * dptr;
char * ch;
float * fptr;

Dereference operator (*)


The unary operator (*) is the dereferencing pointer or indirection pointer , when applied to the
pointer can access the value the pointer points to.

Example:

int i= 15;
int * ptr;
ptr=&i;
printf(“Value of i is :%d”,i);
printf(“Address of i is :%d”,&i);
printf(“Value of i is :%d”,*ptr);
printf(“address of i is :%d”,ptr);
printf(“address of pointer is :%x”,&ptr);

Value of i is : 15 Address of i is : 1003


Value of i: 15
Address of i is : 1003 Address of
ptr is : 2712
Principles of Programming Using C Module 4

The null pointer


➢ Sometimes it is useful to make our pointers initialized to nothing . This is called a null pointer.
➢ A null pointer is the pointer does not point to any valid reference or memory address .We assign a
pointer a null value by setting it to address 0:
➢ Example
int *iptr;
iptr=0; or iptr = NULL;
• This statement assign address 0 to iptr . This statement describe a special preprocessor define called NULL
that evaluates to 0.
• Hence A null pointer is a pointer type which has a special value that indicate it is not pointing to any valid
reference.

Generic Pointers
A generic pointer is a pointer variable that has void as its data type. The void pointer, or the generic pointer,
is a special type of pointer that can be used to point to variables of any data type.
Eg : void *ptr;
int x = 10;
char ch = ‘a’;
ptr = &x;
printf (“\n %d”, *(int*)ptr);
ptr = &ch;
printf (“\n%c”, *(char*)ptr);

output : 10
a

Initialization of pointers
Initializing a pointer variable is a important thing, it is done as follows:
Step 1: Declare a data variable
Step 2:Declare a Pointer variable
Step 3:Assign address of data variable to pointer variable using & operator and assignment
operator.
Example:
int x;
int *p
p = &x;
Principles of Programming Using C Module 4
Pointer expressions and pointer arithmetic

Pointer arithmetic operations are different from normal arithmetic operations.


The operations allowed to on Pointers are:
1. Add or subtract integers to/from a pointer. The result is a pointer.
2. Subtract two pointers to the same type. The result is an int.
3. Comparison of two pointers

Note: Multiplication, addition, division of two pointers not allowed.


1. Add or subtract integers to/from a pointer

➢ Simple addition or subtractions operations can be performed on pointer variables.


➢ If *iPtr points to an integer, *iPtr + 1 is the address of the next integer in memory after *iPtr. *iPtr
- 1 is the address of the previous integer before *iPtr
Examples:

int a=57; int a=57;


int *iptr=&a; int *iptr=&a;
iptr= iptr +1; (or iptr++;) iptr= iptr -1; (or iptr--;)
iptr = iptr +(2B)iptr iptr = iptr -(2B)
=1003+2 iptr =1003-2
iptr =1005 iptr =1001
Note: Pointer is always incremented or decremented as per the type of value it is pointing to.

2. Subtract two pointers to the same type.


In C subtraction of two pointers are allowed.
Example:
int a=10,b=20;
int *p1,*p2; a b
10 20
p1=&a;
p2=&b; 1024 1012
int res=p1-p2;
p1 p2
1024 1012
res=1024-1012
res=12 1048 1098

3. Comparison between two Pointers :

• A pointer comparison is valid only if the two pointers are pointing to same array.
• All Relational Operators can be used for comparing pointers of same type.
• All Equality and Inequality Operators can be used with all Pointer types.
• Two Pointers addition, subtraction and division is not possible.

Pointer Comparison example:

#include<stdio.h>
void main()
{
int *ptr1,*ptr2;
ptr1 = (int *)1000;
ptr2 = (int *)2000;
if(ptr2 > ptr1)
printf("Ptr2 is far from ptr1");
}
Rules for pointer operations
• A pointer variable can be assigned the address of another variable (of the same type)
• A pointer variable can be assigned the value of another variable (of the same type)
• A pointer variable can be initialized with a null value
• Prefix or postfix increment (++) and decrement (--) operators can be applied on a pointer variable (
• An integer value can be added or subtracted from a pointer variable
• A pointer variable can be compared with another pointer variable of the same type using relational operators
(<,>, <=,>=,==,!= ….).
• A pointer variable cannot be multiplied by a constant
• A pointer variable cannot be added to another pointer variable
Write a program to add two floating point numbers. The result should contain only two digits after the
decimal
#include <stdio.h>
int main()
{
float num1, num2, sum = 0.0;
float *pnum1 = &num1, *pnum2 = &num2;
*psum = &sum;
printf (“\n enter two numbers:”);
scanf (“%f %f”, pnum1, pnum2);
*pum = *pnum1 + *pnum2;
printf (“\n%f + %f = %.2f”, *pnum1, *pnum2, *psum);
return 0;
}
Output:
enter two numbers: 2.5 3.4
2.5 + 3.4 = 5.90

Write a program to convert a floating point number into an integer


#include <stdio.h>
int main()
{
flaot fnum, *pfnum = &fnum;
int num, *pnum = &num;
printf (“\n enter the floating point no:”);
scanf (“%f”, &fnum);
*pnum = (int) *pfnum;
printf (“\n the integer equivalent of %.2f = %d”, *pfnum, *pnum);
return 0;
}
Output
enter the floating point no: 3.4
the integer equivalent of 3.40 = 3

Write a program to calculate area of a circle


#include <stdio.h>
void main()
{
double radius, area = 0.0;
double *pradius=&radius, *parea=&area;
printf (“enter the radius\n”);
scanf (“%lf”, pradius);
*parea = 3.14 * (*pradius) * (*pradius);
printf (“\narea = % .2lf”, *parea);
}
Output: enter the radius 7
Area = 153.83
Pointers and functions
➢ Pointers are often passed to a function as arguments.
➢ Allows data items within the calling program to be accessed by the function, altered, and then returned to
the calling program in altered form.(Called address or by location).
➢ When arguments are passed to a function by value.

– The data items are copied to the function.


– Changes are not reflected in the calling program.
Example:

#include<stdio.h>
void swap (int *x, int *y);
void main()
{
int a, b;
a = 5;
b = 20;
swap (&a, &b);
printf (“\n a=%d, b=%d”, a, b);

}
void swap (int *x, int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}

You might also like