1 of 22 - PowerPoint PPT Presentation

About This Presentation
Title:

1 of 22

Description:

CSA2090: Systems Programming Introduction to C Lecture 5: Pointers and Strings Dr. Christopher Staff Department of Computer Science & AI University of Malta – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 23
Provided by: ChrisS186
Category:

less

Transcript and Presenter's Notes

Title: 1 of 22


1
CSA2090Systems ProgrammingIntroduction to C
Lecture 5 Pointers and Strings
  • Dr. Christopher Staff
  • Department of Computer Science AI
  • University of Malta

2
Aims and Objectives
  • More about pointers
  • Traversing arrays using pointers
  • Pointer arithmetic
  • Strings

3
Pointer types
  • Pointers are not just memory addresses
  • They are also variables, so they have types
  • int p, p is a pointer-to-int
  • char c, c is a pointer-to-char
  • struct person fred, fred is a pointer-to-struct-p
    erson

4
Pointer sizes
  • Pointers are always 4 bytes
  • depending on the system architecture
  • they must be large enough to address the largest
    supported memory address
  • But the size of the region that they point to
    depends on the size of the type of the data they
    point to

5
Pointer sizes
6
Pointer sizes
  • If fred is struct person , how many bytes in
    memory does fred point to?
  • struct person
  • int age
  • int height
  • char surname20
  • fred

7
Pointer arithmetic
  • So C knows how many bytes are occupied by the
    data pointed at by a pointer
  • Useful especially when arrays are traversed using
    pointers
  • More later

8
Pointers and Arrays
  • ptr ptr 1
  • If ptr is a pointer-to-char then ptr will
    increase by just one byte
  • But if ptr is a pointer-to-int, then ptr will
    increase by 4 bytes

9
Pointers and Arrays
  • int numbers10, i
  • int iptr numbers0
  • numbers01
  • numbers12
  • numbers23
  • for (i0 ilt3 i)
  • printf(iptr is d\n, iptr)
  • iptriptr1

10
Pointers and Structures
  • struct person fred
  • fred.age
  • struct person fred
  • (fred).age
  • (fred).age becomes easy to make mistakes with,
    especially when member is a pointer
  • fred-gtage

11
Pointers and Functions
  • Lets say that we want to create a variable in a
    calling function, and pass it to a called
    function so that we can store some result in it
  • See func.c, func_ptr1.c, func_ptr2.c

12
Strings
  • In C a string is just an array of characters,
    with a NULL or zero byte terminating the string
  • String handling functions in string.h expect to
    find trailing \0

13
Strings
  • char str110
  • char str2 hello // \0 added by C
  • char str3 // uninitialised
  • str1 goodbye // illegal!!!
  • strcpy(str1, goodbye)
  • But C doesnt do array bounds checking!

14
Pointers and Strings
  • What will happen?
  • strcpy(str3, horror)

15
Pointers and Strings
  • What will happen?
  • strcpy(str3, horror)
  • Because str3 was not initialised, it will contain
    garbage data
  • which C will attempt to interpret as an
    address
  • and try to store string at that location

16
Pointers and Strings
  • This can result in
  • Trying to access protected memory segmentation
    fault
  • Trying to access unaddressable memory, e.g. into
    the middle of a byte or byte 0 bus error
  • Appearing to be successful, but then another
    process legally overwrites the data

17
Initialising pointers
  • Always initialise a pointer
  • If no initial value, then use NULL
  • char cptr NULL
  • And then always test for NULL pointer before
    retrieving values via the pointer!
  • if (!cptr)
  • printf(Error occurred)

18
Pointer Arithmetic
  • char c hello
  • char cptr c
  • These statements are equivalent
  • c cptr c0
  • c3 cptr3 (cptr3) (c3)
  • C treats c as a pointer to an array!
  • You cannot pass by value an array to a function
  • You cannot return an array from a function!

19
Passing arrays to functions
  • char c hello
  • printstring(c)
  • void printstring(char c)
  • \\ OR
  • void printstring(char c)
  • Some caveats for using char see Love, 10

20
Pointers-to-pointers-to
  • A character string is really a pointer to an
    array of characters
  • But what if we want a two dimensional array?
  • an array of strings, for instance
  • char c OR char c

21
Differences
  • Can initialise char c dog, cat,
    horse, bat
  • Cannot initialise char c in the same way!
  • Why?

22
Next week
  • Lab sessions for exercise 2 in Love
  • Attempt Exercises 11.1 to 11.5 at home
  • First part of lab session will cover any problems
    with these, and then attempt 11.6 to 11.8 in lab
  • After Lab session, read Love, 12 on your own
Write a Comment
User Comments (0)
About PowerShow.com