Professional Documents
Culture Documents
(CS F111)
Pointers and Arrays
BITS Pilani Dr. Lov Kumar, Dr. Sudeepta Mishra, Mr. Abhishek Thakur
Hyderabad Campus
Department of CSIS
Concept of Address and
Pointers
• Memory can be ADDR1 Contents1
conceptualized as a linear set ADDR2
of data locations. ADDR3
ADDR4
• Variables reference the ADDR5
contents of a locations ADDR6
*
• Pointers have a value of the *
*
address of a given location
ADDR11 Contents11
*
*
ADDR16 Contents16
char ch = ‘A’;
ch:
0x2000 'A'
The memory
address of the The value of the
variable ch variable ch
4 8/14/2018 CS F111 BITS Pilani, Hyderabad Campus
The & Operator (Address of)
char ch = 'A';
0x2000 'A'
&ch yields the value 0x2000
char ch;
printf("%p", &ch);
Example:
char c = 'A';
char *cPtr;
char* cPtr;
cPtr:
0x2004
0x2000
cPtr
0x2002
0x1FFE 0x1FFF 0x2000 0x2001
etc
'A'
c
12 8/14/2018 CS F111 BITS Pilani, Hyderabad Campus
Initializing Pointers
Correct Incorrect
int i, j; int y, z;
int *x,*y; z = &y; /* z is not a pointer variable, will
x=&i; /* address of i assigned to x */ give warning */
y=&j; /* address of j assigned to y */
int y;
float f = 4.5f; int *z;
float *fPtr = &f; z = y; /* z must reference y with
address operator, will give warning*/
double d = 3.14;
double *d1Ptr; int y;
d1Ptr = &d; char *c1;
c1=&y;
double *d2Ptr; /* c1 and y are not of the same type, will
d2Ptr = d1Ptr; give a warning */
Beware of pointers
int *numPtr; which are not
initialized!
???
numPtr
NULL
numPtr
int main()
{ You do not end up
int *p; assigning a value to the
scanf("%p",&p); memory location
printf("%p",p);
Gives segmentation
*p=10; fault
printf("%d",*p);
}
c: cPtr:
B
A 0x2000
0x2000
int num;
char ch = 'A';
float x;
num:
ch: ‘A’
x:
float x;
chPtr: addr of ch
int* numPtr = NULL;
char *chPtr = NULL; xPtr: addr of x
float * xPtr = NULL;
numPtr = #
chPtr = &ch; num: 65
xPtr = &x;
ch: ‘A’
*xPtr = 0.25;
*numPtr = *chPtr; x: 0.25
pa
a 10
0X2000
ptra
#include <stdio.h>
int main()
If you write int *p2 instead of int **p2,
{ then you will get a warning and
int x, *p1,**p2; printf(“%d”, **p2);
x=100; will give error.
p1=&x;
p2=&p1;
printf(“%d”, **p2);
return 0;
}
int i = 2, j = 3, k;
k = sizeof(++i + ++j); // sizeof() returns 4 or 8 (bytes)
printf(“%d %d %d”, i, j, k);
Output : 2 3 4
sizeof() operator doesn't evaluate the expression, it just
replaces variable with its type so ++i is not evaluated.
• type *a;
(i) addition :
constant(k) : a + k => a + k*sizeof(type)
Ex : int b = 10;
int *a = &b; // address of b is 1000
a = a + 2;
printf(“%p\n”, a); // output is 1008
(ii) subtraction:
An integer can be subtracted from a pointer variable
Ex : int b = 10;
int *a = &b; // address of b is 1008
a = a - 2;
printf(“%p\n”, a); // output is 1000
p1 = p1 – p2 * 10; // incorrect
p1 = p2 / 5; // incorrect
For Ex :
p = &a;
q = &b;
p <= q
p >= q
Ex. p == NULL
p != q
• Syntax
void *data_type;
Generic Pointer
It is valid to assign address of any variable to void *
variable & vice versa
Casting is not required while performing assignment to
void *
Cannot use indirection operator on void *
33 8/14/2018 CS F111 BITS Pilani, Hyderabad Campus
example
const int a = 5;
a += 5; // error
Pointer to a constant :
#include<stdio.h>
int main()
{
int a = 5;
const void *ptr;
ptr = &a;
printf("%p\n",ptr);
printf("%d\n",*((int *)ptr));
*((int *)ptr) = 100; // does not cause error
*ptr = 200; // causes error
a++;
printf("%d\n",*((int *)ptr));
return 0;
}
#include<stdio.h>
int main()
{
int a = 5;
const int *ptr;
ptr = &a;
printf("%p\n",ptr);
ptr++;
printf("%p\n",ptr);
return 0;
}
OUTPUT:
0x7fff529f8be8
0x7fff529f8bec
38 8/14/2018 CS F111 BITS Pilani, Hyderabad Campus
constant pointer to Variable
#include<stdio.h>
int main()
{
int a = 5;
float b = 20;
void *const ptr = &a;
ptr = &b;
return 0;
}
#include<stdio.h>
ptr++; will also cause error
int main()
{
int i = 10;
int j = 20;
const void *const ptr = &i;
printf("ptr: %d\n", *(int *)ptr);
ptr = &j;
*ptr = 100;
return 0;
}
error: cannot assign to variable 'ptr' with error: read-only variable is not
const-qualified type'const void *const’ assignable
ptr = &j *ptr = 100;
int main()
{
int a, b;
int *px;
px = process(a, b);
return 0;
}
Data Types
(2) (3)
(1)
Primitive User-defined
Derived Types
Types Types
• All the data items constituting the group share the same name.
int x[10];
• Individual elements are accessed by specifying the index.
• General syntax:
type array-name [size];
– type specifies the type of elements that will be contained in
the array (int, float, char, etc.)
– size is an integer constant which indicates the maximum
number of elements that can be stored inside the array.
int marks[5];
– marks is an array containing a maximum of 5 integers.
Examples:
int x[10];
char line[80];
float points[150];
char name[35];
• If we are not sure of the exact size of the array, we can define an
array of a large size.
int marks[50];
though in a particular run we may only be using, say, 10
elements.
• No checking!
• C is an UNSAFE language!
int my_array[5];
my_array[5] = { 1, 2, 3, 4, 5 }; /* WRONG */
Output:
10 6 8 20 1 5 11 9
Subtraction :
a.) constant(k) : a – k => a – k * sizeof(type)
int a[7];
int *q = &a[4];
int *p = &a[0];
int k = (q – p); // k = (q - p) / sizeof(type)
printf("k = %d\n", k);
int a[10];
int *pa;
pa = &a[0];
pa+1, pa+2, …
*(pa+1) a[1]
*(pa+2) a[2]
#include<stdio.h>
int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int *pa = &a[0];
for(int i = 0;i<10;i++)
printf("%d %p\n",*(pa+i),pa+i);
}
Output:
1 0x7fff578b5bc0
2 0x7fff578b5bc4
3 0x7fff578b5bc8
4 0x7fff578b5bcc
5 0x7fff578b5bd0
6 0x7fff578b5bd4
7 0x7fff578b5bd8
8 0x7fff578b5bdc
9 0x7fff578b5be0
CS F111 BITS Pilani, Hyderabad Campus
Arrays & Pointers
pa = &a[0] pa = a;
#include<stdio.h>
int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int *pa = a;
for(int i = 0;i<10;i++)
printf("%d %p\n",*(a+i),a+i);
}
Output:
1 0x7fff578b5bc0
2 0x7fff578b5bc4
3 0x7fff578b5bc8
4 0x7fff578b5bcc
5 0x7fff578b5bd0
6 0x7fff578b5bd4
7 0x7fff578b5bd8
8 0x7fff578b5bdc
9 0x7fff578b5be0
CS F111 BITS Pilani, Hyderabad Campus
Arrays & Pointers
#include<stdio.h>
int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
for(int i = 0;i<10;i++)
printf("%d %p\n",i[a], &i[a]);
}
Output:
1 0x7fff578b5bc0
2 0x7fff578b5bc4
3 0x7fff578b5bc8
4 0x7fff578b5bcc
5 0x7fff578b5bd0
6 0x7fff578b5bd4
7 0x7fff578b5bd8
8 0x7fff578b5bdc
9 0x7fff578b5be0
CS F111 BITS Pilani, Hyderabad Campus
Arrays & Pointers
#include<stdio.h>
int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int *pa = &a[9];
for(int i = 9;i>=0;i--)
printf("%d\n",pa[-i]);
}
Output:
1
2
3
4
5
6
7
8
9
10
CS F111 BITS Pilani, Hyderabad Campus
Pass 1D Arrays as Function
argument
• You need to declare array_name and number of
elements in the array as formal argument.
• To call such function you need to pass array_name
and number of elements in the function call.
#include<stdio.h>
• Wastage of memory
• Consecutive space might be unavailable
• Operation like insertion and deletion is time
consuming
• Ex :
• Profit or sales chart of companies
• Point table of a series (in Cricket, football etc…)
• Movie ratings by multiple reviewers
4 9 0
13 -10 2
A= similarly you have matrices B,C
5 3 7
10 0 -3
Product
0 1 2 3
customer 0 4 3 5 1
1 3 5 4 2
2 5 4 2 5
Examples:
int rating[3][4];
int marks[5][10];
float profit[12][25];
double matrix[100][100];
int (*p)[3] = B;
102 8/14/2018 CS F111 BITS Pilani, Hyderabad Campus
Pass 2D array as Function
argument
You have to mention column size in the 2D array parameter
int r,c;
scanf(“%d %d”, &r, &c);
int a[r][c]; // dynamic allocation
•You need to pass first row then column and then array
name