CMPUT 201: Lab 3 - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

CMPUT 201: Lab 3

Description:

Often it's necessary to provide a program parameters (such as names of files to ... A modified version of the main' function uses two special variables argc' and ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 10
Provided by: nla5
Category:
Tags: cmput | lab | style

less

Transcript and Presenter's Notes

Title: CMPUT 201: Lab 3


1
CMPUT 201 Lab 3
C-style Strings and Debugging
October 3rd, 2002
2
Command Line Arguments
  • Often its necessary to provide a program
    parameters (such as names of files to use) from
    the command line
  • This is done with command line arguments
  • programName arg1 arg2 arg3 ...
  • A modified version of the main function uses
    two special variables argc and argv to access
    command line arguments

3
Command Line Arguments
  • int main( int argc, char argv ) ... if
    (argc gt 1) cout ltlt Program name ltlt
    argv0 ltlt endl
  • argc contains the number of parameters, which
    includes the program name (so in a program called
    with one option, argc 2)
  • argv contains an array of strings that holds
    the arguments (so argv0 is the program name,
    argv1 the first parameter and so on)

4
C-style Strings
  • In C there is no built-in string class strings
    are implemented simply as character arrays
  • These arrays contain the characters in the
    string followed by a null byte that is
    represented as \0
  • \0 is an escape sequence like \n (the
    newline character) that marks the end of the
    string
  • C string manipulation functions depend on the
    presence of \0 since this tells them when to
    stop reading the string

5
C-style Strings
  • So a strings size is always one more than the
    number of text characters it contains
  • char str hello
  • This string is 6 bytes long and is stored as
  • str0 str1 str2 str3 str4 str5 h
    e l l o \0
  • The above declaration is equivalent to
  • char str6 hello
  • since this string is 6 bytes long

6
C String Pitfalls
  • You can not use a C string variable the same
    way as other variables in assignment or
    comparison expressions
  • You can use the assignment operator after a
    string only in initialization
  • // this is acceptablechar myString20 alpha
    beta
  • // this is illegalmyString gamma
  • You can not use the comparison operator to
    test if two C strings are equal

7
Built-in String Functions
  • The following functions are included in the
    library cstring
  • Function Description
  • strlen(str) Returns the length of str
    without \0
  • strcmp(str1, Compares the two strings
    alphabetically str2) Returns
    zero (0) if they are equal, non-
    zero otherwise
  • strcpy(dest, Puts the contents of source
    in dest source) source remains
    unchanged

8
Built-in String Functions
  • Function Description
  • strcat(target, Concatenates target and
    source and source) places the
    result in target
  • strstr(ref, Returns the position of the
    first search) occurrence of search
    in ref
  • To use any of these functions, your program
    must include the cstring library
  • include ltcstringgt // should be at the top of
    your program

9
Debugging
  • Many interactive debugging tools exist for
    Unix we will use Data Display Debugger (DDD)
  • You must compile all the modules (the .cpp
    files) with the g flag in order for DDD to
    understand how to debug your application
  • To debug an application, type
  • ddd tokenize1
  • The debugger window then opens with a display
    of the module containing the applications main
    function
Write a Comment
User Comments (0)
About PowerShow.com