Strings Revisited

Strings are arrays of characters. They aren’t a built-in data type like int or bool.

Consider this string:

string s = "HI!";

In memory, this could be stored as: | H | I | ! | \0 | | :—: | :—: | :—: | :—: | | 0x123 | 0x124 | 0x125 | 0x126 |

So… where is the variable s?

Summary: A string variable is just a pointer to a place in memory that has the first character of the string. The rest of the string is in continuous memory, and it ends with \0.

Removing the string alias from the previous example, we get:

char *s = "HI!";

Example: Replacing string with char *

Original:

#include <cs50.h>
#include <stdio.h>
int main(void)
{
    string s = "HI!";
    printf("%s\n", s);
}

Replaced:

#include <stdio.h>
int main(void)
{
    char *s = "HI!";
    printf("%s\n", s);
}

Pop Question: Different Addresses

#include <cs50.h>
#include <stdio.h>
int main(void)
{
    string s = "HI!";
    char c = s[0];
    char *p = &c;
    printf("%p\n", p);
    printf("%p\n", s);
}

This code prints two different addresses separated by a newline, why?

Example: Proving the Definition of a String in C (&s[0] == &s)

#include <cs50.h>
#include <stdio.h>
int main(void)
{
    string s = "HI!";
    char *p = &s[0];
    printf("%p\n", p); // Address of the first character in s
    printf("%p\n", s); // Address of s
}

This program prints out the same pointer twice separated by a newline, why?

For fun, lets print out the address of every character in the string:

#include <cs50.h>
#include <stdio.h>
int main(void)
{
    string s = "HI!";
    printf("%p\n", s);
    printf("%p/n", &s[0]);
    printf("%p/n", &s[1]);
    printf("%p/n", &s[2]);
    printf("%p/n", &s[3]);
}

Creating string

Recall how we created a structure like so:

struct
{
    string name;
    string number;
}

—and assigned it a definition like so:

typedef struct
{
    string name;
    string number;
}
person;

So, to define a type called string, we can do this:

typedef char *string;

Note: The double quotes we’ve been using around our strings tells the compiler (1) where to put each char in memory (2) add the \0 character at the end of the string and (3) return the pointer to the string