Introduction to Pointers Pointers and Functions - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

Introduction to Pointers Pointers and Functions

Description:

Introduction to Pointers Pointers and Functions – PowerPoint PPT presentation

Number of Views:505
Avg rating:3.0/5.0
Slides: 55
Provided by: csU9
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Pointers Pointers and Functions


1
Introduction to PointersPointers and Functions
  • CS2023 Intersession 2007

2
Outcomes Introduction to Pointers
  • Memory as a Programming Concept, Chapters 2, 3
  • On reserve in the library
  • The C Programming Language, Chapter 5
  • Other C programming books on reserve
  • After the conclusion of this section you should
    be able to
  • Describe how memory is allocated in the
    compilation and linking process
  • Describe the two separate areas of memory heap
    and stack
  • Declare and initialize pointers of the
    appropriate type
  • Dereference and copy pointers

3
Compilation Linking Revisited
  • Memory as a Programming Concept in C and C by
    Frantisek Franek
  • What is main purpose of a compiler?
  • Translate source code instructions into machine
    instructions
  • Replace each symbolic reference (such as x in
    x1) by an address reference

4
Compilation, Linking, Execution of C/C
programs
compilation and linking
Franek, Memory as a Programming Concept
Chapter 2, Slide 1
5
A sample C program
include int a100,1,2,3,4,5,6,7,8,9
int b10 void main() int i static
int k 3 for(i 0 i
printf("d\n",ai) bi kai
Chapter 2, Slide 4
Franek, Memory as a Programming Concept
6
Object module structure
Chapter 2, Slide 3
Franek, Memory as a Programming Concept
7
Object module of the sample C program
Chapter 2, Slide 5
Franek, Memory as a Programming Concept
8
Creation of loadmodule
Chapter 2, Slide 6
Franek, Memory as a Programming Concept
9
Memory Management
  • Stack-based memory implicitly managed by
    function calls and function returns.
  • Heap-based memory explicitly managed by the
    programmer.

10
Heap
  • Dynamically allocated variables stored on heap
  • Memory allocated and destroyed at run time, under
    control of programmer!
  • No guarantee that first variable to be destroyed
    is last created
  • This area of memory can have holes and is called
    the heap

11
Memory Management
  • Usually heap and stack begin at opposite ends of
    the program's memory, and grow towards each other

logical address space
Load module
heap
free memory
static
code
stack
low
high
12
Dynamic memory allocation
Chapter 2, Slide 9
Franek, Memory as a Programming Concept
13
Address Space
  • Physical Address Space
  • Memory address in physical system memory
  • Not necessarily contiguous may be scattered
    about
  • Logical Address Space
  • Addresses internal to the program
  • Relative to program beginning address
  • Mapped to physical memory at load-time

14
Addresses
  • Each variable occupies one or more bytes of
    memory
  • Address of first byte is address of variable

contents
address
. . .
0
01010011
2000
1
01110011
2001
i
. . .
2002
i's address is 2000
2003
n-1
01000011
. . .
15
Variables and Objects, pointers and addresses
  • variables and data objects are data containers
    with names
  • the value of the variable is the code stored in
    the container
  • to evaluate a variable is to fetch the code from
    the container and interpret it properly
  • to store a value in a variable is to code the
    value and store the code in the container
  • size of a variable is the size of its container

'A'
16916
Chapter 3, Slide 1
Franek, Memory as a Programming Concept
16
Pointers
  • A pointer is a variable whose value is a memory
    address representing the location of a data
    container on either the run-time stack or on the
    heap (or static data region)
  • Pointers have names, values and types.

17
Declaring Pointers
  • For any C data type T, you can define a variable
    of type
  • "pointer to T"
  • int p pointer to int, or int pointer
  • char q pointer to char, or char pointer
  • double w pointer to pointer to double
  • Here
  • p may point to a block of sizeof(int) bytes
  • q may point to a block of sizeof(char) bytes
  • w may point to a block of sizeof(double) bytes

18
Declaring Pointers
  • The placement of the whitespace around the
    asterisk in a pointer declaration is a convention
  • int p
  • int p
  • int p
  • We will use the third convention

19
Nameless data containers
But how a pointer can know what is at a
particular address?
Chapter 3, Slide 11
Franek, Memory as a Programming Concept
20
What is stored in the four bytes at addresses
802340 .. 802343 ?(a) four characters 'A' 'B'
'C' 'D' (b) two shorts 16961, 17475(c) long
1145258561(d) float 781.035217(e) none can tell
(e) is the right answer.
Chapter 3, Slide 12
Franek, Memory as a Programming Concept
21
Chapter 3, Slide 13
Franek, Memory as a Programming Concept
22
Chapter 3, Slide 14
Franek, Memory as a Programming Concept
23
Setting Pointers
  • Using address operator
  • Using dynamic memory allocation
  • Through calculation
  • Pointer arithmetic

24
Address Operator
  • Declaring a pointer variable sets aside space for
    a pointer but doesn't make it point to an object
  • int p
  • Need to initialize p
  • int i, p
  • p i
  • i is an int variable
  • i is like an int pointer, pointing to the
    variable i
  • (but you must not assign to it)

25
Dereferencing Indirection Operator
  • int i, p
  • p i
  • i 1
  • printf("d\n", i) / prints 1/
  • printf("d\n", p) / prints 1/

26
Dereferencing Indirection Operator
  • p 2
  • printf("d\n", i) / prints 2 /
  • printf("d\n", p) / prints 2 /
  • Can change variable i without actually using i
  • use this to implement function call by reference

27
Dereferencing Indirection Operator
  • Never dereference an unitialized pointer!
  • int p
  • printf("d", p) / prints garbage /
  • Assigning a value to p much worse!
  • int p
  • p 1
  • Where does p point to? It might point to memory
    belonging to the program, causing it to behave
    erratically, or to memory belonging to another
    process, causing a segmentation fault.

28
Addresses and Values of variables
  • int i 1, j 2, pi j
  • printf("ip, jp, pip\n", i, j, pi)
  • printf("pip, pid, id, jd\n", pi, pi,
    i, j)
  • return 0

0x0b0
Address
0x0ac
0x0b4
1
2
0x0b0
i j pi
29
Pointer Assignment
  • Can copy pointers of the same type
  • int i, j, p, q
  • p i
  • q p
  • p 1

30
Pointer Assignment
  • q 2

2
p
i
q
31
Pointer Assignment
  • Don't confuse qp with qp
  • int i, j, p, q
  • p i
  • q j
  • i 1
  • q p

?
p
i
?
q
j
1
p
i
1
q
j
32
Pointer Assignment
  • Cannot assign pointer of one type to another
    without a cast
  • void is an exception (not for all compilers!)
  • What does this print?
  • long i 1145258561
  • long ip i
  • char cp
  • printf("ipld\n", ip)
  • cp (char)ip
  • printf("cpc\n", cp)

33
Using pointers example 1
  • int i, j, pi
  • scanf("dd", i, j)
  • pi i j ? i j
  • printf("d\n", pi)

34
Using pointers example 2
  • int i, j
  • int pi i
  • int pj j
  • scanf("dd", pi, pj)
  • printf("d\n", pi pj ? pi pj)
  • Don't need the operator in scanf, because pi
    and pj are already pointers

35
Using pointers example 3
  • Why bother using i and j?
  • int pi
  • int pj
  • scanf("dd", pi, pj)
  • What will go wrong?

36
const pointers and pointers to const
  • const int p pointer to a constant
    integer, the value of p may change, but the value
    of p can not
  • int const p constant pointer to integer
    the value of p can change, but the value of p
    can not
  • const int const p constant pointer to
    constant integer.

37
Generic Pointers
  • A reference to "any" kind of object
  • use a variable of type void
  • void p
  • defines a generic, or typeless pointer p.
  • Generic pointers cannot be deferenced. Must cast.

38
Generic Pointers
  • Data stored in a memory object can be recovered
    as a value of a specific data type. For example,
    for a generic pointer
  • void p
  • which points to an object containing a double
    value,
  • you can retrieve this value using the following
    syntax
  • (double)p

39
Generic Pointers
  • void p
  • char c 'c'
  • char cp c
  • Can assign to p
  • p cp
  • but not dereference it
  • putchar(p)
  • Have to use cast
  • putchar((char)p)

40
NULL macro
  • Special "zero" value that is useful to initialize
    pointers, and then to compare pointer's value
  • if(p NULL) ...
  • Actually equal to 0
  • if (p 0) ...
  • if (!p) ...
  • NULL defined in six headers, including stdio.h
    and stdlib.h

41
Outcomes Pointers Functions
  • The C Programming Language, Chapter 5, section
    5.2
  • Other textbooks on C on reserve
  • After the conclusion of this section you should
    be able to
  • Write functions that modify more than one
    variable by using pointers as parameters

42
Pointers as Arguments
  • In order to modify a parameter x
  • Pass x to the function
  • Declare corresponding formal parameter p to be a
    pointer
  • p will have the value x, hence p is an indirect
    reference to x
  • Therefore can both read and modify x

43
Pointers as Arguments
  • Trace the execution of
  • void swap(int x, int y)
  • int temp
  • temp x
  • x y
  • y temp
  • / call int i 2, j 3 swap(i, j) /

44
Swap example
  • Initial values i 3, j 4
  • Before call to swap()

3
4
i
j
i
j
45
Swap example
  • Immediately after call

3
4
i
j
i
j
x
y
46
Swap example
  • After temp x

3
4
3
i
j
i
j
x
y
temp
47
Swap example
  • After x y

4
4
3
i
j
i
j
x
y
temp
48
Swap example
  • After y temp

4
3
3
i
j
i
j
x
y
temp
49
Swap example
  • After return from swap()

4
3
i
j
i
j
50
Modifying an Argument to a Function
  • 1. Declare the formal parameter FP as a pointer,
    for example int FP
  • 2. In the body of the procedure, dereference FP,
    that is use FP
  • 3. In the call
  • if actual parameter AP, is the variable you wish
    to modify, use the address of AP
  • for example f(AP)
  • if actual parameter AP is a pointer to the
    variable you wish to modify, use AP without the
    address operator
  • for example f(AP)

51
Example
  • Find the largest and smallest elements in an
    array
  • void maxMin(int a, int n, int max, int min)
  • int i
  • max min a0
  • for (i 1 i
  • if (ai max)
  • max ai
  • else if (ai
  • min ai

52
Calling Program
  • define N 100
  • void maxMin(int a, int n, int max, int min)
  • int main()
  • int bN, i, big, small
  • for (i 0 i
  • scanf(d, bi)
  • maxMin(b, N, big, small)
  • printf(Largest d\n Smallest d\n,
  • big, small)
  • return 0

53
Pointers as Return Values
  • / Given pointers to two integers, return
  • pointer to whichever integer is larger
  • /
  • int max(int a, int b)
  • if (a b)
  • return a
  • else
  • return b
  • / int p, x, y p max(x, y) /

54
Caution!
  • Never return a pointer to an automatic local
    variable
  • int f()
  • int i
  • ...
  • return i
  • The variable i doesn't exist once f returns, so
    the pointer to it won't be valid
Write a Comment
User Comments (0)
About PowerShow.com