PPT05-Pointers and Array
PPT05-Pointers and Array
TOPIC 05
• Pointer
• Pointer Constant & Pointer Variable
• Array
• String Manipulation
POINTER DEFINITION
o ptr = B; analogous with ptr = &B[0]; B is a pointer constant pointing to the first element of an
array.
POINTER CONSTANT & POINTER
VARIABLE
o Pointer constant can only be initialized at definition time
o Example:
int Arr1[10];
Arr1[10] = {1, 2, 3, 4, 5}; // error
Arr1 = {1, 2, 3, 4, 5}; // error
Arr1[10] = 12; // error max 9
Arr1[0] = 23; // ok
o Array characteristics:
• Homogenous
All elements have similar data type
• Random Access
Each element can be reached individually, does not have to be sequential
ARRAY DEFINITION
o Syntax:
type array_value [value_dim];
o Example :
int A[10];
o An illustration of array 1D
o Elements of an array indexed starting from 0
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
ARRAY INITIALIZATION
• Example:
int B[8]={1, 2, -4, 8};
ARRAY INITIALIZATION
int B[5];
Error, why ?
B[5]={0,0,0,0,0};
ACCESSING ARRAYS
27 15
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
12 27 15
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
ACCESSING ARRAYS
o Example:
#include <stdio.h>
void main()
{
int i;
int list_int[10];
for (i=0; i<10; i++){
list_int[i] = i + 1;
printf( "list_int[%d] init with %d.\n", i,
list_int[i]);
}
}
ONE DIMENSIONAL ARRAY
o C compiler does not limit number of dimensional which can be created. Our PC memory does.
o Example Array 1D:
#include<stdio.h>
int SIZE = 5;
void main() {
int i, j;
int n[SIZE] = {15, 9, 1, 7, 5};
for( i=0 ; i<= SIZE ; i++) {
printf("%5d ", n[i]);
for ( j=1; j<=n[i] ; j++)
printf("%c","*");
printf("\n");
}
}
TWO DIMENSIONAL ARRAY
o Syntax 2D Array:
type name_array[row][col];
o Example:
int a[3][4];
Column 0 Column 1 Column 2 Column 3
Row 0 a[ 0 ][ 0 ] a[ 0 ][ 1 ] a[ 0 ][ 2 ] a[ 0 ][ 3 ]
Row 1 a[ 1 ][ 0 ] a[ 1 ][ 1 ] a[ 1 ][ 2 ] a[ 1 ][ 3 ]
Row 2 a[ 2 ][ 0 ] a[ 2 ][ 1 ] a[ 2 ][ 2 ] a[ 2 ][ 3 ]
Column subscript
Array name
Row subscript
TWO DIMENSIONAL ARRAY
o Initialization:
using rmo (row major order)
o Example:
int b[2][2] = {1, 2, 3, 4 }; 1 2
int b[2][2] = { { 1, 2 }, { 3, 4 } }; 3 4
1 0
int b[2][2] = { { 1 }, { 3, 4 } };
3 4
o Syntax 3D Array :
type name_array[row][col][depth];
o Example:
int x[3][2][4] = {{{1,2,3,4}, {5,6,7,8}},
{{11,12,13,14}, {15,16,17,18}},
{{21,22,23,24}, {25,26,27,28}}
};
void main() {
int x[4][3][5] = {{{1, 2, 3}, {0, 4, 3, 4}, {1, 2}},
{{9, 7, 5}, {5, 7, 2}, {9}},
{{3, 3, 5}, {2, 8, 9, 9}, {1, 2, 1}},
{{0}, {1}, {0, 1, 9}}
};
printf(“%5d”, x[2][1][3]);
}
ARRAY OF POINTER
Output : 1 2 3 5
ARRAY OF CHARACTER
o Syntax:
char array_name[value_dim];
o Example:
char name[40];
char ss[20]={‘B’,’I’,’N’,’U’,’S’}; //20 elements
char ss[ ]= {‘B’,’I’,’N’,’U’,’S’}; // 5 elements
STRING
o String is an array of character that ended with null character ( ‘\0’ or in ASCII = 0)
o String constant or string literal is some characters written between double quote
• Example: ”Welcome to Binus”
o String constant type is pointer constant, thus can be assigned to an array of character :
• Example :
char name[40] = ”Amir”; //ok
name = ”Amir”; // error name is a constant pointer
Name[40]= “Amir”; //error
STRING
o Character in c written between single quote. Each uses one byte of computer memory
o Example:
char ch=’A’;
char ch=65; //Ascii Similar
char ch=0x41; //Ascii
o In Standard Library Function (header file string.h) provides functions to manipulate string:
• strlen()
Return a value of string length; excluded null char
• strcpy(s1,s2)
Copy s2 to s1
• strncpy(s1,s2,n)
Copy first n characters of s2 to s1
• strcat(s1,s2)
Adding string s2 to the end of string s1
• strncat(s1,s2,n)
Adding n characters of string s2 to the end of string s1
• strcmp(s1,s2)
Comparing the value of string s1 and s2, if similar returning 0
• etc.
PROGRAM EXAMPLES
strlen(“nana”); // 4
strcmp(“nana”, “nana”) // result 0
strcpy(s1,s2); // s1 = “xyz”, s2 = “xyz”
strncpy(s1,s2,2); // s1 = “xycdef”, s2 = “xyz”
strncpy(s1,s2,4); // if n>=strlen(s2) similar with
// strcpy() s1 = “xyz”
strcat(s1,s2); // s1=“abcdefxyz”, s2=“xyz”
strncat(s1,s2,2); // s1=“abcdefxy”, s2=“xyz”
s1 = “Happy”
s2 = “New Year”
o Paul J. Deitel & Harvey. Deitel. (2022). C how to program. 09. Pearson Education. Hoboken.
ISBN:978-0-13-739839-3. Chapter 7 & 8
o C Programming – Pointers: http://www.exforsys.com/tutorials/c-language/c-pointers.html
o https://www.cs.bu.edu/teaching/cpp/string/array-vs-ptr/
o http://www.programiz.com/c-programming/library-function/string.h
o http://www.tutorialspoint.com/c_standard_library/string_h.htm
o Storing Similar Data Items: http://aelinik.free.fr/c/ch12.htm