Script started on Thu Oct 25 21:56:21 2001
ws05% README.[K[K[K[K[K[K[Kcat README

README

Russell J Lowke
Homework Three 

1.

a) output

1073742608 1073742608 1073742611 1073742611 1073742611
536869634 1 49
abcdefg 1073742608 a 1073742610 c
536869696 536869700


b) table

type            name    address         value

char array   x          536869632   [??? ,??? ,1  ,??? ,??? ,??? ,??? ,??? ,??? ,??? ,???]
char array   y          1073742608  [a   ,b   ,c  ,d   ,e   ,f   ,g   ,NULL,??? ,??? ,???]
int          a          536869696   12
int          b          536869700   1
char ptr     p          536869704   1073742611

Note: ??? is an unknown value


c)
(i)   p[0]     = d
(ii)  *p       = d
(iii) &y[1]    = 1073742609
(iv)  y[1]     = b
(v)   y + 1    = 1073742609
(vi)  *(y + 1) = b


2.
a)a[2];                 3
b)*(a + 2);             3
c)a[4]-a[2]             2
d)a[a[4]-2]             4
e)(a+10)[-8]    3


3.
[1,3,3,4,5,6]
[1,3,6,4,5,6]
[1,3,6,10,5,6]
[1,3,6,10,15,6]


4.
a) *p;                          6
b) p[3];                        3
c) *((p + 3) - 1);      4
d) p[*(p + 2)];         2
e) *(++p);                      5
f) ++*p                         7
g) *p++;                        6


5. What is the bug in the following loop?

        for (p = a ; p < 6 ; p++)
                printf("%d/n", *p);

        ANSWER: the condition in the for loop:  p < 6  will always return false
                        because as p is a pointer address!      thus this loop will not execute.
                        and the compiler warns: "comparison between pointer and integer"
        
                        
6. What do the following statements print?

a) printf("%s\n", s);                           COOKIE
b) printf("%d\n", strlen(s));           6
c) printf("%d\n", strlen(t));           4
d) printf("%c %c\n", s[2], t[0]);       O O
e) printf("%d\n", strlen(t + 1));       3
f) printf("%s\n", ++t);                         KIE


7. What is the bug in the following loop?

        for (i = 0 ; s[i] != '0' ; i++)
                putchar(s[i]);

        ANSWER: a '0' does not terminate the string s.
                        Strings are terminated by NULL.
                        The condition in the for loop should read  s[i] != NULL

                        
8. What does the following print?
                        
        for (t = s ; *t ; t++)
                printf("%s", t);

        ANSWER: COOKIEOOKIEOKIEKIEIEE

        
9. Write two versions of strncpy()
        
        ANSWER array version  :

        char * mStrncpy1(char s1[], char s2[], int n) {
                //
                // strncpy copies exactly n characters from s2 into s1
                // -- array version
        
                int i,                                    // general purpose counter
                    srcLen = strlen(s2);  // length of s2
        
                // loop n times, copying either chars or nulls
                for (i = 0; i < n; i++) {
                        if (i < srcLen)
                                s1[i] = s2[i];
                        else
                                s1[i] = NULL;
                }

                return s1;
        }


        ANSWER pointer version :
        
        char * mStrncpy2(char * s1, char * s2, int n) {
                //
                // strncpy copies exactly n characters from s2 into s1
                // -- pointer version
        
                int i = 0;          // i counts number of characters copied
                char * cpys1 = s1;  // make copy of s1 pointer to return

                // copies from s1 into s2 and checks for n limit
                while( i++ < n && (*s1++ = *s2++) ) ;

                // fill empty space with '\0'
                for(; i < n; s1++, i++) {
                        *s1 = '\0';
                }

                return cpys1;
        }

ws05% cat test.c

#include        <stdio.h>
#include        <ctype.h>
#include    <string.h>

// globals
char y[10];
int i,j,k = 0;                                  // general counters

void printArray(int mArray[], int arraySize) {
        //
        // print contents of array
        //
        // if arraySize = -1 then array terminated
        // with null (string)
        
        printf("[");

        // is null delim.
        if (arraySize == -1) {
                for (arraySize = 0; mArray[arraySize] != NULL; arraySize++) {
                }
        }
                
        for (i = 0; i <= arraySize ; i++) {
                
                if (mArray[i] == NULL)
                        printf("NULL");
                else
                        printf("%d", mArray[i]);
                
                if (i < arraySize)
                        printf(",");
        }

        printf("]");

}

void printCharArray(char mArray[], int arraySize) {
        //
        // print contents of array
        //
        // if arraySize = -1 then array terminated
        // with null (string)
        
        printf("[");

        // is null delim. (p.s. could use strlen!)
        if (arraySize == -1) {
                for (arraySize = 0; mArray[arraySize] != NULL; arraySize++) {
                }
        }
                
        for (i = 0; i <= arraySize ; i++) {
                
                if (mArray[i] == NULL)
                        printf("NULL");
                else
                        printf("%c", mArray[i]);
                
                if (i < arraySize)
                        printf(",");
        }

        printf("]");

}

void initArray(char mArray[], int arraySize) {
        //
        // ensure all values in array are set to NULL
                
        for (i = 0;  i <= arraySize ; i++) {
                mArray[i] = NULL;
        }
}

char * mStrncpy1(char s1[], char s2[], int n) {
        //
        // strncpy copies exactly n characters from s2 into s1
        // -- array version
        
        int i,                                    // general purpose counter
            srcLen = strlen(s2);  // length of s2
        
        // loop n times, copying either chars or nulls
        for (i = 0; i < n; i++) {
                if (i < srcLen)
                        s1[i] = s2[i];
                else
                        s1[i] = NULL;
        }

        return s1;
}


char * mStrncpy2(char * s1, char * s2, int n) {
        //
        // strncpy copies exactly n characters from s2 into s1
        // -- pointer version
        
        int i = 0;          // i counts number of characters copied
        char * cpys1 = s1;  // make copy of s1 pointer to return

        // copies from s1 into s2 and checks for n limit
        while( i++ < n && (*s1++ = *s2++) ) ;

        // fill empty space with '\0'
        for(; i < n; s1++, i++) {
                *s1 = '\0';
        }

        return cpys1;
}


main()
{

        char   x[10];   /* an array of chars */
        int    a,b;     /* a couple of ints  */
        char * p;       /* one pointer       */
        int    mya[] = {1, 2, 3, 4, 5, 6};
        int    myb[] = {6, 5, 4, 3, 2, 1};
        int  * mp;
        char   s[] = "COOKIE";
        char * t = &s[2];
        
        char s1[100] = "abcdefghijklmnopqrstuvwxyz";
        char s2[100] = "12345678901234567890";
        
        // empty arrays
    initArray(x, 10);
    initArray(y, 10);

        x[2] = '1';
        a = 12;

        p = &y[3];
        strcpy(y, "abcdefg");

        printf("%u %u %u %u %u\n", y, &y[0], &y[3], y+3, p);
        printf("%u %c %d\n", x+2, x[2], x[2]);
        printf("%s %u %c %u %c\n", y, y, y[0], p-1, p[-1]);
        printf("%u %u\n", &a, &b);
        

        printf("\n________\n");
        
        printf("(type):char array \tx (address):%u \t(value):", x);
        printCharArray(x, 10);  printf("\n");
        
        printf("(type):char array \ty (address):%u \t(value):", y);
        printCharArray(y, 10);  printf("\n");

        printf("(type):int        \ta (address):%u \t(value):%d\n", &a, a);
        printf("(type):int        \tb (address):%u \t(value):%d\n", &b, b);
        printf("(type):char ptr   \tp (address):%u \t(value):%d\n", &p, p);
        
        printf("\n________\n");

        printf("p[0]     = %c\n", p[0]);
        printf("*p       = %c\n", *p);
        printf("&y[1]    = %u\n", &y[1]);
        printf("y[1]     = %c\n", y[1]);
        printf("y + 1    = %u\n", y+1);
        printf("*(y + 1) = %c\n", *(y + 1));

        printf("________\n");
        
        printArray(mya, 5);     printf("\n");
        
        printf("________\n");

        printf("a) a[2]        %d\n", mya[2]);
        printf("b) *(a + 2)    %d\n", *(mya + 2));
        printf("c) a[4] - a[2] %d\n", (mya[4]-mya[2]));
        printf("d) a[a[4]-2]   %d\n", mya[mya[4]-2]);
        printf("e) (a+10)[-8]  %d\n", (mya+10)[-8]);

        printf("________\n");

        for (j = 0; mya[j] <= 2*mya[j+1]; j++) {
                mya[j+1] += mya[j];
                printArray(mya, 5);     printf("\n");
        }

        printf("________\n");

        printArray(myb, 5);     printf("\n");

        printf("________\n");

        //      int myb[] = {6, 5, 4, 3, 2, 1};

        mp = myb;
        printf("a) *p;             %d\n", *mp);
        printf("b) p[3];           %d\n", mp[3]);
        printf("c) *((p + 3) - 1); %d\n", *((mp + 3) - 1));
        printf("d) p[*(p + 2)];    %d\n", mp[*(mp + 2)]);

        mp = myb;
        printf("e) *(++p);         %d\n", *(++mp));
        
        mp = myb;
        printf("f) ++*p            %d\n", ++*mp);

        // undo previous statement!
        --*mp;
        
        mp = myb;
        
        printf("g) *p++;           %d\n", *mp++);
        printf("!) *p;             %d\n", *mp);

        printf("________\n");
        
        /*
        for (mp = myb ; mp < 6 ; mp++)
                printf("!!! %d/n", *mp);
        */

        printf("XXX\n");
        printf("________\n");
        
        printf("%s\n", s);
        printf("%d\n", strlen(s));
        printf("%d\n", strlen(t));
        printf("%c %c\n", s[2], t[0]);
        printf("%d\n", strlen(t + 1));
        printf("%s\n", ++t);
        printf("________\n");

        for (i = 0 ; s[i] != NULL; i++)
                printf("%c\n", s[i]);
        
        //      putchar(s[i]);
        printf("________\n");

        for (t = s ; *t ; t++)
                printf("%s", t);

        printf("________\n");

        mStrncpy2(s1, s2, 24);
        printCharArray(s1, 30);         printf("\n");
        printCharArray(s2, 30);     printf("\n");
}


ws05% ./test

1073743152 1073743152 1073743155 1073743155 1073743155
536869154 1 49
abcdefg 1073743152 a 1073743154 c
536869216 536869220

________
(type):char array       x (address):536869152   (value):[NULL,NULL,1,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL]
(type):char array       y (address):1073743152  (value):[a,b,c,d,e,f,g,NULL,NULL,NULL,NULL]
(type):int              a (address):536869216   (value):12
(type):int              b (address):536869220   (value):1023
(type):char ptr         p (address):536869224   (value):1073743155

________
p[0]     = d
*p       = d
&y[1]    = 1073743153
y[1]     = b
y + 1    = 1073743153
*(y + 1) = b
________
[1,2,3,4,5,6]
________
a) a[2]        3
b) *(a + 2)    3
c) a[4] - a[2] 2
d) a[a[4]-2]   4
e) (a+10)[-8]  3
________
[1,3,3,4,5,6]
[1,3,6,4,5,6]
[1,3,6,10,5,6]
[1,3,6,10,15,6]
________
[6,5,4,3,2,1]
________
a) *p;             6
b) p[3];           3
c) *((p + 3) - 1); 4
d) p[*(p + 2)];    2
e) *(++p);         5
f) ++*p            7
g) *p++;           6
!) *p;             5
________
XXX
________
COOKIE
6
4
O O
3
KIE
________
C
O
O
K
I
E
________
COOKIEOOKIEOKIEKIEIEE________
[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,NULL,NULL,NULL,NULL,y,z,NULL,NULL,NULL,NULL,NULL]
[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL]
ws05% exit

exit

script done on Thu Oct 25 21:56:49 2001
