Presented by: Course Teacher:
1. Rahid Hasan Robin
ID: 01506452
Maksudur Rahman
2. Sanjida Binte Rahman
ID: 01506430
3. Fardin Hossen
ID: 01506448
4. [Link] Kabir
ID: 01506443
5. Muhammod Salauddin
ID: 01506431(Leader)
ARRAY &
POINTER
❖ WHAT IS ARRAY?
An array is a collection of data
items, all of the same type,
accessed using a common name.
Syntax : data_type array_name[array size];
❖ TYPES OF ARRAY
•One Dimensional Array
•Two Dimensional Array
•Multi-Dimensional Array
❖ ONE DIMENSIONAL ARRAY :
Num[0] Num[1] Num[2] Num[3] Num[4]
5 8 7 6 0
Element 1 Element 2 Element 3 Element 4 Element 5
Syntax : data_type array_name[size]
#include <stdio.h> OUTPUT:
int main()
{ 7
Process returned
int arr[5]={5,6,7,8,9};
0 (0x0)
printf("%d", arr[2]); execution time :
4.437 s
return 0; Press any key to
} continue.
❖ TWO DIMENSIONAL ARRAY :
Column 0 Column 1 Column 1
Row 0 num[0][0] num[0][1] num[0][2]
Row 1 num[1][0] num[1][1] num[1][2]
Row 2 num[2][0] num[2][1] num[2][2]
Syntax : data_type array_name[row][column];
OUTPUT:
#include<stdio.h>
Enter arr[0][0]: 1
int main() Enter arr[0][1]: 2
Enter arr[0][2]: 3
{ Enter arr[1][0]: 1
int arr[3][3],i, j; Enter arr[1][1]: 2
Enter arr[1][2]: 3
Enter arr[2][0]: 1
for(i = 0; i < 3; i++) Enter arr[2][1]: 2
{ Enter arr[2][2]: 3
for(j = 0; j < 3; j++)
{ Process returned 0 (0x0)
printf("Enter arr[%d][%d]: ", i, j); execution time : 6.880 s
scanf("%d", &arr[i][j]); Press any key to continue.
}
}
return 0;
}
❖ MULTI-DIMENSIONAL ARRAY :
1 2 3
4 5 6
7 8 9
Syntax : data_type array_name[size1][size2][size3]….[sizeN];
Initialization:
int arr[2][3][4] = { {3, 4, 7}, {0, -3, 9}, {3, 12, 2} },
{ {1, 4, 6, }, {5, 9, 5}, {1, 4, 9} };
POINTER
❖ WHAT IS POINTER ?
A pointer is a variable which
contains the address in
memory of another variable.
❖ FIGURE OF POINTER
Location
Name i J
Value 6 6004
6004 6002
Address
Of
Location
❖ DECLARATION OF POINTER VARIABLES
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 a fixed data_type.
FOR EXAMPLE:
Int *p;
It declares the variable p as
a pointer variable that points
to an integer data type.
EXAMPLE: OUTPUT:
#include <stdio.h>
int main ()
Address of var variable:
{ 60ff08
int var = 20; Address stored in ip
int *ip variable: 60ff08
ip = &var;
printf("Address of var variable: Value of *ip variable: 20
%x\n", &var );
printf("Address stored in ip Process returned 0 (0x0)
variable: %x\n", ip );
printf("Value of *ip variable:
execution time : 7.659 s
%d\n", *ip ); Press any key to continue.
return 0;
}
Difference Between Ampersand and
Asterisk operators
Ampersand (&) Asterisk (*)
Returns address of Returns the value of
the given variable. the given variable.
NULL POINTER
A pointer that is assigned NULL is called a null pointer.
#include <stdio.h> OUTPUT:
int main ()
{ The value of ptr is : 0
int *ptr = NULL;
printf("The value of ptr is Process returned 0 (0x0)
: %x\n", ptr ); execution time : 3.437 s
return 0; Press any key to
} continue.
3
2
Int**p;p; 1 Pointer
❖ ADVANTAGES OF POINTER
Efficient to Supports on Reduce the
handle dynamic length and Provides Increase
complexity of direct access execution
arrays and memory to memory. speed.
program.
data tables. management.
POINTERS AND FUNCTIONS
#include <stdio.h> OUTPUT:
void swap(int *a, int *b);
int main() m = 10
{ n = 20
int m = 10, n = 20;
printf("m = %d\n", m); printf("n = %d\n\n", n); After Swapping:
swap(&m, &n);
m = 20
printf("After Swapping:\n\n");
n = 10
printf("m = %d\n", m);
Process returned 0 (0x0) execution
printf("n = %d", n);
time : 1.767 s
return 0; Press any key to continue.
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
❖ POINTER AND ARRAY
array
a b c
Ptr*
EXAMPLE:
#include <stdio.h> OUTPUT:
int main() Address of charArr[0] =
{ 6356744
char charArr[4]; Address of charArr[1] =
6356745
int i; Address of charArr[2] =
for(i = 0; i < 4; ++i) 6356746
{ Address of charArr[3] =
6356747
printf(“Address of charArr[%d] = %u\n”, I ,
&charArr[i]); Process returned 0 (0x0)
} execution time : 15.972 s
Press any key to continue.
return 0;
}
❖ POINTERS AND STRUCTURES
• Structures can be created and accessed
using pointers. A pointer variable of a
structure can be created as below:
struct name { member1; member2; . . };
int main()
{
struct name *ptr;
}