Systems Organization I (Honors) - 9/27/2004

 

 

Announcements: Simple quiz a week from Wednesday, Oct.8th

 

 

 

 

Goal: Understand the following code

 

void strcpy (char *dest, char *src) {

  while (*dest++ = *src++);

}

 

This function copies one string to another string.

 

Strings

 

note: "/* . . . */" is a C-style comment. 

 

char x;  /* declares a variable, x, as a char value */

char x[10]; /* a character array with 10 element */

 

Strings are represented as character arrays, that is as multiple characters with consecutive memory locations.  In C strings are generally followed by a NUL character.

 

Example:

 

            Hello            =>                   H - at memory location 5000

                                                            E at memory location 5001

                                                            L at memory location 5002

                                                            L at memory location 5003

                                                            O at memory location 5004

                                                            NUL at memory location 5005

 

For this example you would need a character array of length 6, char x[6].

 

The address of the first character in the string is used to represent the entire string, so here that is 5000.  A pointer is what is used to refer to this memory location.

 

-       char *x;                  /* pointer to character x */

-       char q = *x;           /* q gets the character at the memory location x */

-       *x = q;                   /* the character stored in variable q is written to location x */

-       x = HELLO;      /* x gets location of H, in the above example that would be 5000 */

-       w = &v;                 /* the & is used to get the memory location of a variable */

 

 

 

 

 

 

 

Example:

 

            char *x;

            char v;

            char w;

           

            v = M;

            w = v;

            x = &v;            /* x gets the memory location of variable v */

            *x = N;         /* write N to the memory location x, which is what variable v points to as well */

            putchar(v);      /* prints value of v to the command line */

           

 

Here  an N is printed.

 

Diagram of example:

 

            char *x;

            char v;

            char w;

 

 

            v = M;

 

 

            w = v;

 

            x = &v;

 

 

 

            *x = N;         /* write N to the memory location stored in x */

 

 

            putchar(v);

 

N will be printed (without quotes).

 

Example:

 

            char *x;

            char y;

            x = AARDVARK;

            x = x + 2;

            y = *x;                         /* y now equals R */

 

When you have a pointer value (*x) and you use the arithmetic operators on it (add, subtract, etc.) it adds or subtracts from the memory location value (in a special way).  The value you use to add or subtract from the pointer is scaled by the compiler according to the size of the type you declared the pointer as.  This means when you add 2 to x above you are actually adding 2 times the size of a char type. 

 

Other ways to do pointer arithmetic:

 

            x += 1;

            ++x;                 /* these three are equivalent */

            x++;

 

            y = *(x+2);      /* these two are equivalent */

            y = x[2];

 

 

 

 

To get the last character of a NUL terminated string:

 

            x = . . . . . . . . . . . . . . . .;                  /* some arbitrarily long string */

            while ( *(x+1) != 0 )

                        x = x + 1;

 

            /* make it more compact */

 

            while ( *(x+1) ) x++;                           /* != 0 is the same as true in C, so it is unnecessary */

 

A function to do this:

 

            int strlen (char *s) {

                        int l = 0;

                        while (*s++) l++;

                        return(l);

            }