Professional Documents
Culture Documents
in
C
Needs
Needs
Subscripted Variables
Array Declaration
Array Declaration
printf(%d, numbers[2]);
Pgm contd
Numbers are
12
13
34
long number[4];
When you declare an array, you give the
compiler all the information it needs to allocate
the memory for the array. You tell it the type of
value, which will determine the number of bytes
that each element will require, and how many
printf("\n%p", &value);
To output the address of the third
element of an array called number, you
could write the following:
e.g.
optional, if ignored
size will automatically
be set to the number
of values supplied.
Number of
values may be
equal or less
to the size of
the array
char
exampl
text[5]={ a , b , c , d , e };
es
tex
t
char t[10] = { a , b , c , d , e };
t
a b
Unused
space
char s[ ] = { a , b , c , d , e };
s
\0
st
\0
char st[ ] = { c , o , m , p , \0 };
int num[10] ={ 12, 34, 56, 76, 8, 33 };
num 12 34 56 76
33
Unused
space
char st[ ] ={ a , b , c };
printf(%c%c%c, st[0], st[1],
st[2]);
Outpu
exampl
es
ab
c ] ={ a , b , c };
char st[
t
printf(%s, st );
Outpu
t
abc
Unwanted
%s in printf searches a nul
symbol
exampl
char st[ ] ={ a , b , c, \0 };
es
printf(%s, st );
ab
c ] = abc;
char st[
Outpu
t
printf(%s, st );
ab
c = "comp" , s[4] =
char st[4]
Outpu
t
"uter" ;
clrscr ();
printf ("%s" , s );
Outpu
t
uterco
[sizer];
0,1 0,2 0,3 1,0 1,1 1,2 1,3 2,0 2,1 2,2
Creates
an array of 3 x 4 = 12 consecutive
2,3
int numbers[3]
[5]
int numbers[2][3][4] = {
{ /* First block of 3 rows */
{ 10, 20, 30, 40 },
{ 15, 25, 35, 45 },
{ 47, 48, 49, 50 }
},
{ /* Second block of 3 rows */
{ 10, 20, 30, 40 },
{ 15, 25, 35, 45 },
{ 47, 48, 49, 50 }
}
};
a b c d \ x y z \
0
0
mn \
0
Unused
Unsized double
dimensional array