Recall our array notation:
char *s = "HI!";
("%c", s[0]);
printf("%c", s[1]);
printf("%c", s[2]); printf
This is just syntactic sugar for manipulating the pointers as-they-are, like so:
char *s = "HI!";
("%c", *s); // Retrieve (*) the data at pointer s
printf("%c", *(s + 1));
printf("%c", *(s + 2)); printf
Or,
int numbers[7] = {4,6,8,2,7,5,0};
("%i\n", *numbers);
printf("%i\n", *(numbers + 1));
printf("%i\n", *(numbers + 2));
printf("%i\n", *(numbers + 3));
printf("%i\n", *(numbers + 4));
printf("%i\n", *(numbers + 5));
printf("%i\n", *(numbers + 6)); printf
1
rather than having to figure out the size of the data type ourselves and adding 4 bytes to get to the next chunk of data in memory.*
or &
when we define numbers
, but we still treat it like a pointer.\0
character for arrays.Notice this functional code which determines if two integers are equal:
int main(void)
{
int i = get_int("i: ");
int j = get_int("j: ");
if (i == j)
("Same");
printfelse
("Different");
printf}
Notice this non-functional code for determining if two integers are equal:
int main(void)
{
= get_string("s: ");
string s = get_string("t: ");
string t if (s == t)
("Same");
printfelse
("Different");
printf}
s
and t
are literally just pointers to the first character of their respective, separate strings (e.g., 0x123 != 0x420
).string
abstraction kinda hides thisget_string
returns the address of the first char of a stringHere’s a functional comparison using strcmp
from string.h
(provides string-related functions, doesn’t set a string
typedef):
int main(void)
{
= get_string("s: ");
string s = get_string("t: ");
string t if (strcmp(s,t) == 0)
("Same");
printfelse
("Different");
printf}
strcmp
likely loops over each character and checks if they’re equal that way= get_string();
string s = s;
string t [0] = toupper(t[0]);
t("%s\n", s);
printf("%s\n", t); printf
This prints out the same string twice (the user’s input string, but with the first letter capitalized). Why?
string t = s
sets t equal to s, which is an address like 0x123
, the pointer of the first character in the string s.t
and s
both actually share the same address in memory because of this—we have two pointers to the same location in memory.If we want a genuine copy of s
, we’ll need to use dynamic memory allocation.