t14acharactersandstringvariables.pps
TRANSCRIPT
Dale Roberts
Department of Computer and Information Science,Department of Computer and Information Science,School of Science, IUPUISchool of Science, IUPUI
CSCI 230
Characters and Strings Literals and Variables
Dale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected]
Dale Roberts
Fundamentals of Strings and CharactersFundamentals of Strings and Characters
CharactersCharactersBuilding blocks of programsBuilding blocks of programs
Every program is a sequence of meaningfully grouped charactersEvery program is a sequence of meaningfully grouped characters
Character constantCharacter constantAn An intint value represented as a character in single quotes value represented as a character in single quotes'z''z' represents the integer value of represents the integer value of zz
StringsStringsSeries of characters treated as a single unitSeries of characters treated as a single unit
Can include letters, digits and special characters (Can include letters, digits and special characters (**, , //, , $$))
String literal (string constant) - written in double quotesString literal (string constant) - written in double quotes"Hello""Hello"
Strings are arrays of charactersStrings are arrays of charactersString a pointer to first characterString a pointer to first characterValue of string is the address of first characterValue of string is the address of first character
Dale Roberts
Fundamentals of Strings and CharactersFundamentals of Strings and Characters
String declarationsString declarationsDeclare as a character array or a variable of type Declare as a character array or a variable of type char *char *
char color[] = "blue";char color[] = "blue";
char *colorPtr = "blue";char *colorPtr = "blue";
Remember that strings represented as character arrays Remember that strings represented as character arrays end with end with '\0''\0'
colorcolor has has 55 elements elements
Inputting stringsInputting stringsUse Use scanfscanf
scanf("%s", word);scanf("%s", word);
Copies input into Copies input into word[]word[]
Do not need Do not need && (because a string is a pointer) (because a string is a pointer)
Remember to leave room in the array for Remember to leave room in the array for '\0''\0'
Dale Roberts
Character PointersCharacter Pointers
String constant acts like a character pointerString constant acts like a character pointerchar *pc = “ABCDE”; /* char *pc = “ABCDE”; /* declare a character pointer variabledeclare a character pointer variable */ */
VariableVariable AddressAddress ValueValueconstantconstant 731731 ‘A’‘A’constantconstant 732732 ‘B’‘B’constantconstant 733733 ‘C’‘C’constantconstant 734734 ‘D’‘D’constantconstant 735735 ‘E’‘E’constantconstant 736736 ‘\0’‘\0’pcpc 800800 731731
char s1[] = “abc”;char s1[] = “abc”;
VariableVariable AddressAddress ValueValues1[0] s1[0] 900900 ‘a’‘a’s1[1] s1[1] 901 901 ‘b’‘b’s1[2] s1[2] 902 902 ‘c’‘c’s1[3] s1[3] 903 903 ‘\0’‘\0’
‘A’
‘B’
‘C’
‘D’
‘E’
‘\0’
731
732
733
734
735
736
731800
Dale Roberts
1100
ExampleExample::
char s1[] = “abc”;char s1[] = “abc”;char *s2 = “abc”;char *s2 = “abc”;
f()f(){{s1[1] = ‘y’; /* OK */s1[1] = ‘y’; /* OK */s2[1] = ‘y’;s2[1] = ‘y’; /* wrong (PC is OK)*/ /* wrong (PC is OK)*/s1 = “test”;s1 = “test”; /* wrong */ /* wrong */s2 = “test”; /* OK */s2 = “test”; /* OK */
}}ExampleExample::
char s3[] = “abcdef”;char s3[] = “abcdef”;
f1()f1(){{char *s = s3;char *s = s3;*s = ‘A’; /* s3[0]=‘A’ */*s = ‘A’; /* s3[0]=‘A’ */s = “test”;s = “test”;printf(“%s\n%s\n”,s,s2);printf(“%s\n%s\n”,s,s2);
}}
Character PointersCharacter 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
s2000
2001
2002
2003
‘a’
‘b’
‘c’
‘d’2004 ‘e’
s3[0]=‘A’2000
2001
2002
2003
‘a’
‘b’
‘c’
‘d’2004 ‘e’
Dale Roberts
VariableVariable AddressAddress ValueValue
constantconstant 9090 ‘A’‘A’constantconstant 9191 ‘B’‘B’constantconstant 9292 ‘C’‘C’constantconstant 9393 ‘\0’‘\0’constantconstant 9494 ‘D’‘D’constantconstant 9595 ‘E’‘E’constantconstant 9696 ‘F’‘F’constantconstant 9797 ‘\0’‘\0’constantconstant 9898 ‘G’‘G’constantconstant 9999 ‘H’‘H’Constant 100Constant 100 ‘\0’‘\0’pc[0] pc[0] 200 200 9090pc[1] pc[1] 202 202 9494pc[2] pc[2] 204 204 9898
Pointer ArraysPointer ArraysSyntax:Syntax:int *pi[3]; int *pi[3]; /* pi[0], pi[1], pi[2] *//* pi[0], pi[1], pi[2] */
float *pf[3]; float *pf[3]; /* pf[0], pf[1], pf[2] *//* pf[0], pf[1], pf[2] */
Example 1Example 1::
int i=1, j=2, k=3; int i=1, j=2, k=3;
int *pi[3] = {&i, &j, &k};int *pi[3] = {&i, &j, &k};
Constcan not bechanged
VariableVariable AddressAddress ValueValue
ii 8080 11
jj 8282 22
kk 8484 33
pi[0]pi[0] 100100 8080
pi[1]pi[1] 101101 8282
pi[2]pi[2] 102102 8484
Example 2:
char *pc[3]={“ABC”, “DEF”, “GH”};
Dale Roberts
argcargc andand argvargv In environments those support C, there is a way to pass In environments those support C, there is a way to pass command-command-line argumentsline arguments or parameters to a program when it begin executing. or parameters to a program when it begin executing.
When main is called to begin execution, it is called with two When main is called to begin execution, it is called with two arguments – arguments – argcargc and and argvargv
argcargc : The first (conventionally called : The first (conventionally called argcargc) is the number of command-) is the number of command-line arguments the program was invoked withline arguments the program was invoked with
argvargv : The second (conventionally called : The second (conventionally called argvargv) is a pointer to an array ) is a pointer to an array of character strings that contain the arguments, one per string.of character strings that contain the arguments, one per string.
ExampleExample::
if if echoecho is a program and executed on phoenix prompt, such as is a program and executed on phoenix prompt, such as 1010 <phoenix:/home/droberts> <phoenix:/home/droberts> echo hello worldecho hello world
Command-Line ArgumentsCommand-Line Arguments
pointer arrayargv
argc
e c h o \0
h e l l o \0
w o r l d \0null3
Dale Roberts
Command-Line ArgumentsCommand-Line Arguments
ExampleExample: print out the arguments. ex: : print out the arguments. ex: hello worldhello worldmain (int argc, char *argv[])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[])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[])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);}}