Professional Documents
Culture Documents
ARRAYS IN C
Array – is defined as a finite set of elements having the same type referenced under a common
name.
Declaration of Array in
C – int array[10];
Java – int [] array = new int[10];
ARRAY REPRESENTATION
Homogenous – each element same size – s bytes
VARIABLE-LENGTH ARRAYS
MEMORY ADDRESSES
(Note: arrays are combined memory locations)
Storage cells are typically viewed as being byte-sized
RECALL:
Usually, the smallest addressable unit of memory (byte-sized). Few machines can
directly address bits individually.
Such addresses are sometimes called byte-addresses.
Memory is often accessed as words (data type)
Usually, a word is the largest unit of memory access by a single machine instruction
o CLEAR’s word size is 8 bytes (=sizeof(long))
A word-address is simply the byte-address of the word’s first byte
MEMORY ADDRESS CALCULATION IN AN ARRAY
Array – is a collection of items stored at contiguous memory locations.
POINTERS
Special case of bounded-size natural numbers
Maximum memory limited by processor word-size
32 64
2 bytes = 4GB, 2 bytes = 16 exabytes
A pointer (a basic type in c) is just another kind of value
int *ptr; (the variable “ptr” stores a pointer to an “int”)
POINTER OPERATIONS IN C
Creation
TLDR:
int *int_ptr1 = &value;
int *int_ptr2;
int intVar = 2;
int_ptr1 = 3 INVALID
int_ptr1 = intVar INVALID
int_ptr1 = *another_ptr INVALID
int_ptr1 = &address VALID
int_ptr1 = int_ptr2 VALID
*int_ptr1 = 3 VALID
*int_ptr1 = intVar VALID
*int_ptr1 = *another_ptr VALID
*int_ptr1 = &address INVALID
*int_ptr1 = int_ptr2 INVALID
int *int_ptr1 = &address; VALID
POINTERS ARITHMETIC
Adds 1*sizeof(data) to the memory address
NULL
GENERIC POINTERS
void * - pointer to anything
PASS-BY-REFERENCE
ARRAY AND POINTERS
Array name – a pointer to the initial (0th) array element