80 likes | 91 Views
This module covers the basics of strings and characters in programming, including literals and variables, string declarations, character pointers, and pointer arrays. It also discusses inputting and manipulating strings, as well as command-line arguments. Suitable for beginners in computer science.
E N D
Department of Computer and Information Science,School of Science, IUPUI CSCI N305 Characters and Strings Literals and Variables
Fundamentals of Strings and Characters • 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'
800 731 731 ‘A’ 732 ‘B’ 733 ‘C’ 734 ‘D’ 735 ‘E’ 736 ‘\0’ Character Pointers • String constant acts like a character pointer char *pc = “ABCDE”; /* declare a character pointer variable */ VariableAddressValue constant 731 ‘A’ constant 732 ‘B’ constant 733 ‘C’ constant 734 ‘D’ constant 735 ‘E’ constant 736 ‘\0’ pc 800 731 char s1[] = “abc”; VariableAddressValue s1[0] 900 ‘a’ s1[1] 901 ‘b’ s1[2] 902 ‘c’ s1[3] 903 ‘\0’
104 ‘t’ 105 ‘e’ 106 ‘s’ 107 ‘t’ 108 s1[] ‘\0’ 1000 ‘a’ 1001 ‘b’ 1002 ‘c’ 1003 ‘\0’ s s3[] 1100 1100 2000 2000 2000 ‘a’ ‘a’ s3[0]=‘A’ 2001 2001 ‘b’ ‘b’ 2002 2002 ‘c’ ‘c’ 2003 2003 ‘d’ ‘d’ 2004 2004 ‘e’ ‘e’ Character Pointers 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 */ } Example: char s3[] = “abcdef”; f1() { char *s = s3; *s = ‘A’; /* s3[0]=‘A’ */ s = “test”; printf(“%s\n%s\n”,s,s2); } s2 800 100 100 ‘a’ 101 ‘b’ 102 ‘c’ 103 ‘\0’ ...
Pointer Arrays • Syntax: int *pi[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”}; Const can not be changed
argv pointer array e c h o \0 argc h e l l o \0 3 null w o r l d \0 Command-Line Arguments • argcandargv • 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
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’); } main (int argc, char *argv[]) { while (--argc > 0) printf(“%s%c”, *++argv, (argc > 1) ? ‘ ’ : ‘\n’); } main (int argc, char *argv[]) { while (--argc > 0) printf((argc > 1) ? “%s “ ; “%s\n“, *++argv); }