0% found this document useful (0 votes)
371 views

Pointers in C Programming Study Material

This document provides an overview of pointers in C programming. Some key points: 1. A pointer is a variable that stores the memory address of another variable. Pointers allow variables to be accessed and manipulated via memory addresses rather than values. 2. Pointers can be declared using a data type followed by an asterisk, like int *ptr. They are initialized by assigning the address of a variable using the & operator, like ptr = &x. 3. Pointers enable call by reference in functions, where the function receives the memory address of an argument rather than a copy of its value. This allows the function to modify the original variable. 4. Multidimensional arrays can be represented

Uploaded by

Manjukavi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
371 views

Pointers in C Programming Study Material

This document provides an overview of pointers in C programming. Some key points: 1. A pointer is a variable that stores the memory address of another variable. Pointers allow variables to be accessed and manipulated via memory addresses rather than values. 2. Pointers can be declared using a data type followed by an asterisk, like int *ptr. They are initialized by assigning the address of a variable using the & operator, like ptr = &x. 3. Pointers enable call by reference in functions, where the function receives the memory address of an argument rather than a copy of its value. This allows the function to modify the original variable. 4. Multidimensional arrays can be represented

Uploaded by

Manjukavi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Pointers in C Programming– Study

Material

Pointers
One of the most important and powerful g) Pointers allows for references to function,
features in C language is pointer. this may facilitate passing of function as
“A pointer is a variable, it may contain the arguments to other functions.
memory address of the another variable.”
pointer can have any name that is legal for Pointer declaration:
other variable. It is declared in the same Syntax data-type * pointer name ;
manner like other variables. It is always Example:
denoted by * operator. int *a
A pointer is a variable whose value is, also char *b
an address. Each variable has two attributes; float *c
address and value. A variable can take any int *a - means „a‟ contains the address of
value specified by its data type. variable, which is integer variable.
char *b - means „b‟ contains the address of
variable, which is character variable.
float *c - means „c‟ contain the address of
variable which is float variable.
Accessing variable through pointers:
Example:
• A pointer to an integer is a variable that int *p
can store the address of that integer. x = 15 ;
• Second the value contained by a pointer p=&x
must be an address which indicates the Variable Value Address
location of another variable in the memory. x 15 4001
So pointer is called as address variable. p 4001 4005
Program to access through variable
Features of pointers: pointer:
a) Pointers are efficient in handling data and # include < stdio.h>
main ( )
associated with array.
b) Pointers are used for saving memory {
space. int a = 22, *a ;
c) Pointers reduce length and complexity of float b = 2.25 , *b ;
the program. a=&a;b=&b;
d) Pointer helps to make letter use of the printf ( “ \ n value of a = % d”, * a) ;
available memory. printf ( “ \ n value of b = % d”, * b) ;
e) Since the pointer data manipulation is }
done with address, the execution time is-
faster. Output:
f) Two-dimensional and multidimensional Value of a = 22
array representation is easy in pointers. Value of b = 2.25

Download Study Materials on www.examsdaily.in Follow us on FB for exam Updates: ExamsDaily


Pointers in C Programming– Study
Material

} the program. We can use the assignment


operator to initialize the pointer variable.
Null pointer:
A pointer is said to be a null pointer when its Example:
right value is 0. A null pointer can never p=&n
point to a valid data. For checking a pointer, where „p‟ contains the address of variable
it it is assigned to 0, then it is a null pointer „n‟.
and is not valid.
Example:
int *a ;
int *b ;
b=a=0
Hence b and a become null pointers after the
Pointers and Functions:
integer value of 0 is assigned to them.
The pointer can be used as arguments in
functions. The arguments or parameters to
Pointer to Pointer:
the function are passed in two ways.
Pointer is a variable that contains the
i) call by value
address of the another variable. Similarly
ii) call by reference
another pointer variable can store the
address of this pointer variable. So, we can
Call by value:
say, this is a pointer to pointer variable.
The process of passing the actual value of
Pointer Expressions:
variables is known as “call by value". When
Pointer variables can be used in expressions.
a function is called in program, the values to
Example:
the arguments in the function are taken by
c = *a + *b = ⇒ c = (*a) + (*b) ;
the calling program, the value taken can be
s = 10* - *a / *b ⇒ s = 10* (-(*a))/(*b) ;
used inside the function. Alteration to the
value is not accepted inside the function in
Initializing pointer variable:
calling program but change is locally
The process of assigning the address of a
available in the function.
variable to a pointer variable is known as
initialization. The location of the variable in
Call by Reference:
memory depends on system memory. This
The process of calling a function using
can be achieved in „c‟ through an ampersand
pointers to pass the addresses of variables is
(&) sign. The ampersand is an address
known as call by reference. A function can
operator, which is used to access the address
be declared with pointers as its arguments.
of a variable and assign it to a pointer to
Such function are called by calling program
initialize it. The programs with uninitialized
with the address of a variable as argument
pointers will produce errorneous results. It is
from it.
therefore important to initialize pointer
The address of the variables are altered to
variables carefully before they are used in
the pointers and any changes made to the

Download Study Materials on www.examsdaily.in Follow us on FB for exam Updates: ExamsDaily


Pointers in C Programming– Study
Material

value inside the function is automatically A multidimenstional array can also be


carried out in the location. This change is represented with an equivalent pointer
accepted by the calling program. notation. A two dimensional array can be
considered as a collection of one -
Pointers and Arrays: dimensional arrays.
Array is a collection of similar data type
elements stored under common name. When Syntax:
we declare an array the consecutive memory data-type (*pointer variable) [Expression 2].
locations are located to the array of If 'a‟ is a two dimensional integer array
elements. having 10 rows and 20 columns, „a‟ can be
The elements of an array can be efficiently declared as
accessed by using pointers. pointer ← int (*a) [20] ; rather than
Example: array ← int a[ 10][20] ;
int a[5] = {10, 20, 30, 40, 50}
a[5] means the array „a‟ has 5 elements and Array of pointers:
of integer data type. The array elements can be accessed by two
methods.
• Standard array notation
• Pointer arithmetic
Array rotation is a form of relative
addressing. The compiler treats the
subscripts as a relative offset from the
beginning of the array. When a pointer
The base address of the array start with 0th variable is set to the start of an array and is
element of the array. The array is an integer incremented to find the next element,
type, the integer will have 2 bytes and hence absolute addressing is being implemented.
the address element is incremented by 2. Therefore, using pointers have the following
Example: advantages.
int a[5] = {10, 20, 30, 40, 50} • Accessing arrays through pointers is
int * b ; marginally faster than using array notation.
b=a • Pointers give better control over array
„b‟ is a pointer variable which holds the base processing.
address the array „a‟ i.e. b*= &a[0] • Pointers allows u$ to perform certain
When pointer variable is incremented by 1 functions that are extremely difficult with
then the related base address will be array notation.
incremented by address +2 because pointer
is of type integer. Hence the next address Pointers and Strings:
element increase by 2 till the size of an i) Character and Pointer:
array. A character pointer is a pointer to the
character. It can be declared as
Pointers with Multi - dimensional array: char * pointer - character ;

Download Study Materials on www.examsdaily.in Follow us on FB for exam Updates: ExamsDaily


Pointers in C Programming– Study
Material

A character pointer can also be used to main ( )


access character arrays and string constants, {
in the same way as accessing numeric arrays void interchange (int *, int * ) ;
using their respective pointer variable. int a = 39, b = 77 ;
A character type pointer variable can be printf ( “ \ n Before Interchange: a = %d,
assigned an entire string as part of a variable b = %d , n”, a, b) ;
declarations whereas a numerical pointer interchange (& a, & b) ;
variable cannot be initialized in the same printf (“After interchange: a = %d,
manner as a numerical array. b = %d \ n”, a, b) ;
}
ii) Strings and Pointers: Void interchange (int ‟a, int *b)
A string can conveniently be represented by {
either using a one - dimensional character int t ;
array as a character pointer. t=*a;
A string constant is accessed by a pointer to *a = *b ;
its first element. *b=t;
Char pointers message ; }
The statement pointer message = “I an a Before interchange: a = 39, b = 77
student” allocates memory and stores the After interchange: a = 77, b = 39
character string and then assigns to pointer
message a pointer containing the starting Dynamic memory allocation in ‘C’:
address of the character array. Dynamic memory allocation means, a
program can obtain its memory while it is
Pointers & Structures: running. It allows us to allocate additional
Pointer pointing to a structure is known as memory space or to release unwanted space
structure pointers, at the time of program execution (runtime).
struct Pointers support the dynamic memory
{ allocations in Q language. The C language
member 1 ; provides 4 library functions known as
member 2 ; „Memory management function‟ which can
} variable, * ptr ; be used for allocating and releasing memory
Here the variable represents structure type during execution.
variable and *ptr represents the name of the Function Meaning
pointer. Malloc ( ) used to allocate
blocks of memory
Programs: in required size of
bytes.
1. Program to interchange the values
Free () used to release
stored in two variable using function with previously
pointers. allocated memory
Answer: space.
# include < stdio.h > calloc ( ) used to allocate

Download Study Materials on www.examsdaily.in Follow us on FB for exam Updates: ExamsDaily


Pointers in C Programming– Study
Material

memory space for m2 and p. The value of ml is unknown and p


an array of holds the address of m2 and indirectly the
elements. value 4 is assigned to m2.
realloc () used to modify the Program to find the sum of two numbers
size of the
using pointers
previously
allocated memory # include < stdio.h >
space # include < conio.h >
main ( )
Static Memory Allocation: {
The static memory is allocated during int a, b, c ;
compilation time. When a variable is int *a, *b ;
declared, based on their type, compiler printf ( “\ n Enter two integer value” ) ;
allocates space in memory, these variables scanf (“%.d %d”, &a, &b) ;
can be indirectly accessed with the help of *a = &a ;
pointer variable by assigning the address of *b = &b ;
the variable to the pointer variable. This way c = *a + *b ;
of assigning the pointer variable is called printf ( “ \ n sum of two integer is = %d ”, c)
static memory allocation. ;
Example: }
int m1, m2, *p = &m2 ; Output: Enter two integer value: 20 30
*p=4 Sum of two integer is = 50
When the statement is encountered the
compiler allocates space in memory for ml,

Download Study Materials on www.examsdaily.in Follow us on FB for exam Updates: ExamsDaily

You might also like