This document discusses pointers in C programming. It begins with an introduction to pointers and memory organization. It then covers basics of pointers including declaration, initialization, dereferencing, and pointer expressions. The document discusses various pointer applications including pointer arithmetic, pointers to pointers, dynamic memory allocation, pointers and arrays, and character strings. It provides examples to illustrate concepts like pointer expressions and pointer arithmetic with arrays. The document is intended as a reference for understanding pointers in C.
Introduction to pointers in C, chapter overview, contents including memory organization, applications, types of pointers, memory allocation functions.
Basics needed for pointer concepts including data types, arrays, and standard I/O functions.
Definition and functionality of pointers, benefits such as memory efficiency, handling dynamic data structures, and their role in function arguments.
Understanding how pointers store addresses, addressing concepts with examples, including declaration and initialization. Syntax for declaring pointers, initialization, and understanding address-of (&) and de-reference (*) operators.
Examples illustrating usage of single and double pointers, and the concept of pointer-to-pointer.
Rules and examples of pointer arithmetic including operations on pointer variables and relations with arrays.
Functions for dynamic memory allocation like malloc, calloc, realloc, and the concept of generic pointers.
Differences between various pointers like null, wild, near, far, and huge pointers, along with related examples.
Recap of pointers in C including storage of memory addresses, operators used, pointer arithmetic, and their association with arrays.
Contents
6.1 Introduction
6.2 MemoryOrganization
6.3 Basics of Pointer
6.4 Application of Pointer
6.5 Pointer Expressions
Declaration of Pointer, Initializing
Pointer,
De-referencing Pointer
Void Pointer
Pointer Arithmetic
6.6 Precedence of &, * operators
6.7 Pointer to Pointer
6.8 Constant Pointe
2
6.9 Dynamic Memory
Allocation
6.10 sizeof(), malloc(), calloc(),
realloc(), free()
6.11 Pointers and Arrays
6.12 Pointers and character
string
6.13 Array of pointers
3.
3
Pre-requisite
Basics ofthe C programming language
Data type
Variable
Array
Function call
Standard Input/Output
e.g. printf(), scanf()
4.
Pointers - Introduction
Pointer is a derived type.
Pointers contain memory address as
their values.
Pointers can be used to access and
manipulate data stored in memory.
4
5.
5
Computer Memory Revisited
Computers store data in memory slots
Each slot has an unique address
Variables store their values like this:
Addr Content Addr Content Addr Content Addr Content
1000 i: 37 1001 j: 46 1002 k: 58 1003 m: 74
1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘c’ 1007 a[3]: ‘0’
1008 ptr: 1001 1009 … 1010 1011
6.
6
Computer Memory Revisited
Altering the value of a variable is indeed
changing the content of the memory
e.g. i = 40; a[2] = ‘z’;
Addr Content Addr Content Addr Content Addr Content
1000 i: 40 1001 j: 46 1002 k: 58 1003 m: 74
1004 a[0]: ‘a’ 1005 a[1]: ‘b’ 1006 a[2]: ‘z’ 1007 a[3]: ‘0’
1008 ptr: 1001 1009 … 1010 1011
7.
7
Addressing Concept
Pointerstores the address of another
entity
It refers to a memory location
int i = 5;
int *ptr; /* declare a pointer variable */
ptr = &i; /* store address-of i to ptr */
printf(“*ptr = %dn”, *ptr); /* refer to referee of ptr */
8.
8
Why do weneed Pointer?
Simply because it’s there!
It is used in some circumstances in C
Remember this?
scanf(“%d”, &i);
9.
Benefits of pointers
More efficient in handling arrays and data
tables.
Pointers can be used to return multiple values
from a function via function arguments.
Pointers permit references to functions and
there by facilitating passing of functions as
arguments to other functions.
The use of pointer arrays to character strings
result in saving of data storage space in
memory. 9
10.
Benefits of Pointer
Pointers allow C to support DMA.
Pointers provides an efficient tool for
manipulating dynamic data structures such as
structures, linked list, trees, etc.
Pointers reduce length and complexity
program.
They increase execution speed and thus
reduce the program execution time.
10
11.
11
What actually ptris?
ptr is a variable storing an address
ptr is NOT storing the actual value of i
int i = 5;
int *ptr;
ptr = &i;
printf(“i = %dn”, i);
printf(“*ptr = %dn”, *ptr);
printf(“ptr = %pn”, ptr);
5i
address of iptr
Output:
i = 5
*ptr = 5
ptr = effff5e0
value of ptr =
address of i
in memory
12.
12
Twin Operators
&:Address-of operator
Get the address of an entity
e.g. ptr = &j;
Addr Content Addr Content Addr Content Addr Content
1000 i: 40 1001 j: 33 1002 k: 58 1003 m: 74
1004 ptr: 1001 1005 1006 1007
13.
13
Twin Operators
*:De-reference operator
Refer to the content of the referee
e.g. *ptr = 99;
Addr Content Addr Content Addr Content Addr Content
1000 i: 40 1001 j: 99 1002 k: 58 1003 m: 74
1004 ptr: 1001 1005 1006 1007
14.
example
Write aprogram to print address of
variable along with value.
14
So,
Pointer isa variable just like other
variables of c but only difference is
unlike the other variable it stores the
memory address of any other variables
of c. This variable may be type
of int, char, array, structure, function or
any other pointers.
Examples
17
18.
18
(1)
Pointer pwhich is storing memory address of a int type variable:
int i=50;
int *p=&i;
(2)
Pointer p which is storing memory address of an array:
int arr[20];
int (*p)[20]=&arr;
(3)
Pointer p which is storing memory address of a function:
char display(void);
char(*p)(void)=&display;
19.
19
(4)
Pointer p whichis storing memory address of struct type
variable:
struct abc
{
int a;
float b;
}var;
struct abc *p=&var;
20.
20
Example: Pass byReference
Modify behaviour in argument passing
void f(int j)
{
j = 5;
}
void g()
{
int i = 3;
f(i);
}
void f(int *ptr)
{
*ptr = 5;
}
void g()
{
int i = 3;
f(&i);
} i = ?i = ?i = 3 i = 5
21.
21
An Illustration
int i= 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
22.
22
An Illustration
int i= 5, j = 10;
int *ptr; /* declare a pointer-to-integer variable */
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
ptr int * integer pointer variable
23.
23
An Illustration
int i= 5, j = 10;
int *ptr;
int **pptr; /* declare a pointer-to-pointer-to-integer variable */
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
ptr int * integer pointer variable
pptr int ** integer pointer pointer variable
Double Indirection
24.
24
An Illustration
int i= 5, j = 10;
int *ptr;
int **pptr;
ptr = &i; /* store address-of i to ptr */
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable
*ptr int de-reference of ptr 5
25.
25
An Illustration
int i= 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr; /* store address-of ptr to pptr */
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
*pptr int * de-reference of pptr value of ptr
(address of i)
26.
26
An Illustration
int i= 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 3
j int integer variable 10
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr 3
27.
27
An Illustration
int i= 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 7
j int integer variable 10
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
**pptr int de-reference of de-reference of
pptr
7
28.
28
An Illustration
int i= 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 7
j int integer variable 10
ptr int * integer pointer variable address of j
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr 10
29.
29
An Illustration
int i= 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 7
j int integer variable 9
ptr int * integer pointer variable address of j
pptr int ** integer pointer pointer variable address of ptr
**pptr int de-reference of de-reference of
pptr
9
30.
30
An Illustration
int i= 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 7
j int integer variable 9
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
*pptr int * de-reference of pptr value of ptr
(address of i)
31.
31
An Illustration
int i= 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable -2
j int integer variable 9
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr -2
32.
Chain of pointers
32
Address
2
Address1 value
P2 p1 variable
The pointer variable p2 contains the address of the pointer
variable p1 which points to the location that contains the
desired value. This is known as multiple indirection.
•Example
Rules of pointervariable
A pointer variable can be assigned the address of another
varaible.
A pointer variable can be assigned the values of another
pointer variable.
A pointer variable can be initialized with NULL or zero
value.
A pointer variable can be pre-fixed or post-fixed with
increment or decrement operators.
An integer value may be added or subtracted from a
pointer variable.
When two pointers point to the same array, one pointer
variable can be subtracted from other.
When two pointers point to the same objects of the same
data types, they can compared using relational operators.
Two pointer variable can not be added
A value can not be assigned to an arbitrary address. i.e x=
&10; 36
49
More Pointer Arithmetic
What if a is a double array?
A double may occupy more memory slots!
Given double *ptr = a;
What’s ptr + 1 then?
Addr Content Addr Content Addr Content Addr Content
1000 a[0]: 37.9 1001 … 1002 … 1003 …
1004 a[1]: 1.23 1005 … 1006 … 1007 …
1008 a[2]: 3.14 1009 … 1010 … 1011 …
50.
50
More Pointer Arithmetic
Arithmetic operators + and – auto-adjust
the address offset
According to the type of the pointer:
1000 + sizeof(double) = 1000 + 4 = 1004
Addr Content Addr Content Addr Content Addr Content
1000 a[0]: 37.9 1001 … 1002 … 1003 …
1004 a[1]: 1.23 1005 … 1006 … 1007 …
1008 a[2]: 3.14 1009 … 1010 … 1011 …
51.
Array of string
Pointer to array of string: A pointer which pointing to an
array which content is string, is known as pointer to array of
strings.
#include<stdio.h>
int main()
{
char *array[4]={"c","c++","java","sql"};
char *(*ptr)[4]=&array;
printf("%s ",++(*ptr)[2]);
return 0;
}
51
52.
Array of string
Pointer to array of string: A pointer which pointing to an
array which content is string, is known as pointer to array of
strings.
#include<stdio.h>
int main()
{
char *array[4]={"c","c++","java","sql"};
char *(*ptr)[4]=&array;
printf("%s ",++(*ptr)[2]);
return 0;
}
52
Output: ava
As we knowp[i]=*(p+i)
(***ptr[0])[2]=(*(***ptr+0))[2]=(***ptr)[2]
=(***(&array))[2] //ptr=&array
=(**array)[2] //From rule *&p=p
=(**(&p1))[2] //array=&p1
=(*p1)[2]
=(*&s)[2] //p1=&s
=s[2]=”che” 56
57.
Example
int main()
{ int i,j,temp1,temp2;
int arr[8]={5,3,0,2,12,1,33,2};
int *ptr;
for(i=0;i<7;i++){
for(j=0;j<7-i;j++){
if(*(arr+j)>*(arr+j+1)){
ptr=arr+j;
temp1=*ptr++;
temp2=*ptr;
*ptr--=temp1;
*ptr=temp2;
} } } 57
Dynamic memory Allocation
Malloc –
Use - The C library function void *malloc(size_t
size) allocates the requested memory and returns a
pointer to it
Syntax - void *malloc(size_t size)
Where size -- This is the size of the memory block, in
bytes.
Return Value - This function returns a pointer to the
allocated memory, or NULL if the request fails.
60
Dynamic Memory Allocation
Calloc () –
The C library function void *calloc(size_t nitems, size_t
size) allocates the requested memory and returns a pointer to
it. The difference in malloc and calloc is that malloc does not
set the memory to zero where as calloc sets allocated memory
to zero.
Syntax - void *calloc(size_t nitems, size_t size)
Parameters
nitems -- This is the number of elements to be allocated.
size -- This is the size of elements.
Return Value
This function returns a pointer to the allocated memory, or NULL if
the request fails.
63
64.
example
int main()
{ inti, n; int *a;
printf("Number of elements to be entered:");
scanf("%d",&n);
a = (int*)calloc(n, sizeof(int)); printf("Enter %d
numbers:n",n);
for( i=0 ; i < n ; i++ )
{ scanf("%d",&a[i]); }
printf("The numbers entered are: ");
for( i=0 ; i < n ; i++ )
{ printf("%d ",a[i]);
}
return(0); } 64
65.
Number ofelements to be entered:3
Enter 3 numbers: 22 55 14 The
numbers entered are: 22 55 14
65
Generic pointer
void pointer inc is known as generic pointer. Literal
meaning of generic pointer is a pointer which can
point type of data.
Example:
void *ptr;
Here ptr is generic pointer.
69
Generic Pointer
4. Anytype of pointer can hold generic pointer without any
typecasting.
5. Generic pointers are used when we want to return such pointer
which is applicable to all types of pointers. For example return
type of malloc function is generic pointer because it can
dynamically allocate the memory space to stores integer, float,
structure etc. hence we type cast its return type to appropriate
pointer type
74
75.
Null pointer
Literal meaningof NULL pointer is a pointer which is pointing to
nothing. NULL pointer points the base address of segment.
Examples of NULL pointer:
1. int *ptr=(char *)0;
2. float *ptr=(float *)0;
3. char *ptr=(char *)0;
4. double *ptr=(double *)0;
5. char *ptr=’0’;
6. int *ptr=NULL;
75
76.
Null Pointer
What ismeaning of NULL?
NULL is macro constant which has been defined in
the heard file stdio.h, alloc.h, mem.h, stddef.h and
stdlib.h as
76
Example – Nullpointer
int main()
{#ifndef NULL
#define NULL 5
#endif
printf("%d",NULL+sizeof(NULL));
return 0;
}
79
Output: 2
Explanation:
NULL+sizeof(NULL)
=0+sizeoof(0)
=0+2 //size of int data
type is two byte.
Wild pointer
Wild pointer:
Apointer in c which has not been initialized is known as wild pointer.
81
Output: Any address
Garbage value#include<stdio.h>
int main()
{
int *ptr;
printf("%un",ptr);
printf("%d",*ptr);
return 0;
}
82.
Wild Vs Null
82
There is difference between the NULL
pointer and wild pointer. Null pointer
points the base address of
segment while wild pointer doesn’t point
any specific memory location.
83.
Dangling pointer:
If anypointer is pointing the memory
address of any variable but after some
variable has deleted from that memory
location whilepointer is still pointing
such memory location. Such pointer is
known as dangling pointer and this
problem is known as dangling pointer
problem.
83
Far pointer
int main()
{
int x=10;
int far *ptr;
ptr=&x;
printf("%d",sizeof ptr);
return 0;
}
90
Output: 4
Limitationof far pointer:
We cannot change or modify the
segment address of given far
address by applying any
arithmetic operation on it. That is
by using arithmetic operator we
cannot jump from one segment to
other segment. If you will
increment the far address beyond
the maximum value of its offset
address instead of incrementing
segment address it will repeat its
offset address in cyclic order.
91.
Huge Pointer
Thepointer which can point or access
whole the residence memory of RAM
i.e. which can access all the 16
segments is known as huge pointer.
91
93
Summary
A pointerstores the address (memory
location) of another entity
Address-of operator (&) gets the
address of an entity
De-reference operator (*) makes a
reference to the referee of a pointer
Pointer and array
Pointer arithmetic
Editor's Notes
#2 Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C,” presented by Michael.
#4 Due to the nature of this event, I am expecting some prior knowledge in the C programming language such as...
#8 As its name suggests, … Syntactically, a pointer variable is declared as a type identifier followed by an asterisk “*”.
#9 In fact, pointers are commonly encountered elements in C. Do you remember the simple but mysterious good old friend scanf() statement when you learnt the standard I/O facilities in C?
#21 The program segment on the left is demonstrating “pass by value”, i.e. the value, actually a copy, of the variable i is passed as argument to function f(). The other program shows you “pass by reference”. The address of the variable i is passed to f() instead, thus allowing us to modify the content stored in the variable.