Characters and Strings Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Literals and Variables
Fundamentals of Strings and Characters Building blocks of programs Every program is a sequence of meaningfully grouped characters Character constant An int value represented as a character in single quotes 'z' represents the integer value of z Strings Series of characters treated as a single unit Can include letters, digits and special characters (*, /, $) String literal (string constant) - written in double quotes "Hello" Strings are arrays of characters String a pointer to first character Value of string is the address of first character
Fundamentals of Strings and Characters String declarations Declare as a character array or a variable of type char * char color[] = "blue"; char *colorPtr = "blue"; Remember that strings represented as character arrays end with '\0' color has 5 elements Inputting strings Use scanf scanf("%s", word); Copies input into word[] Do not need & (because a string is a pointer) Remember to leave room in the array for '\0'
Character Pointers String constant acts like a character pointer char *pc = “ABCDE”; /* declare a character pointer variable */ Variable Address Value constant 731 ‘A’ constant 732 ‘B’ constant 733 ‘C’ constant 734 ‘D’ constant 735 ‘E’ constant 736 ‘\0’ pc 800 731 char s1[] = “abc”; s1[0] 900 ‘a’ s1[1] 901 ‘b’ s1[2] 902 ‘c’ s1[3] 903 ‘\0’ ‘A’ ‘B’ ‘C’ ‘D’ ‘E’ ‘\0’ 731 732 733 734 735 736 800
Character Pointers Example: s2 s1[] s3[] s char s1[] = “abc”; CONSTANT MEMORY AREA (READ ONLY) Example: char s1[] = “abc”; char *s2 = “abc”; f() { s1[1] = ‘y’; /* OK */ s2[1] = ‘y’; /* wrong (PC is OK)*/ s1 = “test”; /* wrong */ s2 = “test”; /* OK */ } char s3[] = “abcdef”; f1() char *s = s3; *s = ‘A’; /* s3[0]=‘A’ */ s = “test”; printf(“%s\n%s\n”,s,s2); s2 800 1100 100 100 ‘a’ s1[] 1000 1001 1002 1003 ‘a’ ‘b’ ‘c’ ‘\0’ 101 ‘b’ 102 ‘c’ 103 ‘\0’ 104 105 106 107 ‘t’ ‘e’ ‘s’ ‘\0’ 108 1100 s3[] 2000 s 2001 2002 2003 ‘a’ ‘b’ ‘c’ ‘d’ 2004 ‘e’ s3[0]=‘A’ 2000 2001 2002 2003 ‘a’ ‘b’ ‘c’ ‘d’ 2004 ‘e’ ...
Pointer Arrays Syntax: int *pi[3]; float *pf[3]; int i=1, j=2, k=3; /* pi[0], pi[1], pi[2] */ float *pf[3]; /* pf[0], pf[1], pf[2] */ Example 1: int i=1, j=2, k=3; int *pi[3] = {&i, &j, &k}; Example 2: char *pc[3]={“ABC”, “DEF”, “GH”}; Variable Address Value constant 90 ‘A’ constant 91 ‘B’ constant 92 ‘C’ constant 93 ‘\0’ constant 94 ‘D’ constant 95 ‘E’ constant 96 ‘F’ constant 97 ‘\0’ constant 98 ‘G’ constant 99 ‘H’ Constant 100 ‘\0’ pc[0] 200 90 pc[1] 202 94 pc[2] 204 98 Variable Address Value i 80 1 j 82 2 k 84 3 pi[0] 100 pi[1] 101 pi[2] 102 Const can not be changed
Command-Line Arguments argc and argv In environments those support C, there is a way to pass command-line arguments or parameters to a program when it begin executing. When main is called to begin execution, it is called with two arguments – argc and argv argc : The first (conventionally called argc) is the number of command-line arguments the program was invoked with argv : The second (conventionally called argv) is a pointer to an array of character strings that contain the arguments, one per string. Example: if echo is a program and executed on unix prompt, such as 10 <user:/home/droberts> echo hello world pointer array argv argc e c h o \0 h e l l o \0 w o r l d \0 null 3
Command-Line Arguments Example: print out the arguments. ex: hello world main (int argc, char *argv[]) { int i; for (i = 1; i < argc; i++) printf(“%s%c”, argv[i], (i < argc-1) ? ‘ ’ : ‘\n’); } while (--argc > 0) printf(“%s%c”, *++argv, (argc > 1) ? ‘ ’ : ‘\n’); printf((argc > 1) ? “%s “ ; “%s\n“, *++argv);