Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,

Similar presentations


Presentation on theme: "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,"— Presentation transcript:

1 Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

2 Dale Roberts 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

3 Dale Roberts 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'

4 Dale Roberts Character Pointers String constant acts like a character pointer char *pc = “ABCDE”; /* declare a character pointer variable */ Variable AddressValue constant731‘A’ constant732‘B’ constant733‘C’ constant734‘D’ constant735‘E’ constant736‘\0’ pc800731 char s1[] = “abc”; Variable AddressValue 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 731800

5 Dale Roberts 1100 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);} Character Pointers ‘a’ ‘b’ ‘c’ ‘\0’ 100 101 102 103 CONSTANT MEMORY AREA (READ ONLY) 104 105 106 107 ‘t’ ‘e’ ‘s’ ‘t’ ‘\0’ 108 s1[] 1000 1001 1002 1003 ‘a’ ‘b’ ‘c’ ‘\0’100800 s2... 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’

6 Dale Roberts VariableAddressValue constant90‘A’ constant90‘A’ constant91‘B’ constant92‘C’ constant93‘\0’ constant94‘D’ constant95‘E’ constant96‘F’ constant97‘\0’ constant98‘G’ constant99‘H’ Constant 100‘\0’ pc[0] 20090 pc[1] 202 94 pc[2] 204 98 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}; Const can not be changedVariableAddressValue i801 j822 k843 pi[0]10080 pi[1]10182 pi[2]10284 Example 2: char *pc[3]={“ABC”, “DEF”, “GH”};

7 Dale Roberts 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 phoenix prompt, such as if echo is a program and executed on phoenix prompt, such as 10 echo hello world 10 echo hello world Command-Line Arguments pointer array argv argc e c h o \0 h e l l o \0 w o r l d \0 null 3

8 Dale Roberts Command-Line Arguments Example: print out the arguments. ex: hello world main (int argc, char *argv[]) { int i; int i; for (i = 1; i < argc; i++) for (i = 1; i < argc; i++) printf(“%s%c”, argv[i], (i < argc-1) ? ‘ ’ : ‘\n’); printf(“%s%c”, argv[i], (i < argc-1) ? ‘ ’ : ‘\n’);} main (int argc, char *argv[]) { while (--argc > 0) while (--argc > 0) printf(“%s%c”, *++argv, (argc > 1) ? ‘ ’ : ‘\n’); printf(“%s%c”, *++argv, (argc > 1) ? ‘ ’ : ‘\n’);} main (int argc, char *argv[]) { while (--argc > 0) while (--argc > 0) printf((argc > 1) ? “%s “ ; “%s\n“, *++argv); printf((argc > 1) ? “%s “ ; “%s\n“, *++argv);}


Download ppt "Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,"

Similar presentations


Ads by Google