# Learn C With Me #07 — Strings | Live

**URL:** <https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135>\
**Category:** Learn C With Me\
**Tags:** programming, c\
**Created:** [September 6, 2026, 10:33pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135 "2026-09-06T22:33:24Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![darth](https://forum.linuxrenaissance.com/user_avatar/forum.linuxrenaissance.com/darth/32/46_2.png) [@darth](https://forum.linuxrenaissance.com/u/darth)\
**Post date:** [September 6, 2026, 10:33pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/1 "2026-09-06T22:33:25Z")

</div>

[![](https://forum.linuxrenaissance.com/uploads/default/original/1X/4b11d4f29772428ed03b40f92f9bf7a3e9b23738.jpeg "Learn C With Me #07 — Strings | Live") ](https://www.youtube.com/watch?v=oBZzM9JVXCE)

In Lesson 7 of **Learn C With Me** , we finally wrap up our journey through **arrays in C** and move on to our next major topic: **strings**.

Using **Beej’s Guide to C** as our study material, we finish the remaining section on passing multidimensional arrays to functions and then discover something very C-like: **strings aren’t really a separate built-in type at all**. Instead, they’re closely connected to arrays, pointers, and memory.

We cover:

- **Passing multidimensional arrays to functions**
- Why C needs to know all array dimensions except the first
- How multidimensional array indexes translate into **memory offsets**
- A final look at the relationship between **arrays and pointers**
- **String literals** in C
- The difference between `"double quotes"` and `'single quotes'`
- Escaping characters inside strings
- **String variables**
- Representing strings using `char *`
- Representing strings as **arrays of `char`**
- Accessing individual characters using array notation
- The difference between `sizeof` on an array and `sizeof` on a pointer
- Using `strlen()` and `<string.h>`
- Why `strlen()` returns `size_t`
- **String initializers**
- The difference between a pointer to a string literal and a mutable character array
- Why attempting to modify a **string literal** results in undefined behavior
- **Null-terminated strings**
- The special `'\0'` character
- How C knows where a string ends
- Writing our own simple version of `strlen()`
- **Copying strings**
- Why assigning one string pointer to another doesn’t actually copy the string
- How two pointers can refer to the **same memory**

One of the biggest takeaways from this lesson is that **C doesn’t keep track of a string’s length for us**. A C string is essentially a sequence of characters in memory followed by a special `'\0'` terminator. We even look at how a simple `strlen()`-style function can find the length by walking through those characters until it reaches the terminator.

We also run into an important distinction between `char *s = "Hello";` and `char s[] = "Hello";`. They might initially look almost interchangeable, but their behavior becomes very different when we try to modify the data. This gives us another practical look at the relationship between **pointers, arrays, strings, and memory**.

Finally, we experiment with **copying strings** and see why simply assigning one pointer to another doesn’t create an independent copy. Both pointers can end up referring to the same underlying data, so modifying it through one pointer affects what we see through the other.

As usual, we experiment, make mistakes, follow a few programming-related side quests, get help from the live chat, and try to understand **why C behaves the way it does** rather than simply memorizing syntax.

---

<div class="post-metadata">

**Author:** ![darth](https://forum.linuxrenaissance.com/user_avatar/forum.linuxrenaissance.com/darth/32/46_2.png) [@darth](https://forum.linuxrenaissance.com/u/darth)\
**Post date:** [September 6, 2026, 10:35pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/2 "2026-09-06T22:35:09Z")

</div>

For homework try testing your arrays skill on making a tic-tac-toe game. Or at least attempting to make one and display it here, as code, so we can discuss.

---

<div class="post-metadata">

**Author:** ![iceblade92](https://forum.linuxrenaissance.com/user_avatar/forum.linuxrenaissance.com/iceblade92/32/25_2.png) [@iceblade92](https://forum.linuxrenaissance.com/u/iceblade92)\
**Post date:** [September 8, 2026, 5:22pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/3 "2026-09-08T17:22:59Z")

</div>

might have missunderstood the asingment but did make it so 2 players played tictactoe how ever i did not understand how to alternate player one and two moves. nor did i want to do all the turn order logic or terminal move inputs with wait functions. but found it bit fun to do i used 2 types of arrays for example use of arrays.

> **Spoiler code**
>
> ```c
> #include <stdio.h>
> 
> int main(void) {
> 
> char tictac\[3\]\[3\]={
> 
> {'\_','\_','\_'},
> 
> {'\_','\_','\_'},
> 
> {'\_','\_','\_'}
> 
> };
> 
> printf("empty board\\n");
> 
> for (int i=0; i<3; i++) {
> 
> for (int j=0; j<3; j++) {
> 
> printf("%c ", tictac\[i\]\[j\]);
> 
> if (j == 2) {
> 
> printf("\\n");
> 
> };
> 
> }
> 
> }
> 
> char p1\[3\]\[3\]={
> 
> {'X','\_','\_'},
> 
> {'\_','X','\_'},
> 
> {'\_','\_','X'}
> 
> };
> 
> char p2\[\]\[3\]={
> 
> {'\_','\_','O'},
> 
> {'\_','\_','\_'},
> 
> {'O','O','\_'}
> 
> };
> 
> for (int i=0; i<3; i++) {
> 
> for (int j=0; j<3; j++) {
> 
> if (p1\[i\]\[j\] == 'X') {
> 
> tictac\[i\]\[j\]= 'X';
> 
> printf("Player one placed X at Row:%d Column:%d\\n", i, j);
> 
> };
> 
> if (p2\[i\]\[j\] == 'O') {
> 
> tictac\[i\]\[j\]= 'O';
> 
> printf("Player two placed O at Row:%d Column:%d\\n", i, j);
> 
> };
> 
> }
> 
> }
> 
> printf("Players one wins\\n");
> 
> for (int i=0; i<3; i++) {
> 
> for (int j=0; j<3; j++) {
> 
> printf("%c ", tictac\[i\]\[j\]);
> 
> if (j == 2) {
> 
> printf("\\n");
> 
> };
> 
> }
> 
> }
> 
> };
> 
> ```

 ![Capture](https://forum.linuxrenaissance.com/uploads/default/original/1X/b56b929b736cac8b6dc451076f3e219b777328e9.png)

---

<div class="post-metadata">

**Author:** ![darth](https://forum.linuxrenaissance.com/user_avatar/forum.linuxrenaissance.com/darth/32/46_2.png) [@darth](https://forum.linuxrenaissance.com/u/darth)\
**Post date:** [September 8, 2026, 7:27pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/4 "2026-09-08T19:27:25Z")

</div>

Thanks for spoiler tag. I will do mine first, then look at yours.

---

<div class="post-metadata">

**Author:** ![darth](https://forum.linuxrenaissance.com/user_avatar/forum.linuxrenaissance.com/darth/32/46_2.png) [@darth](https://forum.linuxrenaissance.com/u/darth)\
**Post date:** [October 3, 2026, 4:01pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/5 "2026-10-03T16:01:07Z")

</div>

I started with pluses and minuses to draw, and realized it does not look good so I searched for UTF-8 characters that would better draw the grid but they did not count as regular characters so this was an interesting exercise because my odd rows are 16 long while my even rows are 34 long.

It took me an hour to come up with just this: `row1[i] != '\0'`

```c
#include <stdio.h>

int main(void){
    char row1[] = " 1 │ 2 │ 3 ";
    char row2[] = "───┼───┼───";
    char row3[] = " 4 │ 5 │ 6 ";
    char row4[] = "───┼───┼───";
    char row5[] = " 7 │ 8 │ 9 ";

    for (int i = 0; row1[i] != '\0'; i++)
        printf("%c", row1[i]);
    printf("\n");
    for (int i = 0; row2[i] != '\0'; i++)
        printf("%c", row2[i]);
    printf("\n");
    for (int i = 0; row3[i] != '\0'; i++)
        printf("%c", row3[i]);
    printf("\n");
    for (int i = 0; row4[i] != '\0'; i++)
        printf("%c", row4[i]);
    printf("\n");
    for (int i = 0; row5[i] != '\0'; i++)
        printf("%c", row5[i]);
    printf("\n");
}

```

The reasoning while I have number in those fields where X and O will eventually come is because I think this could be a good way to actually finish this game later on, with character replacement.

I will look at what Iceblade has done, now. I did not see it yet.

---

<div class="post-metadata">

**Author:** ![darth](https://forum.linuxrenaissance.com/user_avatar/forum.linuxrenaissance.com/darth/32/46_2.png) [@darth](https://forum.linuxrenaissance.com/u/darth)\
**Post date:** [October 3, 2026, 4:06pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/6 "2026-10-03T16:06:24Z")

</div>

> [@iceblade92](#):
>
> might have missunderstood the asingment but

Holy shit Iceblade beat the crap out of me. I thought I only need to use strings to draw the table, not the entire functional game.

---

<div class="post-metadata">

**Author:** ![iceblade92](https://forum.linuxrenaissance.com/user_avatar/forum.linuxrenaissance.com/iceblade92/32/25_2.png) [@iceblade92](https://forum.linuxrenaissance.com/u/iceblade92)\
**Post date:** [October 3, 2026, 4:31pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/7 "2026-10-03T16:31:00Z")

</div>

one think a notesed in your code is that most of it could have been done in one loop and that you did make structs but home work asks for more then 1 way sens you only did row[] but could have been row[numbers] like the lesson was about.

---

<div class="post-metadata">

**Author:** ![darth](https://forum.linuxrenaissance.com/user_avatar/forum.linuxrenaissance.com/darth/32/46_2.png) [@darth](https://forum.linuxrenaissance.com/u/darth)\
**Post date:** [October 3, 2026, 4:40pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/8 "2026-10-03T16:40:29Z")

</div>

Now I am looking at my code and wondering why didn’t compiler warn me about having two rows inside the for loop without curly braces. How come my code even works. I see how I can make one loop, but I think curly braces are needed.

EDIT: Actually no. You confused me. The first line in the for loop is a part of for loop and the newline one is outside of the loop, preparing terrain for the next loop.

---

<div class="post-metadata">

**Author:** ![davids](https://forum.linuxrenaissance.com/letter_avatar_proxy/v4/letter/d/65b543/32.png) [@davids](https://forum.linuxrenaissance.com/u/davids)\
**Post date:** [October 5, 2026, 4:44pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/9 "2026-10-05T16:44:24Z")

</div>

I did one too:

```c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

const char logos[3][4] = {" ", {0xef, 0x8c, 0x83, 0x00}, {0xef, 0x8c, 0x86, 0x00}};

int has_won(int board[3][3], int player) {
  int n;
  player += 1;

  // horizontal
  for (int row = 0; row < 3; ++row) {
    n = 0;
    for (int col = 0; col < 3; ++col)
      n += board[row][col] == player;
    if (n == 3) return 1;
  }

  // vertical
  for (int col = 0; col < 3; ++col) {
    n = 0;
    for (int row = 0; row < 3; ++row)
      n += board[row][col] == player;
    if (n == 3) return 1;
  }

  // diagonal right & down
  n = 0;
  for (int i = 0; i < 3; ++i)
    n += board[i][i] == player;
  if (n == 3) return 1;

  // diagonal right & up
  n = 0;
  for (int i = 0; i < 3; ++i)
    n += board[2-i][i] == player;
  if (n == 3) return 1;

  return 0;
}

void draw_board(int board[3][3]) {
  const char template[] =
    " 1 2 3\n"
    " +---+---+---+\n"
    "A | ? | ? | ? |\n"
    " +---+---+---+\n"
    "B | ? | ? | ? |\n"
    " +---+---+---+\n"
    "C | ? | ? | ? |\n"
    " +---+---+---+\n";
  
  int i = 0;
  for (const char *t = template; *t; ++t) {
    if (*t == '?') {
      int player = board[i/3][i%3];
      fputs(logos[player], stdout);
      ++i;
    } else {
      putchar(*t);
    }
  }
}

enum input_result {
  QUIT,
  INVALID,
  POSITION,
};

enum input_result get_input(int *row, int *col) {
  char readbuf[4];
  if (!fgets(readbuf, sizeof(readbuf), stdin)) return QUIT;
  if (readbuf[0] == 'q') return QUIT;
  if (strlen(readbuf) < 2) return INVALID;

  const char *s = "acAC";
  for (; *s; s += 2) {
    if (readbuf[0] >= s[0] && readbuf[0] <= s[1]) {
      *row = readbuf[0] - s[0];
      break;
    }
  }
  if (!*s) return INVALID;

  if (readbuf[1] >= '1' && readbuf[1] <= '3') {
    *col = readbuf[1] - '1';
  } else return INVALID;

  return POSITION;
}

int main(int argc, char const *argv[]) {
  int board[3][3] = {0};
  int quit = 0;
  int won = 0;
  int player = 0;

  draw_board(board);
  puts("To make a move, enter <row><column> e.g. a2\n"
       "Enter q or ctrl-d to quit\n" ); 

  while (!(quit || won)) {
    printf("%s's turn: ", logos[player+1]);

    int row, col;
    switch (get_input(&row, &col)) {
      case QUIT:
        quit = true;
        break;
      case INVALID:
        putchar('\a');
        break;
      case POSITION:
        if (board[row][col] != 0) {
          putchar('\a');
        } else {
          board[row][col] = player + 1;
          if (has_won(board, player)) {
            won = true;
          } else {
            player = (player + 1) % 2;
          }
        }
        break;
    }
    if (!quit || won) {
      draw_board(board);
    }
  }

  if (won) {
    printf("%s wins!\n", logos[player+1]);
  } else {
    puts("Bye!");
  }
}

```

---

<div class="post-metadata">

**Author:** ![iceblade92](https://forum.linuxrenaissance.com/user_avatar/forum.linuxrenaissance.com/iceblade92/32/25_2.png) [@iceblade92](https://forum.linuxrenaissance.com/u/iceblade92)\
**Post date:** [October 6, 2026, 12:10am UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/10 "2026-10-06T00:10:33Z")

</div>

well that was a read i saw you used alot of things the lessons have not coverd yet but was fun to see what it would look like wen someone who knows what there doing and your enum was uniqe to the point i fought is was magic olmost XD

---

<div class="post-metadata">

**Author:** ![davids](https://forum.linuxrenaissance.com/letter_avatar_proxy/v4/letter/d/65b543/32.png) [@davids](https://forum.linuxrenaissance.com/u/davids)\
**Post date:** [October 7, 2026, 3:27pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/11 "2026-10-07T15:27:17Z")

</div>

I’m still learning 🙂 My input function got some problems I can see. Maybe we’ll get to some of that stuff later.

---

<div class="post-metadata">

**Author:** ![darth](https://forum.linuxrenaissance.com/user_avatar/forum.linuxrenaissance.com/darth/32/46_2.png) [@darth](https://forum.linuxrenaissance.com/u/darth)\
**Post date:** [October 11, 2026, 3:44pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/12 "2026-10-11T15:44:04Z")

</div>

Code patreon provided an extra homework:

**Write a function which accepts 3 parameters:**

1. Source string.
2. Number representing the index of a word.
3. Destination string.

It should copy an N-th word from source string to destination. Words are sequences of non-space characters delimited by space (assume it’s a single space).  
If it can’t find the N-th word then function should return `false`, otherwise it should return `true`.

---

<div class="post-metadata">

**Author:** ![davids](https://forum.linuxrenaissance.com/letter_avatar_proxy/v4/letter/d/65b543/32.png) [@davids](https://forum.linuxrenaissance.com/u/davids)\
**Post date:** [October 17, 2026, 5:56pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/13 "2026-10-17T17:56:08Z")

</div>

I had a go:

I added an argument for destination buffer size. If the word doesn’t fit in the destination, it is signaled by setting it to an empty string.

```c
#include <ctype.h>
#include <assert.h>
#include <stddef.h>

static bool is_word_char(char c) {
  return (!isspace(c) && c != '\0');
}

bool copy_nth_word(const char *src, size_t n, char *dst, size_t bufsize) {
  assert(bufsize > 0);
  
  while (true) {
    while (isspace(*src)) ++src;

    if (*src == '\0') return false;
    if (n == 0) break;

    while (is_word_char(*src)) ++src;

    n -= 1;
  }

  if (n > 0) return false;

  size_t i = 0;
  do {
    dst[i++] = *src++;
  } while (i < bufsize && is_word_char(*src));

  if (i == bufsize) i = 0;
  dst[i] = '\0';

  return true;
}

```

---

<div class="post-metadata">

**Author:** ![iceblade92](https://forum.linuxrenaissance.com/user_avatar/forum.linuxrenaissance.com/iceblade92/32/25_2.png) [@iceblade92](https://forum.linuxrenaissance.com/u/iceblade92)\
**Post date:** [October 17, 2026, 8:24pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/14 "2026-10-17T20:24:40Z")

</div>

i see you used alot of things that the lessons have not coverd yet and made it kind of overcomplicated but i do like the helper function for easy false true distinktion but i do not understand why is it a const char \*src like is the pointer it self a constants?

---

<div class="post-metadata">

**Author:** ![Rootlexx](https://forum.linuxrenaissance.com/user_avatar/forum.linuxrenaissance.com/rootlexx/32/26_2.png) [@Rootlexx](https://forum.linuxrenaissance.com/u/Rootlexx)\
**Post date:** [October 17, 2026, 9:23pm UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/15 "2026-10-17T21:23:40Z")

</div>

You’ve got potential UB there: ctype.h functions require that their argument is either `EOF` or _representable as `unsigned char`_, otherwise their behavior is undefined. If `char` is signed then it can hold values which don’t follow that requirement.

Always explicitly cast to `unsigned char` when passing values to these functions.

---

<div class="post-metadata">

**Author:** ![davids](https://forum.linuxrenaissance.com/letter_avatar_proxy/v4/letter/d/65b543/32.png) [@davids](https://forum.linuxrenaissance.com/u/davids)\
**Post date:** [October 18, 2026, 9:34am UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/16 "2026-10-18T09:34:51Z")

</div>

No, AFAIU `const` here makes the `char` dereferenced through the pointer constant. If the pointer itself should be constant that would be `char *const src`.  
Btw, the cdecl program is very useful. It can translate from C type declaration to English, and the other way around.

---

<div class="post-metadata">

**Author:** ![davids](https://forum.linuxrenaissance.com/letter_avatar_proxy/v4/letter/d/65b543/32.png) [@davids](https://forum.linuxrenaissance.com/u/davids)\
**Post date:** [October 18, 2026, 10:24am UTC](https://forum.linuxrenaissance.com/t/learn-c-with-me-07-strings-live/135/17 "2026-10-18T10:24:57Z")

</div>

Oops. It’s spelled out twice in the short man page too, which makes it kind of embarrassing.
