Problem Set Answers: C Pointers and Arrays

 

  1.    #include <stdio.h>
       void alter(int *, int *)
       main()
       {
          int x,y;
          printf("Enter two integers:  ");
          scanf("%d", &x);
          scanf("%d", &y);
          printf("x = %d  x = %d\n",x,y);
          alter(&x,&y);
          printf("x = %d  x = %d\n",x,y);
       }
       void alter(int *px, int *py)
       {
          *px = *px + *py;
          *py = *px * *py;
       }
    
  2.    #include <stdio.h>
       void swap(int *, int *)
       main()
       {
          int x,y;
          printf("Enter two integers:  ");
          scanf("%d", &x);
          scanf("%d", &y);
          printf("x = %d  x = %d\n",x,y);
          swap(&x,&y);
          printf("x = %d  x = %d\n",x,y);
       }
       void swap(int *px, int *py)
       {
          int temp;
          temp = *px;
          *px = *py;
          *py = temp;
       }
    
  3.    a = x  b = y
       a = p  b = q
    
  4. p = 5014
  5. q = 500C
  6. r = 501C
  7. c[1] = 6
  8. c[2] = 18
  9. c[3] = 8
  10. c[2] = 10
  11. r = 500C
  12. p = 5014
  13. p = 5018
  14. *(p+2)
  15. p+3
  16. 	10 10
    	5 5
    	8 8
    
  17. *switch += 10;
  18.    switch++;       or
       ++switch;       or
       switch += 1;    or
       switch = switch + 1;
    
  19. *switch -= 5;
  20. y = *(switch + 2);
  21.    char name[10];
       char *nameptr = name;
    
  22. char *nameptr = &name[3];
  23. *(ptr + 4) = 12;
  24. text[5], the sixth element of the array
  25. 30    25    20    15    10    5
    5    10    15    20    25    30
    5    5    5    5    5    5
    5    6    7    8    9    10
    
  26.    void reverse(int *one, int *two, int size)
       {
          int i, k;
          for (i = 0 ; i < size ; i++)
          {
             k = size - i - 1;
             *(one+i) = *(two+k);
          }
       }
    
  27.   int count(char *str, char ch)
      {
         int i;
         int thecount = 0;
         for (i = 0 ; *(str+i) ; i++)
            if (*(str+i) == ch)
               thecount++;
         return thecount;
      }
    
  28. The loop condition *str uses the character string points to. The pointer str moves down the string as it goes through the loop. Remember in C 0 is false and anything else is true. Thus *str is true when str points to any character in the str. When str points to the null byte (which is 0), *str is false. This means that the loop goes through every character in the string and exits when it reaches the null byte.

    This code removes every occurrence of ch from the string str. Trace it very carefully with an array that contains the character a couple of times, and you will see. You could use 'b' as the second parm with the following array:

    --------------------------------------
    | g | x | b | p | m | b | t | z | \0 |
    --------------------------------------
    
  29. int mystrlen(char *line) {
        int len;
    
        // stop loop at position of null byte, which is the length
        // no body needed for this loop
        for (len = 0 ; *(line+len) != '\0' ; len++);
    
        return len;
    }
    
  30. // instead of adding the loop counter to the pointers, we increment
    // the pointers
    void mystrcpy(char *dest, char *src) {
        while (*src != '\0') {
            *dest = *src;
            src++;
            dest++;
        }
        *dest = '\0';
    }
  31. void mystrcat(char *one, char *two) {
        int i;
    
        // get length of one
        int len = mystrlen(one);
    
        // start at beginning of two and end of one
        // copy everything from two into one
        for (i = 0 ; *(two+i) != '\0' ; i++, len++)
            *(one+len) = *(two+i);
    
        // add null byte to the end of one
        *(one+len) = '\0';
    }
    


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2014