CS157: Intro to C, Part II

Spring 2018

Pointers 2

See this page as a slide show

More fun with pointers and strings

For a very specialized concept of fun.

What does this do?

    void whatami(char *a, char *b) {
        while (*a++ = *b++);
    }

    int main() {
        char alpha[5] = "Han", beta[5] = "Solo";
        whatami(alpha, beta);
    }

What does this do?

    void whatami(char *a, char *b) {
        while (*a++ = *b++);
    }

    int main() {
        char alpha[5] = "Han", beta[5] = "Solo";
        whatami(alpha, beta);
    }

Let’s step through it.

What does this do?

    void whatami(char *a, char *b) {
        while (*a++ = *b++);
    }
    char alpha[5] = "Han", beta[5] = "Solo";
    whatami(alpha, beta);

      ┌──┐                                 ┌──┐
    a:│ ─┼──┐                            b:│ ─┼──┐
      └──┘  │                              └──┘  │
            ∨                                    ∨
          ┌────┬────┬────┬────┬────┐           ┌────┬────┬────┬────┬────┐
    alpha:│ H  │ a  │ n  │ \0 │ ?? │      beta:│ S  │ o  │ l  │ o  │ \0 │
          └────┴────┴────┴────┴────┘           └────┴────┴────┴────┴────┘

First, do the assignment; copy the 'S'.

What does this do?

    void whatami(char *a, char *b) {
        while (*a++ = *b++);
    }
    char alpha[5] = "Han", beta[5] = "Solo";
    whatami(alpha, beta);

      ┌──┐                                ┌──┐
    a:│ ─┼───────┐                      b:│ ─┼───────┐
      └──┘       │                        └──┘       │
                 ∨                                   ∨
          ┌────┬────┬────┬────┬────┐           ┌────┬────┬────┬────┬────┐
    alpha:│ S  │ a  │ n  │ \0 │ ?? │      beta:│ S  │ o  │ l  │ o  │ \0 │
          └────┴────┴────┴────┴────┘           └────┴────┴────┴────┴────┘

Sometime later (vague), do the post-increments, in any order.

What does this do?

    void whatami(char *a, char *b) {
        while (*a++ = *b++);
    }
    char alpha[5] = "Han", beta[5] = "Solo";
    whatami(alpha, beta);

      ┌──┐                                ┌──┐
    a:│ ─┼───────┐                      b:│ ─┼───────┐
      └──┘       │                        └──┘       │
                 ∨                                   ∨
          ┌────┬────┬────┬────┬────┐           ┌────┬────┬────┬────┬────┐
    alpha:│ S  │ a  │ n  │ \0 │ ?? │      beta:│ S  │ o  │ l  │ o  │ \0 │
          └────┴────┴────┴────┴────┘           └────┴────┴────┴────┴────┘

Is an assignment true or false?

What does this do?

    void whatami(char *a, char *b) {
        while (*a++ = *b++);
    }
    char alpha[5] = "Han", beta[5] = "Solo";
    whatami(alpha, beta);

      ┌──┐                                ┌──┐
    a:│ ─┼───────┐                      b:│ ─┼───────┐
      └──┘       │                        └──┘       │
                 ∨                                   ∨
          ┌────┬────┬────┬────┬────┐          ┌────┬────┬────┬────┬────┐
    alpha:│ S  │ a  │ n  │ \0 │ ?? │     beta:│ S  │ o  │ l  │ o  │ \0 │
          └────┴────┴────┴────┴────┘          └────┴────┴────┴────┴────┘

What does this do?

    void whatami(char *a, char *b) {
        while (*a++ = *b++);
    }
    char alpha[5] = "Han", beta[5] = "Solo";
    whatami(alpha, beta);

      ┌──┐                                ┌──┐
    a:│ ─┼───────┐                      b:│ ─┼───────┐
      └──┘       │                        └──┘       │
                 ∨                                   ∨
          ┌────┬────┬────┬────┬────┐          ┌────┬────┬────┬────┬────┐
    alpha:│ S  │ a  │ n  │ \0 │ ?? │     beta:│ S  │ o  │ l  │ o  │ \0 │
          └────┴────┴────┴────┴────┘          └────┴────┴────┴────┴────┘

What does this do?

    void whatami(char *a, char *b) {
        while (*a++ = *b++);
    }
    char alpha[5] = "Han", beta[5] = "Solo";
    whatami(alpha, beta);

      ┌──┐                                ┌──┐
    a:│ ─┼────────────┐                 b:│ ─┼────────────┐
      └──┘            │                   └──┘            │
                      ∨                                   ∨
          ┌────┬────┬────┬────┬────┐          ┌────┬────┬────┬────┬────┐
    alpha:│ S  │ o  │ n  │ \0 │ ?? │     beta:│ S  │ o  │ l  │ o  │ \0 │
          └────┴────┴────┴────┴────┘          └────┴────┴────┴────┴────┘

What does this do?

    void whatami(char *a, char *b) {
        while (*a++ = *b++);
    }
    char alpha[5] = "Han", beta[5] = "Solo";
    whatami(alpha, beta);

      ┌──┐                                ┌──┐
    a:│ ─┼─────────────────┐            b:│ ─┼─────────────────┐
      └──┘                 │              └──┘                 │
                           ∨                                   ∨
          ┌────┬────┬────┬────┬────┐          ┌────┬────┬────┬────┬────┐
    alpha:│ S  │ o  │ l  │ \0 │ ?? │     beta:│ S  │ o  │ l  │ o  │ \0 │
          └────┴────┴────┴────┴────┘          └────┴────┴────┴────┴────┘

What does this do?

    void whatami(char *a, char *b) {
        while (*a++ = *b++);
    }
    char alpha[5] = "Han", beta[5] = "Solo";
    whatami(alpha, beta);

      ┌──┐                                ┌──┐
    a:│ ─┼──────────────────────┐       b:│ ─┼──────────────────────┐
      └──┘                      │         └──┘                      │
                                ∨                                   ∨
          ┌────┬────┬────┬────┬────┐          ┌────┬────┬────┬────┬────┐
    alpha:│ S  │ o  │ l  │ o  │ ?? │     beta:│ S  │ o  │ l  │ o  │ \0 │
          └────┴────┴────┴────┴────┘          └────┴────┴────┴────┴────┘

What does this do?

    void whatami(char *a, char *b) {
        while (*a++ = *b++);
    }
    char alpha[5] = "Han", beta[5] = "Solo";
    whatami(alpha, beta);

      ┌──┐                                ┌──┐
    a:│ ─┼───────────────────────────┐  b:│ ─┼───────────────────────────┐
      └──┘                           │    └──┘                           │
                                     ∨                                   ∨
          ┌────┬────┬────┬────┬────┐          ┌────┬────┬────┬────┬────┐
    alpha:│ S  │ o  │ l  │ o  │ \0 │     beta:│ S  │ o  │ l  │ o  │ \0 │
          └────┴────┴────┴────┴────┘          └────┴────┴────┴────┴────┘

Finally, we copy the '\0'. Is it true or false?

How about this one?

    void whatami(char *a, char *b) {
        while (*a)
            a++;
        while (*a++ = *b++);
    }

so … it’s strcpy!

Useful string functions (review)

#include <string.h>
Must do this to use string functions.
strchr(char *s, char c);
Returns a pointer to the first occurrence of c in s
strstr(char *haystack, char *needle);
Returns a pointer to the first occurrence of needle in the haystack

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2015-06-06T09:46

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building