04.array Programming With Pointer Concept
04.array Programming With Pointer Concept
k a
How to declare an array?
n e e a
y a r j
dataType arrayName[arraySize];
k e
ir te
For example,
a n j e
P at i y e r a
h
float mark[5];
P r t t k
C e
Here, we declared an array, mark, of floating-point type. And its size is 5. Meaning, it
a
can hold 5 floating-point values.
h a n j e
C y
ir te r
It's important to note that the size and type of an array cannot be changed once it is
P at
declared.
h
C
a
Access Array Elements
k e
You can access elements of an array by indices.
a n j e a
k
Suppose you declared an array mark as above. The first element is mark[0], the second element is
y
ir te r
mark[1] and so on.
n e
P at a r j e
r i y t e a
h P t k e
C
Few keynotes:
a n e
h a j
Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element.
C y
ir te r
If the size of an array is n, to access the last element, the n-1 index is used. In this example, mark[4]
P at
Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will be 2124d.
h
Similarly, the address of mark[2] will be 2128d and so on.
This is because the size of a float is 4 bytes.
C
a
How to initialize an array?
k
e e
It is possible to initialize an array during
n
declaration. For example,
a
y a r j k e
ir te n
int mark[5] = {19, 10, 8, 17, 9};
a j e
P at r
You can also initialize an array like this.
r i y t e a
h t
int mark[] = {19, 10, 8, 17, 9};
P k e
C a n e
h y a r j
C ir te
P at
h
C
k a
Access elements out of its bound!
n e e a
Suppose you declared an array of 10 elements. Let's say,
y a r j k e
ir te
int testArray[10];
a n j e
P at
You can access the array elements from testArray[0] to testArray[9].
i y e r a
h r t
Now let's say if you try to access testArray[12].
P t k
C a n e e
h a j
The element is not available. This may cause unexpected output (undefined
run correctly. C y
ir te r
behavior). Sometimes you might get an error and some other time your program may
P at
Hence, you should never access elements of an array outside of its bound.
h
C
k a
C Multidimensional Arrays
n e e
In C programming, you can create an array of arrays. These arrays are known as
a
a j k
multidimensional arrays. For example,
y r e
ir te a n j e
P at
float x[3][4];
i y r
Here, x is a two-dimensional (2d) array. The array can hold 12 elements. You can
e a
h r t
think the array as a table with 3 rows and each row has 4 columns.
P t k
C a n e e
h y a r j
C ir te
P at
h
C
a
Similarly, you can declare a three-dimensional
k
n e e
(3d) array. For example,
a
y a r
float y[2][4][3];j k e
ir te n
Here, the array y can hold 24 elements.
a j e
P at i y e r a
h P r t t k
C a n e e
h y a r j
C ir te
P at
h
C
a
Initializing a multidimensional array
k
n e e a
Here is how you can initialize two-dimensional
y a r j
and three-dimensional arrays:
k e
ir te a n j e
P at r
Initialization of a 2d array
i y e
// Different ways to initialize two-dimensional
r t a
array
h P t k e
C a n e
h
int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
y a r j
C
int c[][3] = {{1, 3, 0}, {-1, 5, 9}}; ir te
P at
h
int c[2][3] = {1, 3, 0, -1, 5, 9};
C
k a
Initialization of a 3d array
n e e a
y a r j k
You can initialize a three-dimensional array in a
e
ir te a n
similar way like a two-dimensional array. Here's
j e
P at
an example,
i y e r a
h
int test[2][3][4] = {
P r t t k
C a
{{3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2}},
n e e
h a j
{{13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9}}};
C y
ir te r
P at
h
C
k a
Row- and column-major order
n e
In computing, row-major order and
e a
y a r j
column-major order are methods for
k
storing multidimensional arrays in linear
e
ir te n
storage such as random access memory.
a j e
P at i y e r
The difference between the orders lies in
a
h r
which elements of an array are
P t t k e
contiguous in memory.
C a n e
h
In row-major order, the consecutive
y a r j
C
elements of a row reside next to each
other,
ir te
whereas the same holds true for
P at
consecutive elements of a column in
h
C
column-major order.
a
12. Consider the following declaration of a two-dimensional
array in C:
n k e
a
char a[100] [100];
r j e k a
y
ir te n e e
Assuming that the main memory is byte- addressable and
P at y a r j
that the array is stored starting from memory address 0,
h r i t e a
C a t
the address of a[40][50] in column major order is
P n k e
(A) 4040
h a r j e
(B) 4050
C y
ir te
P at
(C) 5040
h
(D) 5050
C WB SET 2023
a
12. Consider the following declaration of a two-dimensional
array in C:
n k e
a
char a[100] [100];
r j e k a
y
ir te n e e
Assuming that the main memory is byte- addressable and
P at y a r j
that the array is stored starting from memory address 0,
h r i t e a
C a t
the address of a[40][50] in column major order is
P n k e
(A) 4040
h a r j e
(B) 4050
C y
ir te
P at
(C) 5040
h
(D) 5050
C WB SET 2023
k a
n e e a
y a r j k e
ir te a n j e
P at i y e r a
h P r t t k
C a n e e
h y a r j
C ir te
P at
h
C
k a
e
Q.A three dimensional array in ‘C’ is declared as int A[x][y][z]. Consider that array
n e a
elements are stored in row major order and indexing begins from 0. Here, the address
a j
ir te r
length of an integer):
n k
of an item at the location A[p][q][r] can be computed as follows (where w is the word
y e
P at a r j e
i y e
(A) &A[0][0][0] + w(y * z * q + z * p + r)
r t a
h P t
(B) &A[0][0][0] + w(y * z * p + z*q + r)
k e
C a
(C) &A[0][0][0] + w(x * y * p + z * q+ r)
n e
h
(D) &A[0][0][0] + w(x * y * q + z * p + r)
y a r j
C ir te
P at
h UGC NET CS 2015 Dec – II
C
k a
n e e a
y a r j k e
ir te a n j e
P at i y e r a
h P r t t k
C a n e e
h y a r j
C ir te
P at
h
C
k a
e
Q.A three dimensional array in ‘C’ is declared as int A[x][y][z]. Consider that array
n e a
elements are stored in row major order and indexing begins from 0. Here, the address
a j
ir te r
length of an integer):
n k
of an item at the location A[p][q][r] can be computed as follows (where w is the word
y e
P at a r j e
i y e
(A) &A[0][0][0] + w(y * z * q + z * p + r)
r t a
h P t
(B) &A[0][0][0] + w(y * z * p + z*q + r)
k e
C a
(C) &A[0][0][0] + w(x * y * p + z * q+ r)
n e
h
(D) &A[0][0][0] + w(x * y * q + z * p + r)
y a r j
C ir te
P at
h UGC NET CS 2015 Dec – II
C
Question ID : 87056
k a
n
A three dimensional array in C++ is declared as int A[a][b][c]. Consider that
a j e e
array elements are stored in row major order and indexing begin from [Link]
the address of an item at the location A[r][s][t] computed in terms of word
i y r
length w of an integer is
1.
2.
3.
4.
r
P att e
&A[0][0][0]+w(b*c*s+c*r+t)
&A[0][0][0]+w(b*c*r+c*s+t)
&A[0][0][0]+w(a*b*r+c*s+t)
&A[0][0][0]+w(a*b*s+c*r+t)
k a
n
A three dimensional array in C++ is declared as int A[a][b][c]. Consider that
a j e e
array elements are stored in row major order and indexing begin from [Link]
the address of an item at the location A[r][s][t] computed in terms of word
i y r
length w of an integer is
1.
2.
3.
4.
r
P att e
&A[0][0][0]+w(b*c*s+c*r+t)
&A[0][0][0]+w(b*c*r+c*s+t)
&A[0][0][0]+w(a*b*r+c*s+t)
&A[0][0][0]+w(a*b*s+c*r+t)
y r j
C ir te
int* p1, p2; // Here, we have declared a pointer p1 and a normal variable p2.
P at
h
C
a
Assigning addresses to Pointers
k
Let's take an example.
n e e a
y a
int* pc, c;
r j k e
ir te
c = 5;
a n j e
P at r
pc = &c; //Here, 5 is assigned to the c variable. And, the address of c is assigned to the pc pointer.
i y e
printf("%d", *pc); // Get Value of Thing Pointed by Pointers Output: 5
r t a
h P t k e
C a
Here, the address of c is assigned to the pc pointer. To get the value stored in that address, we used *pc.
n e
h a r j
Note: In the above example, pc is a pointer, not *pc. You cannot and should not do something like *pc
y
= &c;
C ir te
P at
By the way, * is called the dereference operator (when working with pointers). It operates on a pointer
and gives the value stored in that pointer.
h
C
a
Q Which of the following are legal statements in C
k e
programming language?
n
(a) int *P = &44;
a
(b) int *P = &r;
j e a
y
(c) int P = &a;
ir te r n k e
e
(d) int P = a:
P at
Choose the correct option:
y a r j
h r i t e a
t
A (a) and (b)
C P a n k e
e
B (b) and (c)
h y a r j
C (b) and (d)
C ir te
D (a) and (d)
P at
h NTA NET Dec 2019
C
a
Q Which of the following are legal statements
k e
in C programming language?
a n j
(a) int *P = &44;
e a
y r
(b) int *P = &r;
ir te n k e
(c) int P = &a;
P at
(d) int P = a:
a r j e
i y
Choose the correct option:
r t e a
h
A (a) and (b)
P t k e
C
B (b) and (c)
a n e
h y a r j
C (b) and (d)
C ir te
D (a) and (d)
P at
h NTA NET Dec 2019
C
k a
n e e a
y a r j k e
ir te a n j e
P at i y e r a
h P r t t k
C a n e e
h y a r j
C ir te
P at
h
C
NULL Pointers
k a
e
It is always a good practice to assign a NULL value to a pointer variable in case you do not have an
n e a
exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned
a j
r k
NULL is called a null pointer.
y
ir te n e e
a j
The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider the
P at y r
following program −
h r i t e a
t k
int *ptr = NULL;
C P a n e e
h a j
To check for a null pointer, you can use an 'if' statement
as follows −
C y
ir te r
P at
if(ptr) /* succeeds if p is not null */
if(!ptr) /* succeeds if p is null */
h
C
a
Pointer Expressions and Pointer Arithmetic
k
n e e
A limited set of arithmetic operations can be performed on pointers. A pointer may
a
a j
be:
y r
•incremented ( ++ )
ir te n k e
•decremented ( — )
P at a r j e
i y e
•an integer may be added to a pointer ( + or += )
r t a
h t k
•an integer may be subtracted from a pointer ( – or -= )
P e
C a n e
h a j
Pointer arithmetic is meaningless unless performed on an array.
y r
C ir te
Note : Pointers contain addresses. Adding two addresses makes no sense, because
P at
there is no idea what it would point to. Subtracting two addresses lets you compute
the offset between these two addresses.
h
C
int main()
{
k a Output:Value of *ptr = 10
n e e a
int v[3] = {10, 100, 200}; // Declare an
Value of ptr = 0x7ffcae30c710
array
y a r j k e
Value of *ptr = 100
ir te a n
int *ptr; // Declare pointer variable
j e
Value of ptr = 0x7ffcae30c714
P at
ptr = v; // Assign the address of v[0] to ptr
i y e r a
Value of *ptr = 200
r t
Value of ptr = 0x7ffcae30c718
hfor (int i = 0; i < 3; i++)
P t k e
C {
a n e
h a j
printf("Value of *ptr = %d\n", *ptr);
C y
printf("Value of ptr = %p\n\n", ptr);
ir te
ptr++; // Increment pointer ptr by 1 r
} P at
}
h
C
k a
Array Name as Pointers // Declare an array
n e e
An array name acts like a
a
int val[3] = { 5, 10, 15};
y a r j
pointer constant. The value
k e
ir te
of this pointer constant is
j e
P at
the address of the first int *ptr;
element.
i y e r a
h r
For example, if we have an
P at
cout << ptr[0] << " " << ptr[1] << " " << ptr[2];
h
C
Question ID : 87827025066
k a a
Consider the following code segment:
n e e
int arr [] ={0,1,2,3,4};
k e
a
Int i=1, *ptr;
y r j a n j e
ir te r
ptr=arr+2;
i y e a
Arrange the following printf statements in the increasing order of their output.
t r t
P a
A. Printf (“%d”, ptr[i]);
P t n k e
h a
B. Printf (“%d”, ptr[i+1]);
C. Printf (“%d”, ptr[-i]);
C h a r j e
C
D. Printf (“%d”, ptr[-i+1]);
y
ir te
P at
Choose the correct answer from options given below:
h
1. C, A, B, D
2. C, D, A, B
3. D, A, B, C
4. A, B, D, C
C UGC NET CS 7th December 2023
Question ID : 87827025066
k a a
Consider the following code segment:
n e e
int arr [] ={0,1,2,3,4};
k e
a
Int i=1, *ptr;
y r j a n j e
ir te r
ptr=arr+2;
i y e a
Arrange the following printf statements in the increasing order of their output.
t r t
P a
A. Printf (“%d”, ptr[i]);
P t
=>3
n k e
h a
B. Printf (“%d”, ptr[i+1]); => 4
C. Printf (“%d”, ptr[-i]);
C h
=> 1
a r j e
C
D. Printf (“%d”, ptr[-i+1]); =>2
y
ir te
P at
Choose the correct answer from options given below:
h
1. C, A, B, D
2. C, D, A, B
3. D, A, B, C
4. A, B, D, C
C UGC NET CS 7th December 2023
k a
Pointers and Multidimensional Arrays
n e
Consider pointer notation for the two-dimensional numeric arrays. consider the following declaration
e a
a j
int nums[2][3] = { {16, 18, 20}, {25, 26, 27} };
y
ir te r n k
nums[i][j] is equivalent to *(*(nums+i)+j)
e
In general,
P at a r j e
r i y t e a
h P t k e
C a n e
h y a r j
C ir te
P at
h
C
Question ID : 4255894968
k a
What would be the equivalent pointer expression for referring
n e e a
y a r j
the array element ar[m][n][o]
k e
ir te
(1). * (* (* (ar)+m+n)+o)
a n j e
P at
(2). (*(* (*ar+m)+n)+o)
i y e r a
h
(3). (* (* (ar +m)+n) +o)
P r t t k
C
(4). * (* (* (ar+m)+n)+o)
a n e e
h y a r j
C ir te
P at
h
C NTA UGC NET JUNE 2025
Question ID : 4255894968
k a
What would be the equivalent pointer expression for referring
n e e a
y a r j
the array element ar[m][n][o]
k e
ir te
(1). * (* (* (ar)+m+n)+o)
n
Correct Answer: (4) ((*(ar + m) + n) + o)
a j e
P at
(2). (*(* (*ar+m)+n)+o)
i y r
Explanation:
e a
h
(3). (* (* (ar +m)+n) +o)
P r t t k
• ar → start of the 3D array
C
(4). * (* (* (ar+m)+n)+o)
a n
• ar + m → jumps to block m
e e
h a j
• *(ar + m) + n → goes to row n in that block
C y r
• *(*(ar + m) + n) + o → goes to element o in that row
ir te
P at
• Finally, *(*(*(ar + m) + n) + o) → gives the actual value
at ar[m][n][o]
h
C NTA UGC NET JUNE 2025
k a
#include <stdio.h> A)5
n
int main () {
e e a
B) 6
a j
int arr [] = {1,2,3,4,5,6,7,8,9,0,1,2,5}, C) 7
y
ir te r n k
*ip = arr+4;
printf ("%d\n", ip[1]);
e D8
P at
return 0;
a r j e
y
}
h r i t e a
C P t k e
The number that will be displayed on execution of the program is _____.
a n
h a r j e
C y
ir te
P at
h
C
k a
#include <stdio.h> A)5
n
int main () {
e e a
B) 6
a j
int arr [] = {1,2,3,4,5,6,7,8,9,0,1,2,5}, C) 7
y
ir te r n k
*ip = arr+4;
printf ("%d\n", ip[1]);
e D8
P at
return 0;
a r j e
y
}
h r i t e a
C P t k e
The number that will be displayed on execution of the program is _____.
a n
h a r j e
C y
ir te
P at
h
C
k a
#include <stdio.h>
e
A3
n
int main()
a j e a B7
y
{
ir te r n k
int a[] = {2, 4, 6, 8, 10} ;
e
C 11
D 10
P at a
int i, sum = 0, *b = a + 4 ;
r j e GATE 2019
r i y
for (i = 0; i < 5; i++)
t e a
h sum = sum + (*b - i) - *(b - i) ;
P t k e
C a
printf ("%d\n", sum) ;
n e
h
return 0 ;
}
C y a r j
ir te
P at
h
C
k a
#include <stdio.h>
e
A3
n
int main()
a j e a B7
y
{
ir te r n k
int a[] = {2, 4, 6, 8, 10} ;
e
C 11
D 10
P at a
int i, sum = 0, *b = a + 4 ;
r j e GATE 2019
r i y
for (i = 0; i < 5; i++)
t e a
h sum = sum + (*b - i) - *(b - i) ;
P t k e
C a
printf ("%d\n", sum) ;
n e
h
return 0 ;
}
C y a r j
ir te
P at
h
C
[Link] : 5330723730
a
#include<stdio.h>
k
void main()
n e e a
a j
{
y
ir te r k
int arr[]=(1, 2, 3, 4, 5);
n e
P at
int *p = arr;
a r j e
y
printf("%d", *p++);
h i
printf("%d", *(p+1));
r t e a
C
}
P a t n k e
h
Find the output of the above code?
a r j e
(1) 1, 2
C y
ir te
P at
(2) 1, 3
(3) 2, 3
(4) 1, 4
h
C UGC NET AUG- 2024
[Link] : 5330723730
a
#include<stdio.h>
k
void main()
n e e a
a j
{
y
ir te r k
int arr[]=(1, 2, 3, 4, 5);
n e
•*p++ means → *(p++)
P at
int *p = arr; arr[0] → 1
a r j e
•Post-increment happens after dereferencing
y
printf("%d", *p++); → 1
h i e
printf("%d", *(p+1)); ->arr[2] = 3
r t a
C
}
P a t n k e
h
Find the output of the above code?
a r j e
(1) 1, 2
C y
ir te
P at
(2) 1, 3
(3) 2, 3
(4) 1, 4
h
C UGC NET AUG- 2024
[Link] : 5330723746
k a
What will be the output of the following C code?
n
#include<stdio.h>
e e a
y a
void main()
r j k e
ir te
{
a n
int arr[5]={10, 20, 30, 40, 50};
j e
P at
int *p = (int*) (&arr+1);
i y e r a
h r t
printf("%d %d", “*(arr+1), *(p-1));
P t k e
}
C a n e
(1) 10 50 h y a r j
(2) 20 50
C ir te
P at
(3) 30 40
(4) 20 40
h
C UGC NET AUG- 2024
[Link] : 5330723746
k a
What will be the output of the following C code?
n
#include<stdio.h>
e e a
y a
void main()
r j k e
ir te
{
a n
int arr[5]={10, 20, 30, 40, 50};
j e
P at
int *p = (int*) (&arr+1);
i y e r a
*(arr+1)-> arr[1] = 20
h P r t t
printf("%d %d", “*(arr+1), *(p-1));
k
int *p = (int*) (&arr+1);: The
e
}
C
resulting address is then cast
h a a n j e
to an int * (pointer to an
integer) and assigned to p.
(1) 10 50
(2) 20 50
C y
ir te r
This means p now points to
the memory address just after
P at
(3) 30 40 the last element of the array.
*(p-1)= 50
(4) 20 40
h
C UGC NET AUG- 2024
[Link] : 5330723744
k a
Consider following C program:
#include<stdio.h>
n
int main()
e e a
a j
{
y
ir te r
int a, b=0, *y=x+4;
n k
int x[]={2, 4, 6, 8, 10};
e
P at
for(a=0; a<5; a++)
a r j e
{
r i y t
b=b+(*y-a)-*(y-a);
e a
h }
P t k e
C
printf("%d\n", b);
a n e
h a j
return 0;
}
C y
ir te r
P at
What will be the output of the above C program?
(1) 4
h
(2) 6
(3) 8
(4) 10
C UGC NET AUG- 2024
k a
n e e a
y a r j k e
ir te a n j e
P at i y e r a
h P r t t k
C a n e e
h y a r j
C ir te
P at
h
C
[Link] : 5330723744
k a
Consider following C program:
#include<stdio.h>
n
int main()
e e a
a j
{
y
ir te r
int a, b=0, *y=x+4;
n k
int x[]={2, 4, 6, 8, 10};
e
P at
for(a=0; a<5; a++)
a r j e
{
r i y t
b=b+(*y-a)-*(y-a);
e a
h }
P t k e
C
printf("%d\n", b);
a n e
h a j
return 0;
}
C y
ir te r
P at
What will be the output of the above C program?
(1) 4
h
(2) 6
(3) 8
(4) 10
C UGC NET AUG- 2024