SlideShare a Scribd company logo
For more Https://www.ThesisScientist.com
Unit 8
Pointers
Introduction to Pointers
Computers use their memory for storing instructions of the programs and the values of the variables.
Memory is a sequential collection of storage cells. Each cell has an address associated with it. Whenever
we declare a variable, the system allocates, somewhere in the memory, a memory location and a unique
address is assigned to this location.
Pointer is a variable which can hold the address of a memory location rather than the value at the location.
Consider the following statement
int num = 84;
This statement instructs the system to reserve a 2-byte memory location and puts the value 84 in that
location. Assume that a system allocates memory location 1001 for num. Diagrammatically, it can be
shown as
84
num
1001
Variable name
Value
Address of memory location
84
num
1001
Variable name
Value
Address of memory location
As the memory addresses are numbers, they can be assigned to some other variable.
Let ptr be the variable which holds the address of variable num.
Thus, we can access the value of num by the variable ptr. Thus, we can say "ptr points to num".
Diagrammatically, it can be shown as
84
num
1001
1001
num
2057
84
num
1001
1001
num
2057
Pointer Notation
The actual address of a variable is not known immediately. We can determine the address of a variable
using 'address of' operator (&). We have already seen the use of 'address of' operator in the scanf( )
function.
Another pointer operator available in C is "*" called "value a address" operator. It gives the value stored at
a particular address. This operator is also known as 'indirection operator'.
e.g.: main( )
{
int i = 3;
printf ("n Address of i: = %u", & i); /* returns the address * /
printf ("t value i = %d", * (&i)); /* returns the value of address
of i */
}
Pointer Declaration and Initialization
Since pointer variables contain address that belong to a separate data type, they must be declared as pointers
before we use them.
The declaration of a pointer variable takes the following form:
data_type *pt_name
This tells the compiler three things about the variable pt_name.
1. The asterisk (*) tells that the variable pt_name is a pointer variable.
2. pt_name needs a memory location.
3. pt_name points to a variable of type data type.
e.g.: int *p; declares the variable p as a pointer variable that points to an integer data type. The
type int refers to the data type of the variable being pointed to by p and not the type of the value of
the pointer.
Once a pointer variable has been declared, it can be made to point to a variable using an assignment
statement such as p = &quantity; which causes p to point to quantity. That is, p now contains the address
of quantity. This is known as pointer initialization. Before a pointer is initialized, it should not be used. A
pointer variable can be initialized in its declaration itself.
Pointer Expressions
Like other variables, pointer variables can be used in expressions. Arithmetic and comparison operations
can be performed on the pointers.
e.g.: If p1 and p2 are properly declared and initialized pointers, then following
statements are valid.
y = * p1 * *p2;
sum = sum + * p1;
Pointer Arithmetic
C allows us to add integers from pointers as well as subtract one pointer from another. Shorthand operators
like sum + = *p2; can also be used with the pointers for arithmetic expressions. Following operations can
be performed on a pointer:
a. addition of a number to a pointer
e.g.: int i = 4, *J, *k;
J = &i;
J = J+1;
J = J+9;
k = J+3;
b. subtraction of a number from a pointer
e.g.: int i = 4, *J, *k;
J = &i;
J = J-2;
J = J-5;
k = J-6;
c. subtraction of one pointer from another provided that both variables point to elements of the same
array. The resulting value indicates the number of bytes separating corresponding array elements.
But the following operations are not allowed on pointers.
a. multiplication of pointer with a constant
b. addition of two pointers
c. division of pointer by a constant
Pointers and One Dimensional Arrays
When an array is declared, the compiler allocates a base address and sufficient amount of storage to contain
all the elements of the array in contiguous memory locations. The base address is the location of the first
element (index 0) of the array. The compiler also defines the array name as a constant pointer to the first
element.
The array declared as
static int x[5] = {1, 2, 3, 4, 5};
is stored as follows:
Elements x[0] x[1] x[2] x[3] x[4]
Value 1 2 3 4 5
Address 1000 1002 1004 1006 1008
The name x is defined as a constant pointer pointing to the first element, x[0] and therefore the value of x is
1000, the location where x[0] is stored. That is,
x = &x[0] = 1000
Pointers and Multi-dimensional Arrays
A two dimensional array can be defined as a pointer to a group of contiguous one dimensional arrays. A
two dimensional array declaration can be written as:
data_type (*ptvar)[expression2];
This can be generalized to higher dimensional arrays, that is,
data_type (*ptvar) [expression2] [expression3] ... [expression n];
In these declarations data_type refers to the data type of the array, ptvar is the name of the pointer variable,
array is the corresponding array name, and expression1, expression 2 ------ expression n are positive valued
integer expressions that indicate the maximum number of array elements associated with each subscript.
e.g.: 1. Suppose that x is a two dimensional integer array having 10 rows and 20 columns. We
can declare x as int (*x) [20]; rather than int x[10] [20];
In the first declaration, x is defined to be a pointer to a group of contiguous, one
dimensional, 20-element integer arrays. Thus, x points to the first 20-elements array, which
is actually the first row (row 0) of the original two dimensional array. Similarly, (x + 1)
points to the second 20-elements array, which is the second row (row 1) of the original two
dimensional array, and so on, as illustrated below.
First One Dimensional Array
x
Second One Dimensional Array
(x +1)
Nth One Dimensional Array
(x + 9)
Arrays of Pointers
A multi-dimensional array can be expressed in terms of an array of pointers rather than as a pointer to a
group of contiguous arrays. In such situations the newly defined array will have one less dimension than the
original multi-dimensional array. Each pointer will indicate the beginning of a separate (n - 1) dimensional
array.
In general terms, a two dimensional array can be defined as one dimensional array of pointers by writing
data_type *array[expression1];
Similarly, a n dimensional array can be defined as a (n-1) dimensional array of pointers by writing
data_type *array[expression1][expression2]...[expressionN-1];
In these declarations data_type refers to the data type of the original n dimensional array, array is the array
name, and expression1, expression2, . . ., expression n are positive-valued integer expressions that indicate
the maximum number of elements associated with each subscript.
Pointer to Pointers
Pointer is a variable, which contains address of a variable. This variable itself could be another pointer.
Thus, a pointer contains another pointer's address as shown in the example given below:
main()
{
int i = 3; int *j, **k;
j = &i; k = &j;
printf ("Address of i = %dn", &i);
printf ("Address of i = %dn", j);
printf ("Address of i = %dn", *k);
printf ("Address of j = %dn", &j);
printf ("Address of j = %dn", *k);
printf ("Address of k = %dnn", &k);
printf ("Value of j = %dn", j);
printf ("Value of k = %dn", k);
printf ("Value of i = %dn", i);
printf ("Value of i = %dn", *(&i));
printf ("Value of i = %dn", *j);
printf ("Value of i = %dn", **k);
}
The following figure would help you in tracing out how a program prints the output.
Output: Address of i = 6485
Address of i = 6485
Address of i = 6485
Address of j = 3276
Address of j = 3276
Address of k = 7234
Value of j = 6485
Value of k = 3276
Value of i = 3
i
3
6485
i
6485
3276
k
3276
7234
Value of i = 3
Value of i = 3
Value of i = 3
Pointers and Functions
When an array is passed to a function as an argument, only the address of the first element of the array is
passed, but not the actual values of the array elements. If x is an array, when we call sort (x), the address of
x[0] is passed to the function sort. The function uses this address for manipulating the array elements. The
address of a variable can be passed as an argument to a function in the normal fashion. When address is
passed to a function, the parameters receiving the address should be pointers. The process of calling a
function using pointers to pass the address of variables is known as call by reference. The function which is
called “by reference” can change the value of the variable used in the call. Consider the following example:
e.g.: main ()
{
int x;
x = 20;
change (&x);
printf ("%d n", x);
}
change (p)
int *p;
{
*p = *p + 10;
}
When the function change ( ) is called, the address of the variable x, not its value, is passed into the
function change ( ). Inside change ( ), the variable p is declared as a pointer and therefore p is the address of
the variable x.
Pointers to Functions
A pointer to a function is declared as follows:
type (*fptr) ( );
This tells the compiler that fptr is a pointer to a function which returns type value.
A statement like type *fptr ( ); would declare fptr as a function returning a pointer to type.
A function pointer can be made to point to a specific function by simply assigning the name of the function
to the pointer.
The statements,
double (* p1) ( ), mul ( );
p1 = mul;
declare p1 as a pointer to a function and mul as a function and then make p1 to point to the function mul. To
call the function mul, we may now use the pointer p1 with the list of parameters. That is, (*p1) (x, y) is
equivalent to mul (x, y).
Using 'call by reference' intelligently we can make a function return more than one value at a time, which is
not possible ordinarily.
main ( )
{
int radius;
float area, perimeter;
printf ("nEnter radius of a circle:");
scanf ("%d", &radius);
areaperi (radius, &area, &perimeter);
printf ("nArea = %f", area);
printf("nPerimeter = %f", perimeter);
}
areaperi (int r, float *a, float *p)
{
* a = 3.14 *r * r;
* p = 2 * 3.14 * r;
}
In this program there is a mixed call as the value of radius is being passed, but for area and perimeter,
addresses are passed.
Since the addresses are passed, any change made in values stored at addresses contained in the variables a
and p would make the change effective even in main ( ).
Thus the limitation of the return statement, which can return only one value from a function at a time has
been overcome, and it is now possible to return two values from the called function areaperi ( ), as in the
example program.
Functions with a Variable Number of Arguments
In the file stdarg.h there are three macros available called va_start, va_arg and va_list. These macros
provide a method for accessing the arguments of the function when a function takes a fixed number of
arguments followed by a variable number of arguments.
The fixed number of arguments are accessed in the normal way, whereas the optional arguments are
accessed using the macros va_start and va_arg.
va_start is used to initialize a pointer to the beginning of the list of optional arguments.
va_arg is used to advance the pointer to the next argument.
The program given below calls a function display ( ) which prints any number of arguments of any type.
#include <stdarg.h>
main( )
{
printf ("n");
display (1, 2, 5, 6);
printf ("n");
display (2, 4, 'A', 'a', 'b', 'c');
printf ("n");
display (3, 3, 2.5, 299.3, -1.0);
}
display (type, num)
int type, num;
{
int i, j;
char c;
float f;
va_list ptr;
va_start (ptr, num);
switch (type)
{
Case 1:
for (j = 1; j <= num; j++)
{
i = va_arg (ptr, int);
printf ("%d", i);
}
break;
Case 2:
for (j = 1; j <= num; j++)
{
c = va_arg (ptr, char);
printf ("%c", c);
}
break;
Case 3:
for (j = 1; j <= num; j++)
{
f = (float) va_arg (ptr, double);
printf ("%f", f);
}
}
}

More Related Content

What's hot (20)

PPTX
Pointer in C++
Mauryasuraj98
 
PPTX
C Programming Unit-4
Vikram Nandini
 
PPTX
Pointers in c
CHANDAN KUMAR
 
PDF
Pointer
Learn By Watch
 
PDF
10. array & pointer
웅식 전
 
PDF
C Pointers
omukhtar
 
PDF
Pointers_c
ahmed safwat
 
PPTX
Lecture 9
Mohammed Khan
 
PDF
Module 02 Pointers in C
Tushar B Kute
 
PPTX
Pointers in c++
sai tarlekar
 
PPT
Pointers in C
Prabhu Govind
 
PPTX
Function Pointer
Dr-Dipali Meher
 
PPT
Arrays
Saranya saran
 
PPTX
Arrays in c
CHANDAN KUMAR
 
PPTX
Very interesting C programming Technical Questions
Vanathi24
 
PPT
Ch6 pointers (latest)
Hattori Sidek
 
PPT
Pointers - DataStructures
Omair Imtiaz Ansari
 
PPT
Ch7 structures
Hattori Sidek
 
Pointer in C++
Mauryasuraj98
 
C Programming Unit-4
Vikram Nandini
 
Pointers in c
CHANDAN KUMAR
 
10. array & pointer
웅식 전
 
C Pointers
omukhtar
 
Pointers_c
ahmed safwat
 
Lecture 9
Mohammed Khan
 
Module 02 Pointers in C
Tushar B Kute
 
Pointers in c++
sai tarlekar
 
Pointers in C
Prabhu Govind
 
Function Pointer
Dr-Dipali Meher
 
Arrays in c
CHANDAN KUMAR
 
Very interesting C programming Technical Questions
Vanathi24
 
Ch6 pointers (latest)
Hattori Sidek
 
Pointers - DataStructures
Omair Imtiaz Ansari
 
Ch7 structures
Hattori Sidek
 

Similar to C pointers and references (20)

PPT
Pointers on data structure in computer science.ppt
MdSabbirAhmedEkhon
 
PPTX
Algoritmos e Estruturas de Dados - Pointers
martijnkuipersandebo
 
PPT
Pointers C programming
Appili Vamsi Krishna
 
PPT
Pointer in C
Sonya Akter Rupa
 
PPTX
Pointers
Munazza-Mah-Jabeen
 
PPSX
Pointers
Frijo Francis
 
PPT
l7-pointers.ppt
ShivamChaturvedi67
 
PPTX
PPS-POINTERS.pptx
sajinis3
 
PPTX
Pointers
Joy Forerver
 
PPT
detailed information about Pointers in c language
gourav kottawar
 
PPT
Chapter09-10 Pointers and operations .PPT
ShalabhMishra10
 
PPT
Chapter09-10.PPT
shubhamshukla9769280
 
PPT
Pointers definition syntax structure use
dheeraj658032
 
PPTX
4 Pointers.pptx
aarockiaabinsAPIICSE
 
PPTX
unit-7 Pointerdesfsdfsdgsdgaa notes.pptx
TriggeredZulkar
 
PDF
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
malavshah9013
 
DOCX
PPS 9.9.POINTERS IDEA OF POINTERS, DEFINING POINTERS, USE OF POINTERS IN SEL...
Sitamarhi Institute of Technology
 
PDF
Chapter 13.1.8
patcha535
 
PPT
Pointers
Lp Singh
 
PDF
Embedded C The IoT Academy
The IOT Academy
 
Pointers on data structure in computer science.ppt
MdSabbirAhmedEkhon
 
Algoritmos e Estruturas de Dados - Pointers
martijnkuipersandebo
 
Pointers C programming
Appili Vamsi Krishna
 
Pointer in C
Sonya Akter Rupa
 
Pointers
Frijo Francis
 
l7-pointers.ppt
ShivamChaturvedi67
 
PPS-POINTERS.pptx
sajinis3
 
Pointers
Joy Forerver
 
detailed information about Pointers in c language
gourav kottawar
 
Chapter09-10 Pointers and operations .PPT
ShalabhMishra10
 
Chapter09-10.PPT
shubhamshukla9769280
 
Pointers definition syntax structure use
dheeraj658032
 
4 Pointers.pptx
aarockiaabinsAPIICSE
 
unit-7 Pointerdesfsdfsdgsdgaa notes.pptx
TriggeredZulkar
 
Declaring a PointerTo define a pointer, use an asterisk, (), in t.pdf
malavshah9013
 
PPS 9.9.POINTERS IDEA OF POINTERS, DEFINING POINTERS, USE OF POINTERS IN SEL...
Sitamarhi Institute of Technology
 
Chapter 13.1.8
patcha535
 
Pointers
Lp Singh
 
Embedded C The IoT Academy
The IOT Academy
 
Ad

More from Thesis Scientist Private Limited (20)

PDF
HTML guide for beginners
Thesis Scientist Private Limited
 
PDF
Ransomware attacks 2017
Thesis Scientist Private Limited
 
PDF
How to write a Great Research Paper?
Thesis Scientist Private Limited
 
PDF
Research Process design
Thesis Scientist Private Limited
 
PDF
How to write a good Dissertation/ Thesis
Thesis Scientist Private Limited
 
PDF
How to write a Research Paper
Thesis Scientist Private Limited
 
PDF
Internet security tips for Businesses
Thesis Scientist Private Limited
 
PDF
How to deal with a Compulsive liar
Thesis Scientist Private Limited
 
PDF
Driverless car Google
Thesis Scientist Private Limited
 
PDF
Podcast tips beginners
Thesis Scientist Private Limited
 
PDF
Vastu for Career Success
Thesis Scientist Private Limited
 
PDF
Reliance jio broadband
Thesis Scientist Private Limited
 
PDF
Job Satisfaction definition
Thesis Scientist Private Limited
 
PDF
Mistakes in Advertising
Thesis Scientist Private Limited
 
PDF
Contributor in a sentence
Thesis Scientist Private Limited
 
PDF
Different Routing protocols
Thesis Scientist Private Limited
 
PDF
Ad hoc network routing protocols
Thesis Scientist Private Limited
 
PDF
Latest Thesis Topics for Fog computing
Thesis Scientist Private Limited
 
PDF
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Thesis Scientist Private Limited
 
HTML guide for beginners
Thesis Scientist Private Limited
 
Ransomware attacks 2017
Thesis Scientist Private Limited
 
How to write a Great Research Paper?
Thesis Scientist Private Limited
 
Research Process design
Thesis Scientist Private Limited
 
How to write a good Dissertation/ Thesis
Thesis Scientist Private Limited
 
How to write a Research Paper
Thesis Scientist Private Limited
 
Internet security tips for Businesses
Thesis Scientist Private Limited
 
How to deal with a Compulsive liar
Thesis Scientist Private Limited
 
Driverless car Google
Thesis Scientist Private Limited
 
Podcast tips beginners
Thesis Scientist Private Limited
 
Vastu for Career Success
Thesis Scientist Private Limited
 
Reliance jio broadband
Thesis Scientist Private Limited
 
Job Satisfaction definition
Thesis Scientist Private Limited
 
Mistakes in Advertising
Thesis Scientist Private Limited
 
Contributor in a sentence
Thesis Scientist Private Limited
 
Different Routing protocols
Thesis Scientist Private Limited
 
Ad hoc network routing protocols
Thesis Scientist Private Limited
 
Latest Thesis Topics for Fog computing
Thesis Scientist Private Limited
 
Latest Research Topics On Flying Ad-Hoc Networks (FANETs):
Thesis Scientist Private Limited
 
Ad

Recently uploaded (20)

PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
Big Data and Data Science hype .pptx
SUNEEL37
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
Zilliz Cloud Demo for performance and scale
Zilliz
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Big Data and Data Science hype .pptx
SUNEEL37
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
Zilliz Cloud Demo for performance and scale
Zilliz
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 

C pointers and references

  • 1. For more Https://www.ThesisScientist.com Unit 8 Pointers Introduction to Pointers Computers use their memory for storing instructions of the programs and the values of the variables. Memory is a sequential collection of storage cells. Each cell has an address associated with it. Whenever we declare a variable, the system allocates, somewhere in the memory, a memory location and a unique address is assigned to this location. Pointer is a variable which can hold the address of a memory location rather than the value at the location. Consider the following statement int num = 84; This statement instructs the system to reserve a 2-byte memory location and puts the value 84 in that location. Assume that a system allocates memory location 1001 for num. Diagrammatically, it can be shown as 84 num 1001 Variable name Value Address of memory location 84 num 1001 Variable name Value Address of memory location As the memory addresses are numbers, they can be assigned to some other variable. Let ptr be the variable which holds the address of variable num. Thus, we can access the value of num by the variable ptr. Thus, we can say "ptr points to num". Diagrammatically, it can be shown as 84 num 1001 1001 num 2057 84 num 1001 1001 num 2057 Pointer Notation The actual address of a variable is not known immediately. We can determine the address of a variable using 'address of' operator (&). We have already seen the use of 'address of' operator in the scanf( ) function.
  • 2. Another pointer operator available in C is "*" called "value a address" operator. It gives the value stored at a particular address. This operator is also known as 'indirection operator'. e.g.: main( ) { int i = 3; printf ("n Address of i: = %u", & i); /* returns the address * / printf ("t value i = %d", * (&i)); /* returns the value of address of i */ } Pointer Declaration and Initialization Since pointer variables contain address that belong to a separate data type, they must be declared as pointers before we use them. The declaration of a pointer variable takes the following form: data_type *pt_name This tells the compiler three things about the variable pt_name. 1. The asterisk (*) tells that the variable pt_name is a pointer variable. 2. pt_name needs a memory location. 3. pt_name points to a variable of type data type. e.g.: int *p; declares the variable p as a pointer variable that points to an integer data type. The type int refers to the data type of the variable being pointed to by p and not the type of the value of the pointer. Once a pointer variable has been declared, it can be made to point to a variable using an assignment statement such as p = &quantity; which causes p to point to quantity. That is, p now contains the address of quantity. This is known as pointer initialization. Before a pointer is initialized, it should not be used. A pointer variable can be initialized in its declaration itself. Pointer Expressions Like other variables, pointer variables can be used in expressions. Arithmetic and comparison operations can be performed on the pointers. e.g.: If p1 and p2 are properly declared and initialized pointers, then following statements are valid. y = * p1 * *p2; sum = sum + * p1; Pointer Arithmetic C allows us to add integers from pointers as well as subtract one pointer from another. Shorthand operators like sum + = *p2; can also be used with the pointers for arithmetic expressions. Following operations can be performed on a pointer: a. addition of a number to a pointer
  • 3. e.g.: int i = 4, *J, *k; J = &i; J = J+1; J = J+9; k = J+3; b. subtraction of a number from a pointer e.g.: int i = 4, *J, *k; J = &i; J = J-2; J = J-5; k = J-6; c. subtraction of one pointer from another provided that both variables point to elements of the same array. The resulting value indicates the number of bytes separating corresponding array elements. But the following operations are not allowed on pointers. a. multiplication of pointer with a constant b. addition of two pointers c. division of pointer by a constant Pointers and One Dimensional Arrays When an array is declared, the compiler allocates a base address and sufficient amount of storage to contain all the elements of the array in contiguous memory locations. The base address is the location of the first element (index 0) of the array. The compiler also defines the array name as a constant pointer to the first element. The array declared as static int x[5] = {1, 2, 3, 4, 5}; is stored as follows: Elements x[0] x[1] x[2] x[3] x[4] Value 1 2 3 4 5 Address 1000 1002 1004 1006 1008 The name x is defined as a constant pointer pointing to the first element, x[0] and therefore the value of x is 1000, the location where x[0] is stored. That is, x = &x[0] = 1000 Pointers and Multi-dimensional Arrays A two dimensional array can be defined as a pointer to a group of contiguous one dimensional arrays. A two dimensional array declaration can be written as: data_type (*ptvar)[expression2];
  • 4. This can be generalized to higher dimensional arrays, that is, data_type (*ptvar) [expression2] [expression3] ... [expression n]; In these declarations data_type refers to the data type of the array, ptvar is the name of the pointer variable, array is the corresponding array name, and expression1, expression 2 ------ expression n are positive valued integer expressions that indicate the maximum number of array elements associated with each subscript. e.g.: 1. Suppose that x is a two dimensional integer array having 10 rows and 20 columns. We can declare x as int (*x) [20]; rather than int x[10] [20]; In the first declaration, x is defined to be a pointer to a group of contiguous, one dimensional, 20-element integer arrays. Thus, x points to the first 20-elements array, which is actually the first row (row 0) of the original two dimensional array. Similarly, (x + 1) points to the second 20-elements array, which is the second row (row 1) of the original two dimensional array, and so on, as illustrated below. First One Dimensional Array x Second One Dimensional Array (x +1) Nth One Dimensional Array (x + 9) Arrays of Pointers A multi-dimensional array can be expressed in terms of an array of pointers rather than as a pointer to a group of contiguous arrays. In such situations the newly defined array will have one less dimension than the original multi-dimensional array. Each pointer will indicate the beginning of a separate (n - 1) dimensional array. In general terms, a two dimensional array can be defined as one dimensional array of pointers by writing data_type *array[expression1]; Similarly, a n dimensional array can be defined as a (n-1) dimensional array of pointers by writing data_type *array[expression1][expression2]...[expressionN-1]; In these declarations data_type refers to the data type of the original n dimensional array, array is the array name, and expression1, expression2, . . ., expression n are positive-valued integer expressions that indicate the maximum number of elements associated with each subscript.
  • 5. Pointer to Pointers Pointer is a variable, which contains address of a variable. This variable itself could be another pointer. Thus, a pointer contains another pointer's address as shown in the example given below: main() { int i = 3; int *j, **k; j = &i; k = &j; printf ("Address of i = %dn", &i); printf ("Address of i = %dn", j); printf ("Address of i = %dn", *k); printf ("Address of j = %dn", &j); printf ("Address of j = %dn", *k); printf ("Address of k = %dnn", &k); printf ("Value of j = %dn", j); printf ("Value of k = %dn", k); printf ("Value of i = %dn", i); printf ("Value of i = %dn", *(&i)); printf ("Value of i = %dn", *j); printf ("Value of i = %dn", **k); } The following figure would help you in tracing out how a program prints the output. Output: Address of i = 6485 Address of i = 6485 Address of i = 6485 Address of j = 3276 Address of j = 3276 Address of k = 7234 Value of j = 6485 Value of k = 3276 Value of i = 3 i 3 6485 i 6485 3276 k 3276 7234
  • 6. Value of i = 3 Value of i = 3 Value of i = 3 Pointers and Functions When an array is passed to a function as an argument, only the address of the first element of the array is passed, but not the actual values of the array elements. If x is an array, when we call sort (x), the address of x[0] is passed to the function sort. The function uses this address for manipulating the array elements. The address of a variable can be passed as an argument to a function in the normal fashion. When address is passed to a function, the parameters receiving the address should be pointers. The process of calling a function using pointers to pass the address of variables is known as call by reference. The function which is called “by reference” can change the value of the variable used in the call. Consider the following example: e.g.: main () { int x; x = 20; change (&x); printf ("%d n", x); } change (p) int *p; { *p = *p + 10; } When the function change ( ) is called, the address of the variable x, not its value, is passed into the function change ( ). Inside change ( ), the variable p is declared as a pointer and therefore p is the address of the variable x. Pointers to Functions A pointer to a function is declared as follows: type (*fptr) ( ); This tells the compiler that fptr is a pointer to a function which returns type value. A statement like type *fptr ( ); would declare fptr as a function returning a pointer to type. A function pointer can be made to point to a specific function by simply assigning the name of the function to the pointer. The statements, double (* p1) ( ), mul ( ); p1 = mul;
  • 7. declare p1 as a pointer to a function and mul as a function and then make p1 to point to the function mul. To call the function mul, we may now use the pointer p1 with the list of parameters. That is, (*p1) (x, y) is equivalent to mul (x, y). Using 'call by reference' intelligently we can make a function return more than one value at a time, which is not possible ordinarily. main ( ) { int radius; float area, perimeter; printf ("nEnter radius of a circle:"); scanf ("%d", &radius); areaperi (radius, &area, &perimeter); printf ("nArea = %f", area); printf("nPerimeter = %f", perimeter); } areaperi (int r, float *a, float *p) { * a = 3.14 *r * r; * p = 2 * 3.14 * r; } In this program there is a mixed call as the value of radius is being passed, but for area and perimeter, addresses are passed. Since the addresses are passed, any change made in values stored at addresses contained in the variables a and p would make the change effective even in main ( ). Thus the limitation of the return statement, which can return only one value from a function at a time has been overcome, and it is now possible to return two values from the called function areaperi ( ), as in the example program. Functions with a Variable Number of Arguments In the file stdarg.h there are three macros available called va_start, va_arg and va_list. These macros provide a method for accessing the arguments of the function when a function takes a fixed number of arguments followed by a variable number of arguments. The fixed number of arguments are accessed in the normal way, whereas the optional arguments are accessed using the macros va_start and va_arg. va_start is used to initialize a pointer to the beginning of the list of optional arguments. va_arg is used to advance the pointer to the next argument. The program given below calls a function display ( ) which prints any number of arguments of any type. #include <stdarg.h> main( )
  • 8. { printf ("n"); display (1, 2, 5, 6); printf ("n"); display (2, 4, 'A', 'a', 'b', 'c'); printf ("n"); display (3, 3, 2.5, 299.3, -1.0); } display (type, num) int type, num; { int i, j; char c; float f; va_list ptr; va_start (ptr, num); switch (type) { Case 1: for (j = 1; j <= num; j++) { i = va_arg (ptr, int); printf ("%d", i); } break; Case 2: for (j = 1; j <= num; j++) { c = va_arg (ptr, char); printf ("%c", c); } break; Case 3: for (j = 1; j <= num; j++) { f = (float) va_arg (ptr, double); printf ("%f", f); } } }