Docsity

Understanding Arrays & Pointers in C: Representation, Sizes, Multi-Dimensional Arrays, Var, Study notes of Computer Science

Partial preview of the text

Download Understanding Arrays & Pointers in C: Representation, Sizes, Multi-Dimensional Arrays, Var and more Study notes Computer Science in PDF only on Docsity!

Arrays and Pointers in C

Scott Rixner [email protected]

Scott Rixner

Arrays and Pointers

Arrays in C

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

Array size in declaration must be constant

int^

array[10];

int^

b;

array[0]

array[9]

array[10]

array[-1]

Compare:

C:^

int^

array[10]; Java:

int[10]

array;

All elements of same type – homogenous First element (index 0) Last element (index size - 1)

Scott Rixner

Arrays and Pointers

Array Representation

char

c1; int^

a[3]; char

c2; int^

i;

c

i a[2] a[1] a[0]

0x1014 0x100C0x10080x1004^ 0x

c 0x

Could be optimized bymaking these adjacent,and reducing padding(by default, not)^ Array aligned bysize of elements

Scott Rixner

Arrays and Pointers

Array Sizes

What is sizeof(array[3])

?

sizeof(array)

?

int^

array[10];

(^440)

returns the size ofan object in bytes

Scott Rixner

Arrays and Pointers

Variable-Length Arrays

int^

n^ =^

…; int^

array[n];

New C99 feature, but not fully supported by

gcc

We’ll ignore

Scott Rixner

Arrays and Pointers

Addresses Arrays^ Š^

Elements in contiguous addresses, viewed as element

offsets from base of array

Size of storage cells typically viewed as bytes^ Š^

Usually smallest addressable unit of memory^ •^ Few machines can directly access bits individually Š Addresses also known as

byte-addresses

Sometimes viewed as words^ Š^

Most common size of addressable unit of memory Š When applicable, use

word-addresses

Š^ Word-address m = byte-address (m

×^ bytes/word)

Scott Rixner

Arrays and Pointers

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

Scott Rixner

Arrays and Pointers

Using Pointers^ int

i1; int i2; int *ptr1; int *ptr2; i1 = 1; i2 = 2; ptr1 = &i1; ptr2 = ptr1; *ptr1 = 3; i2 = *ptr2;

ptr1: i2: i1: 0x10080x1004 0x

ptr2: 0x10140x10100x100C

0x10000x1000^2313

Scott Rixner

Arrays and Pointers

Using Pointers (cont.)

Type check warning:

*int_ptr

is not an

int

Changes

int_ptr

- doesn’t change

int

int^

int

/* some data to point to

int^

int

int *int_ptr1 = &int1;

get addresses of data

int *int_ptr2 = &int2;int_ptr1 = *int_ptr2;int_ptr1 = int_ptr2;

What happens?

Scott Rixner

Arrays and Pointers

Pointer Arithmetic^ pointer

+^ number

pointer

-^ number

E.g.,

pointer

+^1

adds 1 something

to a pointer

char^

*^ p; char^

a; char^

b; p^ =^ &a;p^ +=^

int^ *

p; int^

a; int^

b; p^ =^ &a;p^ +=^

In each, p now points to b(Assuming compiler doesn’treorder variables in memory)

Adds 1sizeof(char) tothe memory address*

Adds 1sizeof(int) tothe memory address*

Pointer arithmetic is usually

confusing and bad style

Scott Rixner

Arrays and Pointers

Generic Pointers void *: a “pointer to anything” Lose all information about what type of thingis pointed to^ Š^

Reduces effectiveness of type-checking Š Can’t use pointer arithmetic

Useful in limited circumstances^ Š^

void^ Might find useful in last project

*^ p; int^

i; char^

c; p^ =^ (void

*)^

&i;

p^ =^ (void

*)^

&c;

type cast: tells the compiler to“change” an object’s type (for typechecking purposes – does not modifythe object in any way) Dangerous! Sometimes necessary…

Scott Rixner

Arrays and Pointers

Pass-by-Reference^ void^ set_x_and_y(int * x,

int * y)

{^ *x = 1001;^ *y = 1002; } void f() {^ int a = 1;^ int b = 2;^ set_x_and_y(&a,&b); }

a b x y

Scott Rixner

Arrays and Pointers

Arrays and Pointers^ int^

i; int^

array[10]; for^

(i^ =^

0;^ i^

<^ 10;

i++)

{^ array[i]

=^ …;

int^ *p;int^

array[10]; for^ (p

=^ array;

p^ <^

&array[10];

p++)

{ *p^

=^ …;

Scott Rixner

Arrays and Pointers

Strings In C, strings are just an array of characters^ Š^

Terminated with ‘\0’ character Š Arrays for bounded-length strings Š Pointer for constant strings (or unknown length)

char

str1[15] = “Hello, world!\n”;

char *str

= “Hello, world!\n”;

H^ e^

l^ l^

o^ ,^

w^

l

o^ r^

d^!

\n

H^ e^ l length

l^ o

,^

w^

l

o^ r^

d^!

\n^ terminator

C, … Pascal, Java, …

C terminator:

’\0’