#define MAX_SIZE 16 ... int list[MAX_SIZE + 1];
int a[3]; would create three int elements,
addressable as a[0], a[1], and a[2]
a[3], there
is no element named a[3]
int a[3] = {5, -2, 17};
int q[] = {1, 2, 3}; is the same asint q[3] = {1, 2, 3};
extern int a[];
char is used to represent
a character string, the end of which is marked by a byte set to 0
(also known as a NUL character)
int str1[] = {'a', 'b', 'c', '\0'};
int str2[] = "abc";
int str[3] = "abc";
which is the same as int str[3] = {'a', 'b', 'c'};
(omitting the final NUL character)
m[3][2] is stored in consecutive
memory locations as m[0][0], m[0][1], m[1][0], m[1][1], m[2][0], m[2][1]
& operator returns the address of its argument
* operator dereferences its argument to
access the object pointer to by the pointer
int i, j; int *p; /* pointer to `int' */ i = 6; p = &i; /* set `p' to address of `i' */ j = *p; /* set `j' to 6 (value of `i') */ *p = 5; /* set `i' to 5 */
<stdio.h> or
<stdlib.h>)
if (!ptr) statement will cause
statement to be evaluated if ptr is a null pointer
since if (ptr) is the same as if (ptr == 0)
and since the 0 is converted to a null pointer in a pointer
context, the code is implicitly comparing ptr against a
null pointer
if (ptr) statement will evaluate
statement if ptr is not a null pointer
consts like const int a;)
since the value 3 doesn't have a permanent memory address
register variables since registers are not in memory
and thus do not have a memory address
(8 * k), which are roughly the same
as constants in that they don't have a permanent memory address
int
identical(void)
{
int a[3] = {6, 3, 7};
int *p = &a[0]; /* point `p' at the first element of `a' */
if (a[0] == p[0] && a[1] == p[1] && a[2] == p[2])
return 1;
else
return 0;
}
would return 1, since p[n] points to
a[n] for all valid values of n
a
and a pointer p which points to the first element of a
:
a is an address,
p is an address which holds the address of a
a[n],
the computer starts at address a, moves n past
it and fetches value from there;
to retrieve value from p[n],
the computer starts at address p, fetches the address stored
there and adds n to it, then fetches value from
the resulting address
sizeof operator
sizeof(array) returns the amount of memory used by all
elements in array
sizeof(pointer) only returns the amount of memory used by
the pointer variable itself
& operator
&array is an alias for &array[0] and returns
the address of the first element in array
&pointer returns the address of pointer
char array[] = "abc" sets the first four elements in
array to 'a', 'b', 'c', and '\0'
char *pointer = "abc" sets pointer to the
address of the "abc" string (which may be stored in read-only memory
and thus unchangeable)
ptr++ increments ptr by the appropriate
amount to reach the next entry of ptr's datatype
ptr points to address 0xff00:
ptr is a char, ptr++ will
set ptr to 0xff01
ptr is an int (and an int is
4 bytes long), ptr++ will set ptr to 0xff04
ptr1 is pointing at a[0] and
ptr2 is pointing at a[3], then
ptr2 - ptr1 returns the integer value 3, no matter what
the datatype of a is
a and b are different arrays, then
given ptr1 = &a[0] and ptr2 = &b[0],
it is impossible to predict whether ptr1 < ptr2 or
ptr1 > ptr2 (though ptr1 != ptr2 will always
be true)