Strings are arrays of characters. They aren’t a built-in data type like int
or bool
.
Consider this string:
= "HI!"; string s
In memory, this could be stored as:
| H
| I
| !
| \0
|
| :—: | :—: | :—: | :—: |
| 0x123
| 0x124
| 0x125
| 0x126
|
So… where is the variable s
?
s
could be anywhere in the computer’s memory (0xWHATEVER
)s
is storing (in this example) 0x123
, the pointer to the first character in 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!";
string
with char *
Original:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
= "HI!";
string s ("%s\n", s);
printf}
Replaced:
#include <stdio.h>
int main(void)
{
char *s = "HI!";
("%s\n", s);
printf}
string
because the %s
(character string) is built-in to printf
.#include <cs50.h>
#include <stdio.h>
int main(void)
{
= "HI!";
string s char c = s[0];
char *p = &c;
("%p\n", p);
printf("%p\n", s);
printf}
This code prints two different addresses separated by a newline, why?
c
is a copy of s[0]
with its own unique address in memory.&s[0] == &s
)#include <cs50.h>
#include <stdio.h>
int main(void)
{
= "HI!";
string s char *p = &s[0];
("%p\n", p); // Address of the first character in s
printf("%p\n", s); // Address of s
printf}
This program prints out the same pointer twice separated by a newline, why?
s
literally just points to the first character in the string.\0
(by definition/convention of a string)For fun, lets print out the address of every character in the string:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
= "HI!";
string s ("%p\n", s);
printf("%p/n", &s[0]);
printf("%p/n", &s[1]);
printf("%p/n", &s[2]);
printf("%p/n", &s[3]);
printf}
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