Array with base type char • One character per indexed variable • One extra character: '\0' Called ‘null character’ Delimiter of the string To declare a string, declare a char array, e.g. char s[10]; • Declares a c-string variable to hold up to 9 characters • + one null character C-strings
22
Embed
Array with base type char One character per indexed variable One extra character: '\0' Called ‘null character’ Delimiter of the string To declare a string,
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Array with base type char• One character per indexed variable• One extra character: '\0'
Called ‘null character’Delimiter of the string
To declare a string, declare a char array, e.g.
char s[10];
• Declares a c-string variable to hold up to 9 characters
• + one null character
C-strings
String Variable
• Typically ‘partially-filled’ arrayo Declare large enough to hold max-size string,
including the null character.• A standard array:
– If s contains string “Hi Mom!”, stored as:
String Initialization
Can initialize string:
• Need not fill entire array• Initialization places '\0' at end
String Initialization
Can omit array-size:
Automatically makes size one more than length of quoted string
NOT same as:
IS same as:
Accessing elements of a string
• A string IS an array• Can access indexed variables of:
ourString[0] is ‘H’ourString[1] is ‘i’ourString[2] is ‘\0’ourString[3] is unknownourString[4] is unknown
String Index Manipulation
Can manipulate indexed variables
• Be careful!• Here, ‘\0’ (null) was overwritten by a ‘Z’!• If null is overwritten, string no longer ‘acts’ like a
string!• Unpredictable results!
String Input - fgets
char *fgets (char * strPtr, int size, FILE *fp)
– Inputs characters from the specified file pointerthrough \n or until specifed size is reached
– Puts newline (\n) in the string if size not reached!!!
– Appends \0 at the end of the string– If successful, returns the string– Example for stdin:
String Output - fputs
int fputs (const char *strPtr, FILE *fp)• Takes a null-terminated string from memory
and writes it to the specified file pointer• Drops \0• Programmer's responsibility: Make sure the
newline is present at the appropriate place(s)• Example for stdout:
Recall that a “C-string” is an array of characters whose logical end is denoted by a zero valued byte. The C standard library has a number of functions designed to work with C strings.
The strtol() function is one of them. You can see most of them on a Linux system via the command:
/* Copy characters from source to destination */while (*src != '\0') {
*dst = *src;dst++;src++;
}
*dst = *src; /* Copy null character */
return(dststart);
} /* End zstrcpy */
An Implementation of strcpy
It is possible to shrink and obfuscate the code in an attempt to demonstrate ones C language machismo. This approach produces code that is difficult to read, painful to maintain, but may (or may not) produce a trivial improvement in performance . When tempted, just say no!
/* zstrcpy.c */
#include <stdio.h>#include <stdlib.h>
/* Prototype:
Short, less readable version.
char *zstrcpy(char *dst, char *src);
where "dst" is a pointer to the location that the string is to be copied to,"src" is a pointer to the location that the string is copied from.
*/
An alternative implementation
char *zstrcpy(char *dst, char *src){
/* dst - pointer to destination string *//* src - pointer to source string */
char *dststart = dst;
/* Copy characters from source to destination */while (*src++ = *dst++) != '\0') { }
return(dststart);
} /* End zstrcpy */
An alternative implementation
We will explore the use of separately compiled modules later in the semester, but for now we’ll consider some simple cases. The nice thing about functions like zstrcpy() is that theycan be used by multiple programs. For this to be convenient, we do not want to have to include the source code for zstrcpy() in every program that we write that will use it.
/* Prototype statement for zstrcpy() */char *zstrcpy(char *s1, char *s2);
/* Test program */int main() {
char *string1 = "This is a test string";char *string2;
string2 = (char *) malloc(strlen(string1)+1);
zstrcpy(string2, string1);
printf("Copied string: \"%s\"\n", string2);
exit(0);}
Separately compiled modules
Note that this program calls the zstrcpy() function. The “prototype” statement at the head of the main program (highlighted in red) is provided so that the compiler will know what the type of the zstrcpy() function is and what its parameters are.
One way to compile and run this program is the following command line:
gcc –o zstrtest –g –Wall zstrtest.c zstrcpy.c
Note that both source files are specified, which means that both source files will be compiled to build the final executable program “zstrtest”.
Separately compiled modules
Also note in this program the use of the statement
“#include <string.h>”.
This was necessary because the main program calls the string function “strlen()”, and its prototype statement is in the include file “string.h”.
For any given C function you can determine which include files are needed by entering a command, e.g. “man strlen”. This produces the “man” page description of the function which includes the list of include files that must be specified.