1 / 8

C-Strings

C-Strings. Joe Meehean. C-style Strings. String literals (e.g., “foo”) in C++ are stored as const char[] C-style strings characters (e.g., ‘f’) are stored in an array of chars last char is the NULL character ‘’ or just plain 0 A hold over from C sometimes still used

adelie
Download Presentation

C-Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C-Strings Joe Meehean

  2. C-style Strings • String literals (e.g., “foo”) in C++ are stored as const char[] • C-style strings • characters (e.g., ‘f’) are stored in an array of chars • last char is the NULL character ‘\0’ or just plain 0 • A hold over from C • sometimes still used • important to know how they work char ca1[] = {‘C’, ‘+’, ‘+’}; // NO char ca2[] = {‘C’, ‘+’, ‘+’, ‘\0’}; // YES const char *cp = “C++”; // compiler adds ‘\0’

  3. C-style Strings • Manipulated using (const) char* bool contains(const char* str, char& letter){ const char* p = str; while( *p != letter && *p != NULL ){ p++; } return( *p == letter ); } cout << contains(“Java”, ‘C’) << endl;

  4. C-style Strings • Standard C library provides functions for C-style strings • intstrlen(const char *str) • returns length of str, not including null-terminator • intstrcmp(const char *a, const char* b) • returns 0 if a’s string and b’s string are identical • returns > 0 if a’s string > b’s string • returns < 0 if a’s string < b’s string

  5. C-style Strings • Standard C library provides functions for C-style strings • char* strcat(char *destination, char* source) • appends source to destination • puts results into destination • e.g., strcat(“foo”, “bar”) = “foobar” • better have room in destination for strlen(destination) + strlen(source) + 1 characters • char* strcpy(char* dest, char* source) • copies source into destination • better have room in destination for strlen(source) + 1 characters

  6. C-style Strings • Standard C library provides functions for C-style strings • char* strncat(char *destination, char* source, int n) • same as strcat, but only appends n characters • safer version of strcat • char* strncpy(char* dest, char* source, int n) • same as strcpy, but only copies n characters • safer version of strcpy

  7. C-style Strings • Always use these n versions of string copy and concatenate • using strcpy and strcatcauses many, many security exploits • Always remember the null-terminator • strlen won’t work without it • can then cause strncpy & strncat to be wrong • OR, even better ALWAYS use C++’s string class

  8. Questions?

More Related