Showing posts with label Languages. Show all posts
Showing posts with label Languages. Show all posts

Saturday, 8 August 2015

Let us dive into part four of our lesson on pointers and strings. You can revise the previous three parts here, here and here.

Fundamentals of Characters and Strings

          Character constant
        Integer value represented as character in single quotes
        'z' is integer value of z
          122 in ASCII

          String
        Series of characters treated as single unit
        Can include letters, digits, special characters  +, -, * ...
        String literal (string constants)
          Enclosed in double quotes, for example:
                                            "I like C++"
        Array of characters, ends with null character '\0'
        String is constant pointer
          Pointer to string’s first character
        Like arrays

          String assignment
        Character array
          char color[] = "blue";
        Creates 5 element char array color
        last element is '\0'
        Variable of type char *
          char *colorPtr = "blue";
        Creates pointer colorPtr to letter b in string “blue”
        “blue” somewhere in memory
        Alternative for character array
          char color[] = { ‘b’, ‘l’, ‘u’, ‘e’, ‘\0’ };

          Reading strings
        Assign input to character array word[ 20 ]
                               cin >> word 
          Reads characters until whitespace or EOF
          String could exceed array size
                                            cin >> setw( 20 ) >> word;
          Reads 19 characters (space reserved for '\0')

          cin.getline
          Read line of text
          cin.getline( array, size, delimiter );
          Copies input into specified array until either
          One less than size is reached
          delimiter character is input
          Example
                             char sentence[ 80 ];
                             cin.getline( sentence, 80, '\n' );

String Manipulation Functions of String-handling Library

          String handling library <cstring> provides functions to
        Manipulate string data
        Compare strings
        Search strings for characters and other strings
        Tokenize strings (separate strings into logical pieces)





          Copying strings
        char *strcpy( char *s1, const char *s2 )
          Copies second argument into first argument
          First argument must be large enough to store string and terminating null character
        char *strncpy( char *s1, const char *s2,   size_t n )
          Specifies number of characters to be copied from string into array
          Does not necessarily copy terminating null character




          Concatenating strings
        char *strcat( char *s1, const char *s2 )
          Appends second argument to first argument
          First character of second argument replaces null character terminating first argument
          Ensure first argument large enough to store concatenated result and null character
        char *strncat( char *s1, const char *s2, size_t n )
          Appends specified number of characters from second argument to first argument
          Appends terminating null character to result



          Comparing strings
        Characters represented as numeric codes
          Strings compared using numeric codes
        Character codes / character sets
          ASCII
          “American Standard Code for Information Interchange”
          EBCDIC
          “Extended Binary Coded Decimal Interchange Code”

          Comparing strings
        int strcmp( const char *s1, const char *s2 )
          Compares character by character
          Returns
        Zero if strings equal
        Negative value if first string less than second string
        Positive value if first string greater than second string
        int strncmp( const char *s1,
                                                                 const char *s2, size_t n )
          Compares up to specified number of characters
          Stops comparing if reaches null character in one of arguments



          Tokenizing
          Breaking strings into tokens, separated by delimiting characters
          Tokens usually logical units, such as words (separated by spaces)
          "This is my string"  has 4 word tokens (separated by spaces)
          char *strtok( char *s1, const char *s2 )
          Multiple calls required
          First call contains two arguments, string to be tokenized and string containing delimiting characters
          Finds next delimiting character and replaces with null character
          Subsequent calls continue tokenizing
          Call with first argument NULL





          Determining string lengths
          size_t strlen( const char *s )
          Returns number of characters in string
          Terminating null character not included in length


Pointers and Strings: Part Four

Monday, 3 August 2015

Last time we started an important topic in C++, pointers. Today, we shall continue from that.

Using 'const' with Pointers

          const qualifier
        Value of variable should not be modified
        const used when function does not need to change a variable
          Principle of least privilege
        Award function enough access to accomplish task, but no more
          Four ways to pass pointer to function
        Nonconstant pointer to nonconstant data
          Highest amount of access
        Nonconstant pointer to constant data
        Constant pointer to nonconstant data
        Constant pointer to constant data
          Least amount of access









          const pointers
        Always point to same memory location
        Default for array name
                             Must be initialized when declared



'sizeof' Operator

sizeof
–Unary operator returns size of operand in bytes
–For arrays, sizeof returns
( size of 1 element ) * ( number of elements )
–If sizeof( int ) = 4, then
int myArray[10];
cout << sizeof(myArray);
     will print 40
sizeof can be used with
–Variable names
–Type names
–Constant values






Pointers and Strings: Part Two

Sunday, 2 August 2015

Previously we completed the concept of Arrays (parts one, two  three and four), today we will discuss the basics of Pointers.

Pointer Variable Declarations and Initialization

•      Pointer variables:
     Contain memory addresses as values
     Normally, variable contains specific value (direct reference)
     Pointers contain address of variable that has specific value (indirect reference)
      Indirection
     Referencing value through pointer
      Pointer declarations
     * indicates variable is pointer
int *myPtr;
     declares pointer to int, pointer of type int *
     Multiple pointers require multiple asterisks
int *myPtr1, *myPtr2;
      Can declare pointers to any data type
      Pointer initialization
     Initialized to 0, NULL, or address
        0 or NULL points to nothing, and is called ‘Null Pointer’

Pointer Operators:

& (address operator)
–Returns memory address of its operand
–Example
  int y = 5;
int *yPtr;
yPtr = &y;    // yPtr gets address of y

yPtr “points to” y
* (indirection/dereferencing operator)
*yPtr returns y (because yPtr points to y).
*yptr = 9;      // assigns 9 to y
     cout<<*yptr
     cin>>*yptr;
*  and & are inverses of each other

Calling Functions by Reference

          3 ways to pass arguments to function
        Pass-by-value
        Pass-by-reference with reference arguments
        Pass-by-reference with pointer arguments
          Pass-by-reference with pointer arguments
        Use pointers and indirection/dereferencing operator
        Pass address of argument using & operator
        * operator used as alias/nickname for variable inside of function






Pointer & Strings

 
Codexify © 2015 - Designed by Templateism.com