Arrays in C
Arrays
An array is a group of items of same type having a common name. These items are called elements of the array. Elements have a subscript which is integer.
In C, an elements of an array are stored in continuous memory locations.
Defining an array:
An array is defined using the syntax
datatype arrayname[size];
e.g.
int a[10];
char b[5];
float c[4];
First line defines an array a of 10 integers and second line defines a character array b of 5 characters and third line defines array c of 4 float values.
Elements of the array can be any valid C data type.
See Note 1:
Accessing elements of array
An array elements are accessed using indices. Array index always starts with 0 and continues till size-1.
#include int main() { int a[5]; int i; for( i=0;i<5;i++) a[i]=i; for(i=0;i<5;i++) printf("%d\n",a[i]); return 0; } |
int arr[];//error. array size not defined
int b[5];
b=0;//error. array should be accessed using index
float a[4];
for(i=0;i<=4;i++)
scanf("%f",&a[i]);//error . last subscript is 3 not 4
Last error is more dangerous because compiler does not produce error. At run time, the program will take some garbage value. a[4] may over write some other variable.
Initialization of array elements
Like all other variables, uninitialized array elements will have a garbage value , not zero. To initialize all elements of array, the initializers must be given along with definition of the array.
e.g.:
float num[4]={1.2,4.4,-5.6,5.8};
The values of the elements are enclosed in braces and separated using comma.
If an array has initializers, then and only then array size need not be mentioned.
double d[]={3.4,5.7};//correct. Size of d is 2
The initial values can be less than size in which case remaining elements are zero filled
float num[4]={9.9}//num[0]=9.9
//num[1], num[2] and num[3] are 0.0
Arrays and pointers :
Pointer is a variable which stores address of another variable. Arrays and pointers in C are closely related.
1. Array can be used as a pointer
int a[5]={1,2,3,4,5};
ptr = a;
*ptr=200;
ptr = &a[4];
b=*ptr;
In line 3 ptr is assigned to a.
Name of the array = base address of the array
So ptr is assigned to address of a[0].
Line 4 assigns 200 *ptr which makes a[0]=200
Line 5 assigns ptr to address of a[4].
Line 6 makes b as 5 because *ptr is now 5
2. Pointer increments to point at next element of array
int *ptr;
int a[4]={10,20,30,4};
for(i=0;i<4;i++)
{
printf("%d ",*ptr);
ptr++;
}
The program prints 10 20 30 40
This happens because when ptr +1 = ptr + 1*sizeofPointee That is for int, ptr increments changes the address by 4, for char pointer it gets incremented by 1 and for double pointer, it gets incremented by 8.
But that is what we want, because our next array element is stored after 4 locations as one int occupies 4 bytes(in a 32 bit machine) So ptr increment will automagically take us to next element in the array
2. Pointer increments to point at next element of array
int *ptr;
int a[4]={10,20,30,4};
for(i=0;i<4;i++)
{
printf("%d ",*ptr);
ptr++;
}
The program prints 10 20 30 40
This happens because when ptr +1 = ptr + 1*sizeofPointee That is for int, ptr increments changes the address by 4, for char pointer it gets incremented by 1 and for double pointer, it gets incremented by 8.
But that is what we want, because our next array element is stored after 4 locations as one int occupies 4 bytes(in a 32 bit machine) So ptr increment will automagically take us to next element in the array
Arrays of arrays ( 2D arrays): An array with rows and columns is called 2 dimensional array. In such an array, the elements have row index and column index. An mXn array , having m rows and n columns, has mXn elements
Syntax of 2D array defintion
data-type nameofarray[numRows][numCols];
Both number of rows and number of columns must be specified.
To access elements of 2D array, the two indices must be given in separate pairs of square brackets.
int i,j,mat[3][2];//3 rows and 2 columns
mat[0][0]=10;
for(i=0;i<3;i++)
for (j=0;j<2;j++)
scanf("%d",&mat[i][j]);
Initialization of 2D arrays :
e.g.:
int arr[3][2]={{1,2},{1,4},{1,5}};
int b[2][3]={1,2,3,4,5,6};
int c[3][3]={{1},{2,3},{4}};
//c[0][1]=c[0]c[2]=c[1][2]=0
int d[][2]={1,2,3,4,5,6};//d[3][2]
A two dimensional is not same as pointer to pointer
But it can be treated as pointer to array
But it can be treated as pointer to array
Strings: In C string is a character array which is terminated by null character i.e. character whose integer code is 0.
String Defintion
char string-name[size];
String Initialization
char string-name[size]="string value";
You define a string as character array with some size. Once it is defined you can either use it as one string or individual characters which are array elements.
#include<stdio.h> int main() { char str[10]; printf("Enter a string"); scanf("%s",str); for(i=0;i<strlen(str);i++) printf("str[%d]=%c\n",i,str[i]); return 0; } | Output str[0]=h str[1]=e str[2]=l str[3]=l str[4]=o |
In the program above string str is defined as an array of 10 chars. It is read using scanf with %s format specifier. scanf reads characters from keyboard until you enter a white space and stores these in the array. If you type hello the elements of the array will be str[0] is 'h', str[1] is 'e', str[2] is 'l', str[3] is 'l', str[4] is 'o' and str[5] is '\0', the famous null character. It is important that
Every string must end with a null character
For loop prints these characters and stops at strlen. strlen is a library function which returns the length of string till null character, in this example 5.
When you initialize a string as str[10]="program", strlen will be 7 and str[7] will be null character.
Now let us do some programming activity
Write a program to read a line and replace every 'a' with 'z'
Note 1: You can not have array of void or array of functions.
2. But you can have array of function pointers
Comments
Post a Comment