0% found this document useful (0 votes)
69 views

Arrays and Pointers in C: Alan L. Cox Alc@rice - Edu

This document provides an overview of arrays and pointers in C. It discusses how arrays are represented in memory as contiguous blocks of memory and can be accessed using indexes. Pointers are explained as variables that store memory addresses, and can be dereferenced to access the value at that address. Key points covered include pointer arithmetic, passing arrays to functions (which really passes a pointer), null pointers, strings as character arrays, and multi-dimensional arrays.

Uploaded by

sivakumar06
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Arrays and Pointers in C: Alan L. Cox Alc@rice - Edu

This document provides an overview of arrays and pointers in C. It discusses how arrays are represented in memory as contiguous blocks of memory and can be accessed using indexes. Pointers are explained as variables that store memory addresses, and can be dereferenced to access the value at that address. Key points covered include pointer arithmetic, passing arrays to functions (which really passes a pointer), null pointers, strings as character arrays, and multi-dimensional arrays.

Uploaded by

sivakumar06
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Arrays and Pointers in C

Alan L. Cox
[email protected]
Objectives
Be able to use arrays, pointers, and strings in
C programs
Be able to explain the representation o these
data types at the machine level, including
their similarities and dierences
Cox / Rixner Arrays and Pointers 2
Cox / Rixner Arrays and Pointers 3
Arrays in C
!o bounds chec"ing#
Allo$ed % usually causes no error
array&'() may over$rite b
*nli"e +ava, array si,e in declaration
int array[10];
int b;
array[0] = 3;
array[9] = 4;
array[10] = 5;
array[-1] = 6;
Compare- C- int array[10];
+ava- int[] array = new int[10];
All elements o same type % homogenous
.irst element /index (0
Last element /index si,e 1 '0
Cox / Rixner Arrays and Pointers 4
Array 2epresentation
3omogeneous 4ach element same si,e % s bytes

An array o m data values is a se5uence o m s bytes

6ndexing- (
th
value at byte s (, '
st
value at byte s ', 7
m and s are not part o representation

*nli"e in some other languages

s "no$n by compiler % usually irrelevant to programmer

m oten "no$n by compiler % i not, must be saved by


programmer
a[0]
a[1]
a[2]
0x1000
0x1004
0x1008
int a[3];
Cox / Rixner Arrays and Pointers 5
Array 2epresentation
char c1;
int a[3];
char c2;
int i;
c1
a[0]
a[1]
a[2]
i
0x1000
0x1004
0x1008
0x100C
0x1014
c2 0x1010
Could be optimi,ed
by ma"ing these
adjacent, and
reducing padding
/by deault, not0
Array aligned by
si,e o elements
Cox / Rixner Arrays and Pointers 6
Array 8i,es
9hat is
sizeof(array[3]):
sizeof(array):
int array[10];
;
;(
returns the size of
an object in bytes
Cox / Rixner Arrays and Pointers 7
<ulti1=imensional Arrays
int atri![2][3];
atri![1][0] = 1";
atri![0][0]
atri![0][1]
atri![0][2]
0x1000
0x1004
0x1008
atri![1][0]
atri![1][1]
atri![1][2]
0x100C
0x1010
0x1014
2ecall- no bounds chec"ing
9hat happens $hen you $rite-
atri![0][3] = 42;
Row Major
Organization
Cox / Rixner Arrays and Pointers
>ariable1Length Arrays
int
f#nction(int n)
$
int array[n];
%
New C99 feature: Variable-length arrays
efine within functions
!lobal arrays "ust still ha#e fixe $constant% length
Cox / Rixner Arrays and Pointers !
<emory Addresses
8torage cells are typically vie$ed as being
byte1si,ed

*sually the smallest addressable unit o memory


& .e$ machines can directly address bits individually

8uch addresses are sometimes called byte-


addresses
<emory is oten accessed as $ords

*sually a $ord is the largest unit o memory


access by a single machine instruction
&
CL4A2?s $ord si,e is @ bytes /A sizeof(&on')0

A word-address is simply the byte1address o the


$ord?s irst byte
Cox / Rixner Arrays and Pointers "#
Pointers
8pecial case o bounded1si,e natural numbers

<aximum memory limited by processor $ord1si,e

B
CB
bytes A ;DB, B
E;
bytes A 'E exabytes
A pointer is just another "ind o value

A basic type in C
int ()tr;
The variable ptr is a pointer to an int.
Cox / Rixner Arrays and Pointers ""
Pointer Operations in C
Creation
& variable 2eturns variable?s memory address
=ereerence
* pointer 2eturns contents stored at address
6ndirect assignment
* pointer = val 8tores value at address
O course, still have...
Assignment
pointer = ptr 8tores pointer in another variable
Cox / Rixner Arrays and Pointers "2
*sing Pointers
int i1;
int i2;
int ()tr1;
int ()tr2;
i1 = 1;
i2 = 2;
)tr1 = *i1;
)tr2 = )tr1;
()tr1 = 3;
i2 = ()tr2;
i1+
i2+
)tr1+
0x1000
0x1004
0x1008
%
)tr2+
%
0x100C
0x1010
0x1014
1
2
0!1000
0!1000
3
3
Cox / Rixner Arrays and Pointers "3
*sing Pointers /cont.0
Fype chec" $arning- int,)tr2 is not an int
int1 becomes @
int int1 = 1036; -( soe .ata to )oint to (-
int int2 = /;
int (int,)tr1 = *int1; -( 'et a..resses of .ata (-
int (int,)tr2 = *int2;
(int,)tr1 = int,)tr2;
(int,)tr1 = int2;
9hat happens:
Cox / Rixner Arrays and Pointers "4
*sing Pointers /cont.0
Fype chec" $arning- (int,)tr2 is not an int (
Changes int,)tr1 % doesn?t change int1
int int1 = 1036; -( soe .ata to )oint to (-
int int2 = /;
int (int,)tr1 = *int1; -( 'et a..resses of .ata (-
int (int,)tr2 = *int2;
int,)tr1 = (int,)tr2;
int,)tr1 = int,)tr2;
9hat happens:
Cox / Rixner Arrays and Pointers "5
Pointer Arithmetic
pointer G number pointer % number
4.g., pointer 0 1 adds ' something to a pointer
char ();
char a;
char b;
) = *a;
) 0= 1;
int ();
int a;
int b;
) = *a;
) 0= 1;
6n each, p no$ points to b
/Assuming compiler doesn?t
reorder variables in memory0
Adds 'Hsi,eo/char0 to
the memory address
Adds 'Hsi,eo/int0 to
the memory address
Pointer arithmetic should be used cautiously
Cox / Rixner Arrays and Pointers "6
Fhe 8implest Pointer in C
8pecial constant pointer 1233

Points to no data

=ereerencing illegal % causes segmentation fault

Fo deine, include 4st.&ib5h6 or 4st.io5h6


Cox / Rixner Arrays and Pointers "7
Deneric Pointers
void H- a Ipointer to anythingJ
Lose all inormation about $hat type o thing
is pointed to

2educes eectiveness o compiler?s type1chec"ing

Can?t use pointer arithmetic


7oi. ();
int i;
char c;
) = *i;
) = *c;
)#tchar(((char ()));
type cast- tells the compiler to
IchangeJ an object?s type /or
type chec"ing purposes % does
not modiy the object in any $ay0
=angerous# 8ometimes
necessary7
Cox / Rixner Arrays and Pointers "
Pass1by12eerence
7oi.
set,!,an.,y(int (!8
int (y)
$
(! = 1001;
(y = 1002;
9
7oi.
f(7oi.)
$
int a = 1;
int b = 2;
set,!,an.,y(*a8*b);
9
1
'
a
b
!
y
1001
100'
Cox / Rixner Arrays and Pointers "!
Arrays and Pointers
=irty IsecretJ-
Array name a pointer to the
initial /(th0 array element
a[i] ((a0i)
An array is passed to a unction
as a pointer

Fhe array si,e is lost#


*sually bad style to interchange
arrays and pointers

Avoid pointer arithmetic#


Really int (array
int
foo(int array[]8
#nsi'ne. int size)
$
% array[size - 1] %
9
int
ain(7oi.)
$
int a[10]8 b[5];
% foo(a8 10)% foo(b8 5) %
9
(ust ex)licitly
)ass the size
*assing arrays:
Cox / Rixner Arrays and Pointers 2#
Arrays and Pointers
int
foo(int array[]8
#nsi'ne. int size)
$
%
)rintf(:;.<n=8 sizeof(array));
9
int
ain(7oi.)
$
int a[10]8 b[5];
% foo(a8 10)% foo(b8 5) %
)rintf(:;.<n=8 sizeof(a));
9
What does this print?
What does this print?

4#
... because array is really
a pointer
Cox / Rixner Arrays and Pointers 2"
Arrays and Pointers
int i;
int array[10];
for (i = 0; i 4 10; i00)
$
array[i] = %;
9
int ();
int array[10];
for () = array; ) 4 *array[10]; )00)
$
() = %;
9
These two blocks of code are functionally equivalent
Cox / Rixner Arrays and Pointers 22
8trings
6n C, strings are just an array o characters

Ferminated $ith KL(? character

Arrays or bounded1length strings

Pointer or constant strings /or un"no$n length0


char str1[15] = :>e&&o8 wor&.?<n=;
char (str2 = :>e&&o8 wor&.?<n=;
> e & & o 8 w & o r . ?<n
length
> e & & o 8 w & o r . ?<n
ter"inator
*ascal+ ,a#a+ -
C+ -
C ter"inator: @<0@
Cox / Rixner Arrays and Pointers 23
8tring length
<ust calculate length-
Provided by standard C library- Ainc&#.e 4strin'5h6
int
str&en(char str[])
$
int &en = 0;
whi&e (str[&en] ?= B<0@)
&en00;
ret#rn (&en);
9
can pass an
array or pointer
Chec" or
terminator
array access
to pointer#
9hat is the si,e
o the array:::
Pointer to Pointer /char HHargv0
Cox / Rixner Arrays and Pointers 24
Passing arguments to main-
int
ain(int ar'c8 char ((ar'7)
$
555
9
an arrayMvector o
char H
2ecall $hen passing an
array, a pointer to the
irst element is passed
si,e o the argv arrayMvector
8uppose you run the program this $ay
21CD; 5-)ro'ra he&&o 1 2 3
ar'c == 5 /ive strings on the
command line)
Cox / Rixner Arrays and Pointers 25
char HHargv
ar'7[0]
ar'7[1]
ar'7[2]
0x1000
0x1008
0x1010
ar'7[3]
ar'7[4]
0x1018
0x10'0
:5-)ro'ra=
:he&&o=
:1=
:2=
:3=
Fhese are strings##
!ot integers#
Cox / Rixner Arrays and Pointers 26
!ext Fime
8tructures and *nions

You might also like