Pointers & Arrays
Pointers and Arrays
Pointer
Pointer is a variable that store the
address of another variable
Syntax :
<type> *ptr_name;
<type> *ptr_name;
Two
operators mostly used in pointer : * (content of) and & (address of)
Example:
Initialize an integer pointer into a data variable:
int i, *ptr;
ptr = &i;
To assign a new value to the variable pointed by the pointer:
*ptr = 5; /* means i=5 */
Initialize an integer pointer into a data variable:
int i, *ptr;
ptr = &i;
To assign a new value to the variable pointed by the pointer:
*ptr = 5; /* means i=5 */
Pointer to Pointer
Pointer to pointer is a variable that
saves another address of a pointer
Syntax:
<type>
**ptr_ptr
;
Example:
int
i, *ptr, **ptr_ptr;
ptr
= &i;
ptr_ptr
= &ptr;
To assign new value to i:
*ptr
= 5; // means i=5 ;
**ptr_ptr = 9; // means i=9; or *ptr=9;
Array
Data saved in a certain structure to be
accessed as a group or individually. Some variables saved
using the same name
distinguish by their index.
Array characteristics:
–Homogenous
All elements have similar data type
–Random Access
Each element can be reached individually, does
not have to be sequential
Accessing Arrays
Two analogous ways of accessing an
element i=2;
(A+2) or A[2]
A
is equivalent with &A[0] or a constant pointer to the first element
of particular array
To show A[2] on the monitor screen:
printf(“%d”,A[2]) or
printf(“%d\n”,*(A+2));
Pointer Constant & Pointer Variable
Pointer constant can only be initialized
at definition time
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
int Arr2[10] = {1, 2, 3, 4, 5};
//ok
One Dimensional Array
C compiler does not limit number of
dimensional which can be created. Our PC memory does.
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
Syntax
2D Array:
type name_array[row][col];
Three Dimensional Array
Syntax
3D Array :
type name_array[row][col][depth];
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 Character
Array
filled with character/s
Syntax:
char
array_name[value_dim];
Example:
char name[40];
char ss[20]={‘B’,’I’,’N’,’U’,’S’}; //20 elements
char ss[ ]= {‘B’,’I’,’N’,’U’,’S’}; // 5 elements
String
String is an array of character that
ended with null character (
‘\0’ or in ASCII = 0)
String
constant
or string literal
is
some characters written between double quote
-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
A Constant String can be linked at
compile-time:
”Hello,” ” world”
Similar to:
”Hello,
world”
Example string initialization:
char
s[ ] = ”BiNus”;
Similar to :
char s[ ] =
{’B’,’i’,’N’,’u’,’s’,’\0’};
String as a
data type does not known in C
Char vs String
Character
in c written between single quote.
Each uses one byte of computer memory
Example:
char
ch=’A’;
char
ch=65; //Ascii
char
ch=0x41; //Ascii
String
written in between double quote.
String Manipulation
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.
Comments
Post a Comment