Adobe Scan Nov 15, 2024
Adobe Scan Nov 15, 2024
declaration "/
tt marks[ 30): * array
fur (i =0;1<= 29;it+)
Array Initialization
Look at the following array declarations:
int run{6l=(2,4, 12, 5, 45, 5 ):
nt nË=12,4, 12, 5, 45, 5);
Aoat pressi]={12.3, 34.2, -23.4, -11.3 );
iong int gdp[ 10);
This shows that arrays can be initialized while declaring them. When we
do so, mentioning the dimension of the array is optional, as in arrays n[ ]
and press[]above. Also, note that the array gdp[ ] has auto storage
dass and it has not been initialized, so it contains garbage values. If we
declare it as astatic array,all its elements would be set to 0.
Array Elements in Memory
Consider the following array declaration:
12 -45 23 346 77 90
34 66
int i;
int marks[]={55,65, 75, 56, 78, 78, 90 }:
for (i =0; i<= 6; it+ )
displayl ( marks[ i ]) ;
for (i=0 ;i<=6; it+)
display2 (&marks[ i ] ) ;
return 0;
78 go
55 65 75 5678
Here, to display1() we are passing value of an array elerment, whereas
to display2() we are passing address of an array elernent. Since at a
collected
time only one element or its address is being passed, they are
in an integer variable m, or an integer pointer n respectively. Since n
contains the address of array element, to print out the array elernent,
we are usingthe 'value at address'' operator (")
Pointers and Arrays
us first learn
Tobe able to see what pointers have to do with arrays, let
some pointer arithmetic. Consider the folowing example:
#incude <stdio.h
int main)
int i 3, "x;
float j= 1.5, "y:
char k ='c, *z
printf ("Value ofi= %d\n",i);
printf ( "Value of j= %f\n",j):
printf ("Value of k= %c\n", k):
x=&i; y= &j; z =&k;
printf ("Original address inx= %u\n'", x);
printf ("Originaladdress in y= %u\n", y):
printf ("Original address in z = %u\n",z):
retürn 0;
24 34 12 44 56 17
int i;
for (i =0;i<=n-1;it+)
Flexible Arrays
we do not know the size of an array at the time of writing the
gam, we can receve t during execution, This is shown tbelow.
scant{"%".Amax ):
This makes the array more flexible, as we do not have to commit to its
tthe tine ef writing the program, This feature has been added in
and woutd be rejected by older complers, as they expect aray
dimension to be apositive non tero integer constànt.
The evariable sized arrays can also be created by using astandard library
hunction malloc ). The following program shows how to use it.
Rexitble artay
$nude <stdio > size
228
includeet
eturn 0:
nt nan, p,i
p=tun {&nax)
for(i=0,1<mo i )
printí ("%d", plj}.
return 0;
int fun{ int *num }
There are a few important points that you should note about the
program:
ia) arr has been declared as static to ensure that it remains alive when
control returns from fun( ) and we can access it in main( .
(b) When we attempt to return the array arr from fun( ) what gets
returned is onlyits base address. Hence return type of fun( ) is int *
ic) Since a function can return only one value, the address of array is
returned explicitly through return statement, whereas the array
size is returned through num using a call by reference.
catein (d) The number of elements in the array is obtained by dividing the
ebase array size bysize of its 0th element.
onvert
Since
sions
P</>Programs
target
Problem 13.1
top,
Wite a program that interchanges elements at odd position with
elements at even position in an array of 10 elements.
eturned Program