Академический Документы
Профессиональный Документы
Культура Документы
Arrays
Arrays
An array is a sequence (in memory) of like items.
Arrays are NOT data types.
Arrays must be declared before they are used.
General form:
type variable-name[number_of_elements];
The array size must be explicit at compile time needed to
reserve memory space
Array elements individually accessed with index.
General form:
variable-name[index];
Zero based subscripts
No compile-time or run-time limit checking
Initialization of Arrays
Elements can be initialized when they are declared in the
same way as local and global variables.
type array_name[size] = { list of values separated by commas };
int number[3] = {0, 0, 0};
Unspecified elements will be set to zero automatically.
Array declarations may omit the size.
int counter[] = {1, 1, 1, 1};
char RGB_image[][3] = {{255,0,255}, {0,255,0}};
Arays must be statically declared.
void SomeFunction(int num_elements)
{
int temp[num_elements]; // error
}
C Strings
A C string is an array of characters, terminated with a zero byte.
C strings can be initialized when defined:
char outputString[] = "Cow";
same as:
outputString[0] = 'C'; Compiler computes
outputString[1] = 'o'; the size of the array
outputString[2] = 'w';
outputString[3] = 0;
(3 + 1 = 4 bytes)
#define MAX_NUMS 5
int average(int values[]) 0x05e8
{ SP values 0x05ea 0x05f2
int i, sum = 0; i 0x05ec 5
for (i = 0; i < MAX_NUMS; i++) sum 0x05ee 15
sum = sum + values[i];
0x05f0 Return Adr
return (sum / MAX_NUMS);
} SP nums[0] 0x05f2 1
nums[1] 0x05f4 2
int main() nums[2] 0x05f6 3
{ nums[3] 0x05f8 4
int nums[MAX_NUMS] =
{ 1, 2, 3, 4, 5 }; nums[4] 0x05fa 5
int mean = average(nums); mean 0x05fc 3
return 0; 0x05fe Return Adr
} 0x0600
} 0x05f0
a 0x05f2 4
b 0x05f4 3
void swap(int a, int b) swap
temp 0x05f6 3
{ 0x05f8 Return Adr
int temp = a; a 0x05fa 3
a = b; main b 0x05fc 4
Pointer Operators
A pointer variable is declared with the asterisk operator (*)
type *var; // same - whitespace doesnt matter
type* var;
Dereferencing any expression returns a value
*var returns contents of the memory location
pointed to by var
**var returns contents of the memory location
pointed to by the memory location pointed
to by var
*3 returns the contents of memory location 3
A pointer is created with the reference operator (&)
&var
Reference must be applied to a memory object
&3 is illegal as it would return a pointer to a constant
Examples of Pointers
0x05ea
int *ptr1;
0x05ec
int *ptr2; 0x05ee
int **ptr3; 0x05f0
int i = 4; 0x05f2
int j; ptr1 0x05f4 0x05fa
ptr1 = &i; ptr2 0x05f6 0x05fc
ptr2 = &j; ptr3 0x05f8 0x05f4
ptr3 = &ptr1; i 0x05fa 4
j 0x05fc 4
0x05fe Return Adr
printf("%04x", ptr1); 05fa 0x0600
printf("%04x", ptr2); 05fc
printf("%04x", *ptr1); 0004
printf("%04x", *ptr2); ????
j = *ptr1;
printf("%04x", j); 0004
printf("%04x %04x", *ptr3, **ptr3); 05fa 0004
int a = 3; 0x05ee
int b = 4; 0x05f0
a 0x05f2 0x05fa
swap(&a, &b);
b 0x05f4 0x05fc
} swap
temp 0x05f6 3
0x05f8 Return Adr
void swap(int* a, int* b) a 0x05fa 3 4
{ main b 0x05fc 4 3
int temp = *a; 0x05fe Return Adr
0x0600
*a = *b;
*b = temp;
}
Null Pointers
Sometimes we want a pointer that points to nothing.
Used for invalid pointer error returns
Used to catch errors
NULL is a predefined macro that contains a value that
non-null pointer should never hold, usually NULL=0.
int *p;
p = NULL; // p is a null pointer
Pointer Arithmetic
Address calculations depend on size of elements
ints are 16-bits or 2 bytes per element.
e.g., to find 4th element (x[3]), we add 3*2 to base address
If double, we'd have to add 12 (3*4) to find address of 4th
element.
C does size calculations under the covers,
depending on size of item being pointed to:
double x[10]; Allocates 40 bytes
(4 per element)
double *y = x;
*(y + 3) = 13;
Same as x[3]
y++; (base address plus 12)
*y = 3.1415926;
x[1] = 3.1415926
String Parsing
1. What is the value of X?
char str[] = "Point X = 100";
Walk the array until token
or NULL terminator.
2. Find token: (Token must be unique)
char* ptr = str;
int number = 0;
while (*ptr && (*ptr++ != 'X'));
4. Convert to decimal:
isdigit returns 0 or 1
while (isdigit(*ptr))
number = number * 10 + (*ptr++ - '0');