C : Introduction - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

C : Introduction

Description:

C : Introduction – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 24
Provided by: toshi155
Category:
Tags: introduction | pi

less

Transcript and Presenter's Notes

Title: C : Introduction


1
Basic Data Types of C
Integer Types
short month // half of a machine word int car
// one machine word unsigned long
distance // one or two machine words

Character Types char c \0 //
one byte char EndOfLine \n
Real Types double salary // two machine
words float wage // one machine word
2
Pointer Types Initialization
int ip1, ip2 double dp
int ival 1027 // pi initialized to address
no object int pi 0 // pi2 initialized to
address ival int pi2 ival // pi and pi2
now both address ival pi pi2 // pi2 now
address no object pi2 0
3
Pointers More
The void pointer can hold the address value of
any pointer type.
int i 0 int pi i double d -9.01
double pd d void pv pi // pv
holds the address value of pi pv pd
// pv now holds the address value of pd
pi // type int evaluates to the address
contained within pi pi // type int
evaluates to the actual address of pi int ppi
pi // a pointer to a pointer to int.
pi
ppi
4
Pointer Arithmetic
double p
sizeof (double)
p
pk
Data type Current address New
address
p 5000 p 5000 p 5000
p 1
char int double
5001
p 1
5000 12 5002
p 6
5000 64 4976
5
Array Types
double x50, y200 double z1020 //
2-dimensional array
x0
x49
addr addr 49 4
const int ArraySize 10 long AArraySize
int A20 int v 20 Av 0 //
index v exceeds 19 core dumped.
6
Increment Decrement Operators
int ia10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
int i 0, j 9 while ( i i iaj-- // ia10 9, 8, 7, 6, 5, 5,
6, 7, 8, 9
i increments i after its current value is used
as index into ia.
int ia10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
int i -1, j 10 while ( i instead of 10 because ia10 is illegal.
ia i ia--j // ia10 9, 8, 7, 6, 5,
5, 6, 7, 8, 9
i increments i before its use as an index into
ia.
7
Shorthand Binary Operators
i 1
i i 1
left operand
i - j
j j 1 i i j
i j
i i j j j 1
More efficient!
8
Multidimensional Arrays
int ia43 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11
int main() const int rowSize 4
const int colSize 3 int ia rowSize
colSize for ( int i 0 i i) for ( int j 0 j j) ia ij i j
9
The sizeof Operator
Returns the size of an object or type name in
bytes.
sizeof (type name) sizeof (object) sizeof
object
The value returned is of a machine-specific type
size_t.
include int ia 0, 1, 2
size_t array_size size_of ia size_t
element_num array_size / sizeof ( int )
10
Arrays vs Pointers
The array identifier evaluates to the address of
its first element.
Its type is that of pointer to the elements
type.
int ia 0, 1, 2, 3, 5, 8, 13, 21 // The
type of ia is int // two equivalent forms ia
ia0 // both yield the value of the first
element ia ia0 // both acccess the
address of // the second element ia1 ia1
// both access the value of // the second
element (ia1) ia1 // the dereference
operator has a // higher precedence than the //
addition operator ia 1 // the sum of the
first // element and 1 (ia1)
// the second element
11
Traversal of an Array throughPointer Manipulation
// iterate across an array without // knowing
its actual size include void
ia_print (int pbegin, int pend) while (
pbegin ! pend) cout pbegin int main() int
ia9 0, 1, 1, 2, 3, 5, 8, 13, 21
ia_print ( ia, ia9 )
include int main() int ia9
0, 1, 1, 2, 3, 5, 8, 13, 21 int
pbegin ia int pend ia 9
while (pbegin ! pend ) cout pbegin 12
Array Parameters
An array is always passed as a pointer to its
first element.
// three equivalent declarations of putValues(
) void putValues ( int ) void putValues ( int
) // a better declaration if the
argument
// calling the function with is an
array void putValues ( int 10 ) // ok, but
the arrays size is irrelevant
The changes to an array parameter within the
called function are made to the array argument
itself (call-by-reference).
// A function does not intend to change the array
elements void putValues (const int 10 )
13
Multidimensional Arrays as Parameters
Specify the size of all its dimensions beyond
that of its first.
void putValues (int matrix 10, int rowSize )
If the function is to accept a matrix with
variable sizes in all its dimensions, it needs
to use pointer to pointer types.
void putValues (int matrix , int rowSize,
int colSize) // wrong void putValues (int
matrix, int rowSize, int colSize) // ok now
14
Contd
void putValues (int matrix, int rowSize, int
colSize) int ia23 0, 1, 2 , 3, 4,
5 // illegal the type of ia does not
match int putValues ( ia, 2, 3)
int pa pa new int 2
// two-step dynamic memory allocation for
(int i 0 i 3 pa00 0 pa01 1 pa02
2 pa10 3 pa11 4 pa12 5
putValues ( pa, 2, 3 ) // ok now
15
new and delete
Dynamic memory allocation memory is allocated at
run-time.
(int)
int pi new int // pi ? int pi new int
(1024) // allocates and initializes an
object int pia new int 10 // allocates
an array of ten integers
delete pi delete pia
16
Reference Type
A reference serves as an alternative name for an
object.
Similar to the use of a pointer but without
resorting to pointer syntax.
int ival 1024 int refVal ival // ok
refVal is a reference to ival int refVal
// error a reference must be initialized to
an object refVal 2 // ok,
equivalent to ival 2. int pi refVal
// initialize pi with the address of ival
17
References (Contd)
// defines two objects of type int int ival
1024, ival2 2048
1024
2048
ival2
ival
// defines one reference and one object int rval
ival, rval2 ival2
rval
rval4
// defines one object, one pointer, one
reference int ival3 1024, pi ival3, ri
ival3
2048
1024
ival3
rval2
ri
// defines two references int rval3 ival3,
rval4 ival2
pi
rval3
18
Pointers, Arrays, and References
int a 3, 8, -4, 6 int b a0 int
c (a3) int d c int e d
What are stored in the array a after execution
of the statements below?
b-- d b a1 c (a1) e - a2
c0 d e b d1 2 (e)
19
Initialization (1)
int a 3, 8, -4, 6
b
reference to a variable
int b a0
20
Initialization (2)
address
int c (a3)
b
reference to a pointer
int d c
c
d
21
Initialization (3)
address
int e d
b
c
d
e
22
Update (1)
2
16
a0--
b--
b
d b a1
c
d
a3 a0 a1
e
23
Update (2)
52
12
26
c (a1)
b
e - a2
c
d
a1 - a2
c0 d e b
a1 a1 a1 a0
e
d1 2 (e)
a2 2 a1
Write a Comment
User Comments (0)
About PowerShow.com