You are on page 1of 11

UNIT II: Arrays and Pointers

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

 An array of m data values is a sequence of m x s bytes


Note: Kaya tinawag na homogenous ung array, kasi meron silang pare-parehas na SIZE, not
because of their TYPE.
Remember: Variables are memory locations, and when we create an array, we need to tell the
computer what size would be allocated for each element (data type).
m and s are not part of representation

 s known by compiler – usually irrelevant to programmer (size)


 m often known by compiler – if not, must be saved by programmer (data type)
Note: Proper declaration would be declaring char data type variables FIRST, before declaring int
ones.
ARRAY SIZES

sizeof – returns the size of an object in bytes


MULTI-DIMENSIONAL ARRAYS
matrix[0][3] may overwrite matrix[1][0]

VARIABLE-LENGTH ARRAYS

 New C99 Feature: Variable-length array defined within function


 Global arrays must still have fixed (constant) length

MEMORY ADDRESSES
(Note: arrays are combined memory locations)
Storage cells are typically viewed as being byte-sized
RECALL:

One memory cell is represented as a byte-sized

 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

&variable Returns variable’s memory address


Dereference

*pointer Returns contents stored at address


Indirect assignment

*pointer = val Stores value at address


Assignment

pointer = ptr Stores pointer in another variable

TRACE THE PROGRAM:

WHAT IS WRONG WITH THIS CODE?


 *int_ptr1 means you are referring to a value, not a memory location. When you assigned
int_ptr2 to it, it’s not an int, but a pointer to an int (address). Pero ung sasalo, *int_ptr1
is expecting a literal value.
 As a pointer (not declared, but initialized) kung walang *, ito ay tatanggap ng &address
or another pointer (na walang *).

int *int_ptr1 = &int1; valid


*int_ptr1 = int_ptr2; Invalid, dapat *ptr = *ptr o kaya
*ptr = literalValue;
*ptr = 3;
int_ptr1 = &int1; Valid
int2 = *ptr2 Valid
*int_ptr1 = int2 Valid, kasi variable (literal value) ung int2
int_ptr1 = *int_ptr2 Invalid, because *int_ptr2 is not an int
pointer, it’s a value

Int_ptr1 is expecting a memory


location/another pointer
*ptr = *ptr2 Valid, assign the value pointed to by ptr2 to
ptr

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

 Special constant pointer NULL


 Points to no data
 Dereferencing illegal – causes segmentation fault
 To define, include <stdlib.h> or <stdio.h>

GENERIC POINTERS
void * - pointer to anything

 Lose all information about what type of thing is pointed to.


 Reduces effectiveness of compiler’s type-checking
 Can’t use pointer arithmetic.

PASS-BY-REFERENCE
ARRAY AND POINTERS
Array name – a pointer to the initial (0th) array element

 When an array is passed to a function as a pointer, the array size is lost.


 It’s usually bad style to interchange arrays and pointers

*Correction: I think it should be 4 instead of 8.


STRINGS

 In C, strings are just an array of characters.


 Terminated with ‘\0’ character.
 Arrays for bounded-length strings.
 Pointer

Pointer to Pointer (char **argv)

You might also like