Arrays & Strings
Arrays & Strings
Arrays & Strings
Int i[10];
i[0]=101;
i[1]=102;
i[2]=103;
i[9]=110;
2
Declaration of arrays
Like any variable you have to declare an array before use.
Syntax:
Datatype variable-name[size];
ex- float grade[50];
As individual array element can be used anywhere with a
statement such as
g=grade[6];
for(sum=0;i=0;i<50;i++)
sum=sum+grades[i];
5
Inialization of arrays
Type array_name [size]={list of values};
Ex--int number[3]={0,1,2};
Float number [5]={0.0,3.7,8.9};
It will initilize only first 3 elements and rest will be at to zero.
In declaration of an array,the size may be omitted,in such cases
the compiler allocates enough space for all initialized elements.
int number[]={1,1,1,1};
Int number[3]={10,20,30,40};
6
Drawbacks
1.There is no convenient way to initialize only selected
elements.in case we know abt the index of a particular
element only then we can initialize that element acc.to
own choice.
2.There is no shortcut method to initialize a large number
of elements.
13
14
15
16
int *ptr;
Ptr=(int *)malloc(10);
We can use SIZE OF operator to make program more readable
ptr=(int *)malloc(5*sizeof(int));
It allocates memory space to hold 5 integers value.
If there is not sufficient memory then malloc return NULL.
ptr=(float *)malloc(10 * sizeof(float));
if(ptr= =NULL)
printf(sufficient memory not available);
Unlike variables and arrays,allocated memory has no name
associated with it so it can be accessed only THRO
POINTERS.
17
main()
{
int *p,n,i;
printf(enter no.of integers to be entered);
scanf(%d,&n);
p=(int *)malloc(n * sizeof(int));
if(p= =NULL)
{
printf(memory not available);
exit(0);
}
for(i=0;i<n;i++)
{
printf(enter the integer);
scanf(%d,p+i);
}
for(i=0;i<n;i++)
{
printf(%d,*(p+i));
}
}
18
.
19
20
21
22
main()
{
int I ,*ptr;
ptr=(int *)malloc(5* sizeof(int));
if(ptr= =NULL)
{
printf(memory not available);
exit(0);
}
printf(enter 5 integers);
for(i=0;i<5;i++)
scanf(%d,ptr+i);
ptr=(int *)realloc(ptr, 9* sizeof(int));
if(ptr= =NULL)
{
printf(memory not available);
exit(0);
}
printf(enter more4 integer);
for(i=5;i<9;i++)
{
scanf(%d,ptr+i);
}
for(i=0;i<9;i++)
{
printf(%d,*(ptr+i));
}
}
23
func()
{
space for 10 int will reserve
int *ptr;
ptr=(int*)malloc(10* size of(int));
.
}
All local variable vanish when function terminates and since ptr
is a local variable so it will deallocated at the termination of
function.
But space allocated dynamically is not deallocated automatically
so that space remains there leading to memory leak.
We should free memory space by putting a call to free( )at the
end of function.
It is valid to return a pointer to dynamically allocated memory
because the space allocated dynamically is not released after
termination of function.
25
26
2-D arrays
int i,j,a[3][3];
printf(enter matrix);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(%d,&a[i][j]);
}
}
printf(the entered matrix);
for(i=0;i<3;i++)
{
for(j=0;j<3j++)
{
printf(%d,a[i][j]);
}
}
Col 0
[0][0]
row 0
310
[1][0]
row1
100
789
col 2
[0][2]
276
[1][1]
[1][2]
456
[2][0]
row2
col 1
[0][1]
567
780
[2][1]
456
[2][2]
123
27
28
value
a[0]
a[1]
a[2]
address
a+0
a+1
a+2
value
*a
*(a+1)
*(a+2)
30
31
Array of pointers
int *a[2][5];
It can be explained in terms of pointers as:int (*a)[5]; //[ ] has higher precedence than *
a[0] points to 1st element in 0th row.
a[1] points to 1st element in 1st row.
a[2] points to 1st element in the 2nd row.
a([2]+3) points to 3rd element in 2nd row.
*(a[2]+3) points to valus of 3rd element in 2nd row.
32
main()
{
int arr[4][2]={ {156,90},
{456,67},
{134,45},
{890,32} };
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<2;j++)
{
printf(%d, *(*(arr+i)+j));
}
}
35
37
38
Array of pointers
main()
{
int *arrp[3];
int arr[3]={5,10,15},i;
for(i=0;i<3;i++)
arrp[i]=arr+i;
printf(address and element);
for(i=0;i<3;i++)
{
printf(%u,arrp[i]);
printf(%d,*(arrp+i));
}
}
Output : 5 10 15
39
/*calling function*/
/*function definition*/
40
Pointer to function
main()
{
int mul( ),(*p)( ),x,y,z;
sacnf(%d%d,&x,&y);
p=&mul;
z=(*p)(x,y);
printf(%d,z);
getch();
}
int mul(int p,int q)
{
int m=p*q;
return(m);
}
41
STRING
String is collection of Characters (or ) group of elements in which
symbols enclosed within quotation marks.
String is always declared as character arrays.
in other words character arrays are called as STRINGS.
Every string is terminated with \0( NULL)character.
char name[ ]={I,n,d,I,a,\0};
42
interpretation
Arrays whose elements are characters called as string
/* size? */
43
String Initialization
Initialization
char m[9] = I like C;
char m[ ] = I like C;
char m[ ] = { I, , l, i, k, e, ,C };
char m[ ] ={ { I },{ l },{ i },{ k },{ e },{ u } };
44
main()
{
char name1[9] = I like C;
char name2[9 ] ={ { I },{ l },{ i },{ k },{ e },{ u } };
char name3[9 ] = { I, , l, i, k, e, ,c,\0 };
clrscr();
printf(name1=%s,name1);
printf(name1=%s,name1);
printf(name1=%s,name1);
}
45
Strlen function
It counts the number of characters in a given string.
49
Strcpy function
This function copies the contents of 1 string to another.
strcpy(s2,s1);
S1 =source string
S2 =destination string
S1 is copied to s2.
51
main()
{
char s1[10],s2[10];
int i;
printf(enter string);
gets(s1);
for(i=0;i<10;i++)
s2[i]=s1[i];
printf(first string,s1);
printf(second string,s2);
}
52
Stricmp function
This function compares 2 strings.it compares 2 strings
without knowing upper case and lower case.if strings
are same then it returns to 0 otherwise non-zero value.
diff=stricmp(str1,str2);
if(diff= =0)
puts(strings are equal);
else
puts(strings are not equal);
54
Strcmp function
This function compares 2 strings.it compares 2 strings
and also check the upper case and lower case. if strings
are same then it returns to 0 otherwise non-zero value
diff=strcmp(str1,str2);
if(diff= =0)
puts(strings are equal);
else
puts(strings are not equal);
55
String Constants
String constants are different to all other constant types (e.g.,
character, float, short, enum, int, etc).
All other constants are not allocated memory, and do not have an
address. That is, they are not variables.
double *pval1 = 9.6;
/* Invalid. Won't compile. */
double *pval2 = &9.6;
/* Invalid. Won't compile. */
int *parray = { 1, 2, 3 }; /* Invalid. Won't compile. */
Memory has static extent. Can return a pointer to a string constant from a function.
char * getHello()
/* Return a pointer to an array defined within
the function */
{
char *phello = "Hello World\n";
return phello;
}
String constant expression defines a pointer to the beginning of constant array. Can
index string constant directly. Show print_base_b().
58
String constants may be concatenated at compile time. This is useful for writing a long
string as the following is illegal.
"this is
a string"
For this \ form, the second line must appear on the first column, else the
intervening white-space will be added to the string.
The string concatenation form is preferred over the \ form.
59
Character arrays
A character array may be initialised with a string constant.
char string[] = a string;
Summary
The following are a selection of valid array
operations.
short val = 9;
short *pval = &val;
double array[] = { 1.0, 2.0, 3.0 };
double *parray = array;
char str[] = "Hello World!\n"; /* readwrite. */
str[1] = 'u';
61
isupper(int
islower(int
isalpha(int
isdigit(int
isalnum(int
isprint(int
isgraph(int
ispunct(int
isspace(int
c)
c)
c)
c)
c)
c)
c)
c)
c)
int toupper(int c)
int tolower(int c)
63
Arrays of Pointers
Pointers are themselves variables. Can have an array of
pointers.
int *parray[N];
a1[] = { 1, 2, 3, 4 };
a2[] = { 5, 6, 7 };
*pa[] = { a1, a2 };
**pp = pa;
*p = pa[1];
val;
val
val
val
val
val
val
=
=
=
=
=
=
pa[1][1];
pp[1][1];
*(pa[1] +
*(pp[1] +
*(*(pp+1)
p[1];
/* equivalent operations */
1);
1);
+ 1));
68
Multi-dimensional Arrays
C provides rectangular multiple-dimensional arrays.
These are not to be confused with arrays of pointers to arrays.
Multi-dimensional arrays are defined by multiple square
brackets [][].
float
{
{
{
};
matrix[3][4] =
2.4, 8.7, 9.5,
6.2, 4.8, 5.1,
7.2, 1.6, 4.4,
{
2.3 },
8.9 },
3.6 }
The initialisation list for the array consists of nested braces for
each row of the array.
70
71
Higher Dimensions
M-D arrays can be higher than just 2-D.
short
{
{
{
{
};
array3d[4][2][3] = {
{ 0, 1, 2 }, { 3, 4, 5 }
{ 6, 7, 8 }, { 9, 10, 11 }
{ 12, 13, 14 }, { 15, 16, 17 }
{ 18, 19, 20 }, { 21, 22, 23 }
},
},
},
}
a[] = { 1, 2, 3 };
b[] = { 4, 5, 6 };
*c[] = { a, b };
d[][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
M-D arrays:
1.
2.
3.
Pointer arrays:
1.
2.
3.