Plab - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Plab

Description:

C's 'const' is a qualifier that can be applied to the declaration of any ... Do not confuse what the 'const' declaration 'protects' ! A pointer to a const variable: ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 20
Provided by: NirFri
Category:
Tags: confuse | img | plab

less

Transcript and Presenter's Notes

Title: Plab


1
Plab Tirgul 2Const, C Strings
2
Pointers
  • int main()
  • int i,j
  • int x // x points to an integer
  • i 1
  • x i
  • j x

i
j
x
1
3
Pointers
  • int main()
  • int i,j
  • int x // x points to an integer
  • i 1
  • x i
  • j x

i
j
x
1
0x0100
0x0100
4
Pointers
  • int main()
  • int i,j
  • int x // x points to an integer
  • i 1
  • x i
  • j x

i
j
x
1
0x0100
1
0x0100
5
Cs const
  • Cs const is a qualifier that can be applied
    to the declaration of any variable to specify its
    value will not be changed.
  • const double e 2.71828
  • const char msg Warning msg1
    w // illegal !

6
Cs const
  • Do not confuse what the const declaration
    protects !
  • A pointer to a const variable
  • int const p 1,2,3
  • p1 1 // illegal!
  • (p1) 1 // illegal!
  • p NULL //legal
  • A const pointer to a variable
  • int const const_p 1,2,3
  • const_p1 0 // legal !
  • const_p NULL // illegal!

7
Pointers Syntax
  • Compare
  • (1)int const p 1,2,3//ok
  • (2)const int p 1,2,3//bad style!
  • (3)int const p 1,2,3//ok
  • (2) and (3) are synonyms in C to a pointer to a
    const array.We encourage right to left reading
    of declarations, to achieve better readability
    and avoid errors.

8
Pointers and User Defined Types
  • struct Complex
  • int img
  • int real
  • Complex const comp1 comp2 //ok,initialize
    using comp2
  • comp1.img 3 // illegal ! comp1 value is
    constant
  • All the members of a const variable are immutable
    !

9
Compare to Javas final
  • All (methods as well as data ) are Class members.
  • final makes primitive types constants and
    references to objects constant.
  • The values inside the referred objects are not
    constant !
  • final int LIMIT 10int LIMIT 11// illegal
    ! final MyObject obj1 MyObject()
  • MyObject obj2 NULLMyObject obj3
    MyObject()obj2 obj1//fine, both point now to
    the same objectobj1 obj3 // illegal
    !obj1.setSomeValue(5) // legal !
  • Because All are class members you would
    normally use them as class constants and declare
    them as static final

10
Const Usage
  • The const declaration can (and should !) be used
    in the definition of a functions arguments, to
    indicate it would not change them
  • int strlen(const char )
  • Why use ? (This is not a recommendation but a
    must)
  • Clearer code
  • Avoids errors
  • Part of the interfaces you define!
  • We will see more of const meaning and usage
    when we get to C

11
  • Strings in C

12
String Chars C vs. Java
  • JavaChar is 2 bytes (Unicode).String an
    object which behaves as a primitive type (an
    immutable object, passed by value)
  • CChar usually 1 byte. An Integer.String An
    array of characters.char txt1 text
  • char txt2 textchar txt3
    t,e,x,t,\0

13
C Strings
  • Strings are always terminated by a null
    character, (a character with integer value 0).
  • There is no way to enforce it when you do your
    own strings ?
  • Remember its there
  • Allocate memory for it
  • char text string // means text5 g
    and text6 \0
  • // 7 chars are allocated!

14
C Strings Examples
  • Recall arrays (as learned in Class2) are
    essentially constant pointers.
  • char txt1 textchar txt2
    textint i strlen(txt1) // i 4, same for
    strlen(txt2)txt10 n //nexttxt1
    txt2 // illegal !txt2 txt1
  • //legal. now txt2 points to the same string.
  • if ( ! (strcmp(txt2,next))
  • //This condition is now true

15
C Strings Manipulation
  • To manipulate a single character use the
    functions defined in ctype.h
  • include ltctype.hgt char c A
  • isalpha(c) isupper(c) islower(c) .
  • Manipulation of Strings is done by including the
    string.h header file
  • // copy a string
  • char strcpy(char dest,const char src)
  • // append a string
  • char strcat(char dest,const char src)

16
C Strings Manipulation (2)
  • // compare two strings.
  • //when str1 lt str2 lexicographically return lt 0
  • //when str1 lt str2 lexicographically return gt 0
  • //when identical return 0
  • int strcmp(const char str1, const char str2)
  • // return strings length, not including the \0
    !!
  • size_t strlen(const char str)
  • Other functions
  • strncpy(),strncat(),strcmp()
  • NOTE
  • All C library functions assumes the usages of
    \0 and enough storage space.
  • No boundary checks ! You are responsible.

17
C Strings Functions
  • An array version of strcpy()
  • void strcpy(char dest,const char src)
  • int i 0
  • while ((desti srci) ! \0))
  • i
  • A pointers version of strcpy()
  • void strcpy(char dest,const char src)
  • while ((dest src) ! \0))
  • dest
  • src

18
C Strings Functions (2)
  • An experienced C programmer would write
  • void strcpy(char dest,const char src)
  • while ((dest src) ! \0))
  • Actually the comparison against \0 is
    redundant void strcpy(char dest,const char
    src)
  • while (dest src)
  • Style note Unlike KR book, we do NOT encourage
    you to write such code. However, you should be
    able to read and understand it. The language
    features are used in any case.

19
C Strings Functions
  • What does this do? What are the assumptions ?
  • int f(const char p,const char q)
  • for(p p q p,q)
  • return p-q
Write a Comment
User Comments (0)
About PowerShow.com