0% found this document useful (0 votes)
24 views10 pages

03 Arrays Pointers

Arrays in C allow storing homogeneous elements and are represented by contiguous memory locations. Multi-dimensional arrays are stored in row-major order. Pointers in C store memory addresses and can be used to access and update array elements indirectly. Pointer operations include dereferencing, address-of, assignment, and indirect assignment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views10 pages

03 Arrays Pointers

Arrays in C allow storing homogeneous elements and are represented by contiguous memory locations. Multi-dimensional arrays are stored in row-major order. Pointers in C store memory addresses and can be used to access and update array elements indirectly. Pointer operations include dereferencing, address-of, assignment, and indirect assignment.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Arrays in C

All elements of same type – homogenous

Unlike Java, array size in declaration

int array[10]; Compare: C: int array[10];


int b; Java: int[] array = new int[10];
First element (index 0)
array[0] = 3; Last element (index size - 1)
array[9] = 4;
array[10] = 5;
array[-1] = 6;

No bounds checking!
Allowed – usually causes no obvious error
array[10] may overwrite b

Cox Arrays and Pointers 1


Array Representation
Homogeneous  Each element same size – s bytes
 An array of m data values is a sequence of ms bytes
 Indexing: 0th value at byte s0, 1st value at byte s1, …

m and s are not part of representation


 Unlike in some other languages
 s known by compiler – usually irrelevant to programmer
 m often known by compiler – if not, must be saved by
programmer

0x1008 a[2]
0x1004 a[1] int a[3];
0x1000 a[0]

Cox Arrays and Pointers 2


Array Representation
char c1;
int a[3];
char c2;
int i;

0x1014 i
0x1010 c2
0x100C a[2] Could be optimized by
a[1] making these adjacent,
0x1008
and reducing padding
a[0] (by default, not)
0x1004

0x1000 c1
Array aligned by
size of elements

Cox Arrays and Pointers 3


Array Sizes

int array[10];

What is

sizeof(array[3])? 4
returns the size of
an object in bytes
sizeof(array)? 40

Cox Arrays and Pointers 4


Multi-Dimensional Arrays

0x1014 matrix[1][2]
0x1010 matrix[1][1]
int matrix[2][3];
0x100C matrix[1][0]
0x1008 matrix[0][2]
matrix[1][0] = 17;
0x1004 matrix[0][1]
0x1000 matrix[0][0]
Recall: no bounds checking

What happens when you write: “Row Major”


Organization
matrix[0][3] = 42;

Cox Arrays and Pointers 5


Pointers

Special case of bounded-size natural numbers


 Maximum memory limited by processor word-size
 232 bytes = 4GB, 264 bytes = 16 exabytes

A pointer is just another kind of value


 A basic type in C

int *ptr;

The variable “ptr” stores a pointer to an “int”.

Cox Arrays and Pointers 6


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

Of course, still have...

Assignment
pointer = ptr Stores pointer in another variable
Cox Arrays and Pointers 7
Using Pointers

int i1;
int i2;
int *ptr1; 0x1014 … 0x1000
int *ptr2;
0x1010 ptr2:
i1 = 1; 0x100C … 0x1000
i2 = 2;
0x1008 ptr1:
ptr1 = &i1; 0x1004 i2: 2
3
ptr2 = ptr1;
0x1000 i1: 3
1
*ptr1 = 3;
i2 = *ptr2;

Cox Arrays and Pointers 8


Using Pointers (cont.)
int int1 = 1036; /* some data to point to */
int int2 = 8;

int *int_ptr1 = &int1; /* get addresses of data */


int *int_ptr2 = &int2;

*int_ptr1 = int_ptr2;

*int_ptr1 = int2;

What happens?

Type check warning: int_ptr2 is not an int

int1 becomes 8
Cox Arrays and Pointers 9
Using Pointers (cont.)
int int1 = 1036; /* some data to point to */
int int2 = 8;

int *int_ptr1 = &int1; /* get addresses of data */


int *int_ptr2 = &int2;

int_ptr1 = *int_ptr2;

int_ptr1 = int_ptr2;

What happens?

Type check warning: *int_ptr2 is not an int *

Changes int_ptr1 – doesn’t change int1


Cox Arrays and Pointers 10

You might also like