Basic Usage
- Hold multiple values all of the same base type
- Use indices to access individual elements; like most languages these days, indices start at
0
- Declare as
type var[CONST_SIZE]
, e.g.int temps[3]
- Can also declare w/initialization list:
type var[] = {val1, val2, val3}
, e.g.int temps[] = {75, 67, 83}
and array will be as long as the list
Multiple dimension arrays
- Two or more dimensions; the size of each must be specified
- Declare as
myarray[size1][size2]
, access individual element withmyarray[i][j]
- Initialize by nesting lists:
int testr[2][3] = { {4, 3, 2}, {6, 8, 33} };
- Does initialization one row at a time
myarray[i]
refers to a one dimensional array of the base type (row i);myarray[size1][size2]
can be used as an array of arrays
Example
#include <stdio.h>
int main (void) {
char a[2][4] = { {'w','o','w','\0'}, {'h','i','\0','X'} };
int i, j;
for (i = 0; i < 2; i++)
for (j = 0; j < 4; j++)
printf("a[%d][%d] is %c\n", i, j, a[i][j]);
for (i = 0; i < 2; i++)
printf("row a[%d] is %s\n", i, a[i]); // a[i] is a char array (i.e. a string)
return 0;
}
Arrays and Functions
Use []
to indicate an array variable in function definition and in function prototype, e.g.
void doArray (int [], int); /* prototype version */
void doArray (int myarray[], int size) /* on function itself */
- Pass whole array to function with just name, no subscript: aray contents are passed by REFERENCE
- Element values may be changed permanently within the function
- Doesn't create new storage location (ie, doesn't copy array)
- Array name is really address of first element (more later on this)
- Array names are passed by VALUE (copied, changes won't reach caller)
- Individual array elements passed to functions are handled like any variable of that base type
If declaring a multi-dimensional array as function argument, need sizes of subcripts for all dimensions but 1st
<ul>
<li>eg: void passarray(int a[][3][4])
receives a 3 dimensional array
</li> <li>Sizes needed because arrays are stored sequentially - need dimensions to locate items
</li> <li>Another example of low-level nature of C data structures.
</li></ul>
Can use const
in parameter list to make array unmodifiable: void doArray (const int ma[])
results in compile error if any attempt to assign or read new value into an array element
Command line options
To read what arguments if any were on a command line invocation, use the following main
function declaration:
main(int argc, char *argv[])
argc
is count of how many arguments, including program name-
char *argv[]
is a "pointer to an array of characters". We will explain pointers in detail later. For now, you can use it likechar argv[][]
- a 2d array of chars -
argv[0]
is program name (string) argv[i]
isi
th argument as a string
Example: given ./a.out one two
argc
is 3,
argv[0]
is “./a.out”, argv[1]
is “one”,
and argv[2]
is “two”
CAUTION: review the below sections after learning pointers
Pointers and arrays
Welcome to C’s Wacky World
- A declared array, say
int a[10]
, is "really" just an address that starts a block of memory. - So, writing
a
is generally the same as writing&a[0]
a[3]
is a synonym for*(a + 3)
(offset three from pointer to start of array)&a[3]
is a synonym fora + 3
int *ptr
and int a[10]
variables are
often interchangeable but not 100%:
a = ptr
will be a compile error since size ofa
could get changed if this were allowed;sizeof(a)
is always fixedptr = a
is perfectly acceptable, butprintf("%d", sizeof(ptr));
is only 4 or 8, the size of a pointer- locally declared array types (but not function parameters) have allocated space the size of the whole array:
char s[100]; printf("%d",sizeof(s));
prints 100 (bytes)
char s[10]; printf("%d",sizeof(&s[0]));
prints the size of a pointer, 4 or 8 bytes
Arrays of pointers
- Note writing
main(int argc, char *argv[])
is same asmain(int argc, char **argv)
since more generally any function parametermytype myvar[]
is nearly identical to writingmytype * myvar
. - Common use is array of strings
- Each string is pointer to a string array,
eg:
char *suitptr[4] = {"Hearts", "Diamonds", "Clubs", "Spades"}
- Note that this is not the same as a 2d array which is laid out sequentially in memory