Professional Documents
Culture Documents
1
Dept. of CSE, IIT KGP
Introduction
2
Dept. of CSE, IIT KGP
Basic Concept
3
Dept. of CSE, IIT KGP
Contd.
xyz Î variable
50 Î value
1380 Î address
4
Dept. of CSE, IIT KGP
Contd.
6
Dept. of CSE, IIT KGP
Address vs. Value
7
Dept. of CSE, IIT KGP
Values vs Locations
1024: 32 value
x
address
name New Type : Pointer
8
Dept. of CSE, IIT KGP
Pointers
9
Dept. of CSE, IIT KGP
Pointer
10
Dept. of CSE, IIT KGP
Pointers
11
Dept. of CSE, IIT KGP
Pointer Usage Example
0xcafe 0000
0xbeef 0000
0x0000 0004
0x0000 0000
12
Dept. of CSE, IIT KGP
Pointer Usage Example
0x0000 0004
0x0000 0000
13
Dept. of CSE, IIT KGP
Pointer Usage Example
0x0000 0004
0x0000 0000
14
Dept. of CSE, IIT KGP
Pointer Usage Example
0x0000 0004
0x0000 0000
15
Dept. of CSE, IIT KGP
Pointer Usage Example
0x0000 0004
0x0000 0000
16
Dept. of CSE, IIT KGP
Accessing the Address of a Variable
• Example:
p = &xyz;
– The address of xyz (1380) is assigned to p.
17
Dept. of CSE, IIT KGP
Contd.
int arr[20];
:
&arr;
• Pointing at array name.
&(a+b)
• Pointing at expression.
18
Dept. of CSE, IIT KGP
Example
#include <stdio.h>
main()
{
int a;
float b, c;
double d;
char ch;
19
Dept. of CSE, IIT KGP
Output:
20
Dept. of CSE, IIT KGP
Pointer Declarations
• General form:
data_type *pointer_name;
21
Dept. of CSE, IIT KGP
Contd.
• Example:
int *count;
float *speed;
22
Dept. of CSE, IIT KGP
Things to Remember
float x;
int *p;
: Î will result in erroneous output
p = &x;
int *count;
:
count = 1268;
23
Dept. of CSE, IIT KGP
Accessing a Variable Through its Pointer
int a, b;
int *p; b = a;
Equivalent to
:
p = &a;
b = *p;
24
Dept. of CSE, IIT KGP
Example 1
#include <stdio.h>
main()
{ Equivalent
int a, b;
int c = 5;
int *p;
a = 4 * (c + 5) ;
p = &c;
b = 4 * (*p + 5) ;
printf (“a=%d b=%d \n”, a, b);
} a=40 b=40
25
Dept. of CSE, IIT KGP
Example 2
#include <stdio.h>
main()
{
int x, y;
int *ptr;
x = 10 ;
ptr = &x ;
y = *ptr ;
printf (“%d is stored in location %u \n”, x, &x) ;
printf (“%d is stored in location %u \n”, *&x, &x) ;
printf (“%d is stored in location %u \n”, *ptr, ptr) ;
printf (“%d is stored in location %u \n”, y, &*ptr) ;
printf (“%u is stored in location %u \n”, ptr, &ptr) ;
printf (“%d is stored in location %u \n”, y, &y) ;
*ptr = 25;
printf (“\nNow x = %d \n”, x);
}
26
Dept. of CSE, IIT KGP
Address of x: 3221224908
Address of y: 3221224904
Address of ptr: 3221224900
Output:
Now x = 25
27
Dept. of CSE, IIT KGP
Pointer Expressions
28
Dept. of CSE, IIT KGP
Contd.
29
Dept. of CSE, IIT KGP
Contd.
30
Dept. of CSE, IIT KGP
Scale Factor
32
Dept. of CSE, IIT KGP
• Note:
– The exact scale factor may vary from one machine to another.
– Can be found out using the sizeof function.
– Syntax:
sizeof (data_type)
33
Dept. of CSE, IIT KGP
Example: to find the scale factors
#include <stdio.h>
main()
{
printf (“No. of bytes occupied by int is %d \n”, sizeof(int));
printf (“No. of bytes occupied by float is %d \n”, sizeof(float));
printf (“No. of bytes occupied by double is %d \n”, sizeof(double));
printf (“No. of bytes occupied by char is %d \n”, sizeof(char));
}
Output:
34
Dept. of CSE, IIT KGP
Passing Pointers to a Function
35
Dept. of CSE, IIT KGP
Example: passing arguments by value
#include <stdio.h>
main()
{
int a, b;
a = 5; b = 20;
swap (a, b); Output
printf (“\n a=%d, b=%d”, a, b);
} a=5, b=20
38
Dept. of CSE, IIT KGP
Example
39
Dept. of CSE, IIT KGP
Contd.
41
Dept. of CSE, IIT KGP
Arrays and pointers
42
Dept. of CSE, IIT KGP
Arrays
• Consequences:
– ar is a pointer
– ar[0] is the same as *ar
– ar[2] is the same as *(ar+2)
– We can use pointer arithmetic to access arrays more
conveniently.
• Declared arrays are only allocated while the scope is valid
char *foo() {
char string[32]; ...;
return string;
} is incorrect
43
Dept. of CSE, IIT KGP
Arrays
44
Dept. of CSE, IIT KGP
Arrays
• Pitfall: An array in C does not know its own length, & bounds
not checked!
– Consequence: We can accidentally access off the end of
an array.
– Consequence: We must pass the array and its size to a
procedure which is going to traverse it.
• Segmentation faults and bus errors:
– These are VERY difficult to find;
be careful!
– You’ll learn how to debug these in lab…
45
Dept. of CSE, IIT KGP
Arrays In Functions
} }
46
Dept. of CSE, IIT KGP
Arrays and pointers
47
Dept. of CSE, IIT KGP
Arrays and pointers
#define N 20
int a[2N], i, *p, sum;
• p = a; is equivalent to p = *a[0];
• p is assigned 300.
• Pointer arithmetic provides an alternative to array indexing.
• p=a+1; is equivalent to p=&a[1]; (p is assigned 304)
int a[N];
• a is a constant pointer.
49
Dept. of CSE, IIT KGP
Pointer arithmetic and element size
double * p, *q ;
• The expression p+1 yields the correct machine address for the
next variable of that type.
• Other valid pointer expressions:
– p+i
– ++p
– p+=i
– p-q /* No of array elements between p and q */
50
Dept. of CSE, IIT KGP
Pointer Arithmetic
– x = (*p)++ ⇒ x = *p ; *p = *p + 1;
51
Dept. of CSE, IIT KGP
Pointer Arithmetic
52
Dept. of CSE, IIT KGP
Pointer Arithmetic
53
Dept. of CSE, IIT KGP
Pointer Arithmetic
54
Dept. of CSE, IIT KGP
Example with 2-D array
TO BE DISCUSSED LATER
55
Dept. of CSE, IIT KGP
Structures Revisited
struct stud {
int roll;
char dept_code[25];
float cgpa;
};
struct stud a, b, c;
56
Dept. of CSE, IIT KGP
Arrays of Structures
57
Dept. of CSE, IIT KGP
Example :: sort by roll number (bubble sort)
#include <stdio.h> for (k=0; k<n; k++)
struct stud scanf (“%d %s %f”, &class[k].roll,
{ class[k].dept_code,
int roll; &class[k].cgpa);
char dept_code[25]; for (j=0; j<n-1; j++)
float cgpa; for (k=j+1; k<n; k++)
}; {
if (class[j].roll > class[k].roll)
main() {
{ t = class[j];
struc stud class[100], t; class[j] = class[k];
int j, k, n; class[k] = t;
}
scanf (“%d”, &n); }
/* no. of students */ <<<< PRINT THE RECORDS >>>>
}
58
Dept. of CSE, IIT KGP
Example :: selection sort
int min_loc (struct stud x[],
int k, int size) int selsort (struct stud x[],int n)
int j, pos;
{ {
pos = k; int k, m;
for (j=k+1; j<size; j++) for (k=0; k<n-1; k++)
if (x[j] < x[pos]) {
pos = j; m = min_loc(x, k, n);
return pos; temp = a[k];
} a[k] = a[m];
a[m] = temp;
}
main()
}
{
struc stud class[100];
int n;
…
selsort (class, n);
…
59
Dept. of CSE, IIT KGP
Arrays within Structures
60
Dept. of CSE, IIT KGP
Pointers and Structures
• You may recall that the name of an array stands for the
address of its zero-th element.
– Also true for the names of arrays of structure variables.
• Consider the declaration:
struct stud {
int roll;
char dept_code[25];
float cgpa;
} class[100], *ptr ;
61
Dept. of CSE, IIT KGP
– The name class represents the address of the zero-th
element of the structure array.
– ptr is a pointer to data objects of the type struct stud.
• The assignment
ptr = class;
will assign the address of class[0] to ptr.
62
Dept. of CSE, IIT KGP
• Once ptr points to a structure variable, the members
can be accessed as:
ptr –> roll;
ptr –> dept_code;
ptr –> cgpa;
63
Dept. of CSE, IIT KGP
A Warning
64
Dept. of CSE, IIT KGP
Structures and Functions
65
Dept. of CSE, IIT KGP
Example: complex number addition
#include <stdio.h> struct complex add (x, y)
struct complex { struct complex x, y;
float re; {
float im; struct complex t;
};
t.re = x.re + y.re ;
main() t.im = x.im + y.im ;
{ return (t) ;
struct complex a, b, c; }
scanf (“%f %f”, &a.re, &a.im);
scanf (“%f %f”, &b.re, &b.im);
c = add (a, b) ;
printf (“\n %f %f”, c,re, c.im);
}
66
Dept. of CSE, IIT KGP
Example: Alternative way using pointers
#include <stdio.h>
struct complex {
float re;
float im; void add (x, y, t)
}; struct complex *x, *y, *t;
{
main() t->re = x->re + y->re;
{ t->im = x->im + y->im;
struct complex a, b, c; }
scanf (“%f %f”, &a.re, &a.im);
scanf (“%f %f”, &b.re, &b.im);
add (&a, &b, &c) ;
printf (“\n %f %f”, c,re, c.im);
}
67
Dept. of CSE, IIT KGP
Dynamic Memory Allocation
68
Dept. of CSE, IIT KGP
Basic Idea
69
Dept. of CSE, IIT KGP
Contd.
70
Dept. of CSE, IIT KGP
Memory Allocation Process in C
71
Dept. of CSE, IIT KGP
Contd.
• malloc
– Allocates requested number of bytes and returns a pointer
to the first byte of the allocated space.
• calloc
– Allocates space for an array of elements, initializes them to
zero and then returns a pointer to the memory.
• free
Frees previously allocated space.
• realloc
– Modifies the size of previously allocated space.
73
Dept. of CSE, IIT KGP
Allocating a Block of Memory
• General format:
ptr = (type *) malloc (byte_size);
74
Dept. of CSE, IIT KGP
Contd.
• Examples
p = (int *) malloc(100 * sizeof(int));
76
Dept. of CSE, IIT KGP
Points to Note
77
Dept. of CSE, IIT KGP
Example
#include <stdio.h> printf("Input heights for %d
students \n",N);
main() for (i=0; i<N; i++)
{ scanf ("%f", &height[i]);
int i,N;
float *height; for(i=0;i<N;i++)
float sum=0,avg; sum += height[i];
78
Dept. of CSE, IIT KGP
Releasing the Used Space
• How?
– By using the free function.
• General syntax:
free (ptr);
where ptr is a pointer to a memory block which has
been previously created using malloc.
79
Dept. of CSE, IIT KGP
Altering the Size of a Block
• How?
– By using the realloc function.
80
Dept. of CSE, IIT KGP
Contd.
– The new memory block may or may not begin at the same
place as the old one.
• If it does not find space, it will create it in an entirely
different region and move the contents of the old block
into the new block.
– The function guarantees that the old data remains intact.
– If it is unable to allocate, it returns NULL and frees the
original block.
81
Dept. of CSE, IIT KGP
Pointer to Pointer
• Example:
int **p;
p = (int **) malloc(3 * sizeof(int *));
p[0]
p int ** int *
p[1] int *
int *
p[2]
82
Dept. of CSE, IIT KGP
2-D Array Allocation
85
Dept. of CSE, IIT KGP
Contd.
86
Dept. of CSE, IIT KGP
Contd.
• Each structure of the list is called a node, and
consists of two fields:
– One containing the item.
– The other containing the address of the next item in the
list.
• The data items comprising a linked list need not
be contiguous in memory.
– They are ordered by logical links that are stored as part
of the data in the structure itself.
– The link is a pointer to another structure of the same
type.
87
Dept. of CSE, IIT KGP
Contd.
88
Dept. of CSE, IIT KGP
Contd.
struct node_name
{
type member1;
type member2;
………
struct node_name *next;
}
89
Dept. of CSE, IIT KGP
Illustration
90
Dept. of CSE, IIT KGP
Contd.
roll
name
age
next
n2 n3
n1
91
Dept. of CSE, IIT KGP
Example
#include <stdio.h>
struct stud
{
int roll;
char name[30];
int age;
struct stud *next;
}
main()
{
struct stud n1, n2, n3;
struct stud *p;
93
Dept. of CSE, IIT KGP
Alternative Way
94
Dept. of CSE, IIT KGP