Docstoc

t14 ACharacters And String Variables

Document Sample
t14 ACharacters And String Variables Powered By Docstoc
					                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




                                           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

                           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'

                         Dale Roberts
             Character Pointers
String constant acts like a character pointer
char *pc = “ABCDE”;   /* declare a character pointer variable */

Variable        Address         Value      800 731      731 ‘A’
constant        731             ‘A’                     732 ‘B’
constant        732             ‘B’                     733 ‘C’
constant        733             ‘C’
constant        734             ‘D’                     734 ‘D’
constant        735             ‘E’                     735 ‘E’
constant        736             ‘\0’
pc              800             731                     736 ‘\0’




char s1[] = “abc”;
Variable        Address         Value
s1[0]           900             ‘a’
s1[1]           901             ‘b’
s1[2]           902             ‘c’
s1[3]           903             ‘\0’


                      Dale Roberts
              Character Pointers
                                                                 CONSTANT MEMORY
Example:                                            s2           AREA (READ ONLY)

                                                   100
                                              800 1100                  100 ‘a’
   char s1[] = “abc”;
   char *s2 = “abc”;                                s1[]                101 ‘b’
                                             1000    ‘a’                102 ‘c’
   f()
                                             1001    ‘b’
   {                                                                    103 ‘\0’
     s1[1] = ‘y’;   /* OK */               1002 ‘c’
     s2[1] = ‘y’;    /* wrong (PC is OK)*/ 1003                        104 ‘t’
     s1 = “test”;    /* wrong */                ‘\0’
                                                                       105 ‘e’
     s2 = “test”;   /* OK */
   }                                                                   106 ‘s’
Example:                                                               107 ‘t’
   char s3[] = “abcdef”;                                               108 ‘\0’

   f1()                       s           s3[]                        2000
                             1100
                             2000     2000                                   ‘a’
   {                                         ‘a’ s3[0]=‘A’            2001
                                      2001                                   ‘b’
     char *s = s3;                           ‘b’                      2002   ‘c’
     *s = ‘A’; /* s3[0]=‘A’ */        2002   ‘c’                      2003   ‘d’
     s = “test”;                      2003   ‘d’                      2004
     printf(“%s\n%s\n”,s,s2);         2004                                   ‘e’
                                             ‘e’
   }

                           Dale Roberts                    ...
                          Pointer Arrays
Syntax:
int *pi[3];
/* pi[0], pi[1], pi[2] */
float *pf[3];
/* pf[0], pf[1], pf[2] */

Example 1:                               Example 2:
int i=1, j=2, k=3;                       char *pc[3]={“ABC”, “DEF”, “GH”};
int *pi[3] = {&i, &j, &k};
                                                   Variable     Address     Value
                                                   constant            90    ‘A’
     Variable   Address     Value                  constant            91    ‘B’
                  80         1                     constant            92    ‘C’
        i                                          constant            93    ‘\0’
                                                   constant   Const    94    ‘D’
        j         82         2
                                                   constant   can not 95     ‘E’
                  84         3                     constant            96    ‘F’
        k                                          constant
                                                              be
                                                                       97    ‘\0’
                                                   constant   changed 98     ‘G’
      pi[0]      100         80
                                                   constant            99    ‘H’
      pi[1]      101         82                    Constant          100     ‘\0’
                                                   pc[0]              200    90
      pi[2]      102         84                    pc[1]              202    94
                                                   pc[2]              204    98

                                    Dale Roberts
           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 phoenix prompt, such as
      10 <phoenix:/home/droberts> echo hello world
                  argv      pointer array
                                             e c h o \0
                  argc                       h e l l o \0
                    3
                                null         w o r l d \0


                              Dale Roberts
    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);
}


                     Dale Roberts

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:5
posted:5/29/2012
language:English
pages:8
waheed anjum waheed anjum http://
About