Skip to content

Instantly share code, notes, and snippets.

@Theartbug
Created January 18, 2020 00:02
Show Gist options
  • Save Theartbug/d836382285873bbde05541360eac79e7 to your computer and use it in GitHub Desktop.
Save Theartbug/d836382285873bbde05541360eac79e7 to your computer and use it in GitHub Desktop.
C++ strings / arrays

Arrays

Strings

  • Strings are represented by arrays of characters in this course
    • also with the String class, but not in this class
  • size of array must be specified at time of creation (constant or literal)
    • must be known at compile time, can not be a variable brought in by cin, etc.
    • can copy elements into a bigger array
    • must settle on a max length (defined array size)
    • wherever the string ends, there will be a null character \0
    • must create an array that is +1 longer than desired max
  • NULL / \0 character happens to coincide with 0 in the ASCII system
  • ex: char str[5]
    • an array called str that holds 5 characters
    • on the stack, not in the heap, will get recycled when the current function exists
  • can read in a string, but is dangerous
    • cin >> str
    • will skip leading whitespace
    • will continue reading until a whitespace is hit, then stops reading and enters \0
    • whitespace is left in the stream
  • buffer overflow error
    • if we read in a string that is larger than our 5 character limit
    • will place the following overflowing characters in another place in memory
    • can possibly overwrite another variable
    • no error is generated!
  • no array bounds checked
    • will let you check for things in memory beyond what you allocated for the array
    • str[7] may return something belonging to some other else!

cin

  • use cin.get(arrayToPlaceInto, arraySize, characterToStopAt) instead
    • does not read in the characterToStopAt, have to manually eat it
    • will safely read in a certain number of characters to arraySize - 1 (so there is space for the null character \0)
    • ex: cin.get(str, 5, '\n')
      • will stop at and leave /n in the stream, will have to deal with that in the buffer
    • will assume \n if third parameter is left out, not required in function
    • cin.get() will get a single character and consume
      • equivalent to cin >>
  • use cin.peek() to look at the next character in the buffer without consuming characters from buffer
  • cin.get() is lower level, does not ignore whitespace (will stop at not consume) cin >> does (consumes)
  • homework: cin.get() first character, cin.peek() at second character for '+' / '-'

flushing the stream

  • while(cin.get() != '\n') {} will read in all characters until a newline is reached
    • but also reads in the newline (consumes)
  • displaying strings is not as difficult
  • cout << will print out characters continuously until a \0 is reached
    • if does not exist, will start to read out random memory
    • very dangerous!

string operators

  • very few operations can be preformed on arrays of characters
  • cannot compare
    • an array is the address is the first element in memory (a memory address)
  • == or != simply be comparing memory locations
    • even in contents are the same
  • strcmp(firstArray, secondArray) compares strings
    • 0 if equal
    • < 0 if first is less than second
    • 0 if first is greater than second

    • greater and less means total of character ASCII values
    • always compare in this manner: if (strcmp(srt1, str1) < 0)
  • strcpy(str1, str2)
    • copies values in memory over, creates a new string in memory
    • will copy until the first \0 is hit

working with char arrays

  • use for-loops as an alternative to cin.get()
  • requires placing \0 manually
  • must define a max character limit for the loop to not go past

I/O, conditionals, loops, arrays

conditionals

  • may use any kind of value in the expression
    • will check for zero or nonzero (checking the numeric value)

helpers

  • tolower() comes from the <ctype> library
  • toupper() comes from the <std> library

loops

  • prefix ++i instead of postfix i++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment