Example 6 another pointer pitfall - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Example 6 another pointer pitfall

Description:

Example 6 another pointer pitfall – PowerPoint PPT presentation

Number of Views:110
Avg rating:3.0/5.0
Slides: 27
Provided by: ceb245elec
Category:

less

Transcript and Presenter's Notes

Title: Example 6 another pointer pitfall


1
  • Example 6 (another pointer pitfall)
  • Illegal indirection
  • short int i, j
  • float p
  • j 2
  • p i
  • p 124

0200 0201 0202 0203 0204 0205 0206 0207
i
j
p
2
  • p now holds the address of i (p is 0200)
  • Since p is a float pointer, the value of 124 is
    converted to a single precision float format of 4
    byte, and then is stored in memory locations
    0200, 0201, 0202, and 0203
  • This implies memory locations 0202 and 0203 get
    overwritten
  • As a result, variables i does not have the
    intended value of 124 (actually now it is 17144),
    and variable j no longer has the value 2

3
  • NEVER use a pointer of one type to point to a
    variable of a different type
  • The result could be unpleasant (e.g., system
    crash or wrong solution)
  • If you dont believe, you might want to try...
  • gcc w8ce1.c
  • w8ce1.c In function main'
  • w8ce1.c9 warning assignment from incompatible
    pointer type
  • a.out
  • Bus Error (core dumped)

4
  • Example 7 (operations of pointers)
  • include
  • main()
  • int x, y
  • int ip
  • ip x
  • x 2
  • ip ip 26
  • / x is 28 now /
  • y ip 7
  • / y is 35 now /
  • ip 4
  • / x is 32 now /
  • printf("x, y d, d\n", x, y)

5
  • Example 8 (display pointers)
  • include
  • main()
  • int x
  • int ip
  • ipx
  • printf("ip p\n", ip)
  • - Output (on a Sun SPARC machine)
  • ip efffe89c

6
  • Example 9 (display address of pointers)
  • include
  • main()
  • int x
  • int ip
  • ipx
  • printf(address of ip p\n", ip)
  • - Output (on a Sun SPARC machine)
  • ip efffe898
  • Before we go ahead further, we need to review
    the increment and decrement operators

7
  • The Increment and Decrement operators , --
  • k or k means k k1
  • k-- or --k means k k-1
  • Example
  • int k1
  • k
  • Result k is now equal to 2
  • Example
  • int k1
  • k
  • Result k is now equal to 2

8
  • Example
  • int k4
  • k--
  • Result k is now equal to 3
  • Example
  • int k4
  • --k
  • Result k is now equal to 3

9
  • Difference between k and k
  • Example
  • k1
  • i10(k) / After the operation, i is 10 /
  • jk / now j is 2 /
  • k is incremented after the expression is
    evaluated
  • i.e.
  • i10(k) means
  • i10k kk1
  • Example
  • sj si / same as
  • sj si jj1 /

10
  • Example
  • k1
  • i10(k) / After the operation, i is 20 /
  • jk / now j is 2 /
  • k is incremented before the expression is
    evaluated
  • i.e.
  • i10(k) means
  • kk1 i10k
  • Example
  • sj si / same as
  • j j1 sj si /

11
  • Example
  • si c / same as
  • si c i i 1 /

12
  • Precedence
  • -- -
  • Do thongs from right to left
  • However,
  • a t
  • / same as
  • a t
  • t t 1 /

13
  • Example 10 (, - -, and pointers)
  • include
  • main()
  • int x
  • int ip
  • x12
  • ipx
  • ip / x is 13 now /
  • (ip) / x is 14 now /
  • printf("x d\n", x)
  • Consider the following
  • a ip / This means

14
  • Example 11
  • include
  • main()
  • int x, val, p
  • p x
  • printf("(1) p p\n", p)
  • / e.g., p aaaa0000 /
  • val 256
  • p val
  • printf("(2) x, p d, p\n", x, p)
  • / x, p 256, aaaa0004 /

15
  • Output of Example 11 (on my Red Hat, Linux
    version 2.4.7-10)
  • (1) p 0xbffff914
  • (2) x, p 256, 0xbffff918
  • (3) val, p 256, 0xbffff914

16
  • Example 12 (pointers and types)
  • include
  • main()
  • char c
  • short int s
  • int i
  • float x
  • double y
  • char cp
  • int ip
  • short int sp
  • float xp
  • double yp
  • cp c sp s ip i
  • xp x yp y
  • printf("Before cp, sp, ip, xp, yp p, p, p,
    p, p\n", cp, sp, ip, xp, yp)

17
  • Output of Example 12
  • Before cp, sp, ip, xp, yp efffe89f, efffe89c,
    efffe898, efffe894, efffe888
  • After cp, sp, ip, xp, yp efffe8a0, efffe89e,
    efffe89c, efffe898, efffe890
  • Should fully understand the above things
  • Typical computers memory is byte-addressable,
    i.e. each cell contains a byte
  • char 1 byte
  • short int 2 bytes
  • int, float 4 bytes
  • double 8 bytes

18
  • 7-2 Arrays
  • 7-2-1 One Dimensional Arrays
  • Example 1
  • int aa10
  • Declares aa to be an array of integers
  • The size of aa is 10
  • Array subscripts start at 0, e.g.
  • aa0, aa1, , aa9.
  • The notation ak refers to the k-th element of
    the array
  • A subscript can be any integer expressions, e.g.
    int variables, int constants

19
aa0 aa1 aa2 aa3 aa4 aa5 aa6 a
a7 aa8 aa9
20
  • Assignment
  • aa4 657
  • aa5 232
  • Initialization
  • int bb4 24, 54, 70, 17
  • As the argument of a function
  • Example 2 (C code)

21
/ SYEN 4399/5399 Example of arrays. May 28,
2003 / include define SIZE 100 int
read_data_and_get_size(float aa, int max_size)
/ or (float , int) / main() int k,
array_size float array_dataSIZE
array_size read_data_and_get_size(array_data,
SIZE) printf("array_size d\n",
array_size) for(k0 kprintf("data_arrayd f\n", k,
array_datak) int read_data_and_get_size(flo
at aa, int max_size) / note a complete code
must care about max_size / int i0
while(scanf("f", aai)!EOF) i return
i
22
- Input 416 43 643 12 678 45 548 44 2390 (press
control D) - Output array_size
9 data_array0 416.000000 data_array1
43.000000 data_array2 643.000000 data_array3
12.000000 data_array4 678.000000 data_array
5 45.000000 data_array6
548.000000 data_array7 44.000000 data_array8
2390.000000
23
  • 7-3 Pointers and Function Arguments
  • 7-3-1 Arguments - Call by Value
  • Fortran or Pascal programmers might feel
    uncomfortable about this aspect of C
  • Fortran call-by-reference
  • Pascal can use var parameters
  • This means that the code inside a function can
    access the original argument, not a local copy
  • C The functions just get a local copy of the
    passed-in arguments
  • Example 1

24
/ SYEN 4399/5399 Example of function Xian Liu,
May 28, 2003 / include int
power(int, int) main() int n, m n6
mpower(2, n) printf("m, n d, d\n", m,
n) int power(int base, int n) / this
function raises to n-th power / int p for
(p1 n0 --n) ppbase return p m,
n 64, 6
25
  • 7-3-2 Array as Arguments
  • The above story is different for arrays
  • When the name of an array is used as an
    argument, the function gets the address of the
    beginning of this array
  • This implies that the code inside the function
    can alter any element of the array
  • We do not specify the dimension of the 1-d array
    when it is a parameter of a function

26
  • Example 2
  • float ff(int size, float list)
  • int i
  • float sum0.0
  • for(i0 i
  • sum sum listi
  • return(sum/size)
  • / If we insert the following code, then the
    original array may be changed
  • list4454
  • list646045765
  • /
Write a Comment
User Comments (0)
About PowerShow.com