c strings
Download
Skip this Video
Download Presentation
C-Strings

Loading in 2 Seconds...

play fullscreen
1 / 8

C-Strings - PowerPoint PPT Presentation


  • 114 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'C-Strings' - adelie


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
c strings

C-Strings

Joe Meehean

c style strings
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’

c style strings1
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;

c style strings2
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
c style strings3
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
c style strings4
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
c style strings5
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
ad