CSSE 132 Mini-reading

Strings and Structs in 10 Minutes

Review how C stores strings, how a character differs from a string literal, and how to declare and use structs.

Why This Matters

Strings let C programs store and print text, while structs let one variable hold related information of different types. In CSSE 132, both ideas connect C code to the memory it uses: a string is an array of character values, and a struct has fields laid out in memory. Worksheet 08 asks you to trace these representations and use their syntax correctly.

Core Idea

A C string is an array of char values that ends with the null character '\0'. The null character marks where the text stops. It is a character value with numeric value 0, not the two visible characters \ and 0.

'a': one character

Single quotes create one char value. A char is a one-byte number, so 'a' has a character code between 0 and 255.

"a": a string

Double quotes create a string literal with two characters: {'a', '\0'}. In an expression, it provides the address of the first character.

A struct is a compound type with named fields. It groups related values together, such as a city's name, ZIP code, and a pointer to a note. Unlike an object in some languages, a C struct contains fields but no methods.

Worked Examples

Example 1: Build a string character by character

char greeting[3];
greeting[0] = 'H';
greeting[1] = 'i';
greeting[2] = '\0';
printf("%s\n", greeting);

The array has exactly three entries: 'H', 'i', and the terminator. %s tells printf to start at the first entry and print characters until it reaches '\0', so this prints Hi. Without a terminator, printf would continue reading past the array while looking for one.

Example 2: Follow a pointer through a string

char *s = "hello";
printf("%s\n", s + 2);

s stores the address of the first character, 'h'. Because a char occupies one byte, s + 2 points to the third character, 'l'. Printing from that address continues to the terminator, so the output is llo.

You may change where the pointer variable points, for example s = s + 1;. The literal's characters are stored in read-only memory, though, so writing s[0] = 'H'; is not valid.

Example 3: Compare two strings carefully

char first[3] = {'H', 'i', '\0'};
char *second = "Hi";

first == second

This expression is false because the array name and second refer to different memory locations. Even though both locations hold the text Hi, == compares addresses here, not the characters inside the strings. Draw two separate arrays in a memory diagram before deciding whether a pointer comparison can be true.

Example 4: Declare and access a struct

struct city {
    char name[10];
    int zipcode;
    char *note;
};

struct city home;
struct city *cityp = &home;
home.zipcode = 47803;
cityp->note = "Near campus";

struct city is the type name. The variable home is the struct itself, so use the dot operator: home.zipcode. cityp points to that struct, so use -> to access a field through the pointer: cityp->note. The expression cityp->note is shorthand for (*cityp).note.

Example 5: Account for struct storage

struct student {
    int id;
    char username[12];
    char *major;
};

On the 64-bit CSSE 132 platform, this struct occupies 24 bytes: 4 bytes for id, 12 for username, and 8 for the pointer major. The first two fields use 16 bytes together, so the pointer begins at an 8-byte-aligned offset and the total is 24 bytes.

Common Mistakes

Forgetting '\0'

Reserve one array entry for the terminator. A five-letter string needs at least six char entries.

Mixing up quote styles

'a' is one character value. "a" is a two-entry string containing 'a' followed by '\0'.

Comparing addresses instead of text

== between string pointers asks whether they point to the same location. Equal-looking text can be stored at different addresses.

Using the wrong field operator

Use . with a struct variable and -> with a pointer to a struct.

Quick Reference

ConceptC formMeaning
Character'R'One char value.
String literal"Rose"{'R', 'o', 's', 'e', '\0'}.
String outputprintf("%s", s);Print from s through '\0'.
Struct fieldcity.zipcodeField of a struct variable.
Pointer fieldcityp->zipcodeField through a struct pointer.
Equivalent pointer form(*cityp).zipcodeSame field access as cityp->zipcode.

Check Yourself

Try these before opening the answers. The values differ from Worksheet 08.

  1. Write the contents of the array created by char word[] = "cat";, including every character.
  2. What does printf("%s", "planet" + 3); print? Explain the starting location.
  3. Why can char a[3] = {'H', 'i', '\0'}; and char *b = "Hi"; contain the same text while a == b is false?
  4. Declare a struct book with a char array field named title of length 20, an int field named pages, and a char * field named author.
  5. If struct book *bp points to a book, write two equivalent expressions that access its pages field.

Answers

Show the answers
  1. {'c', 'a', 't', '\0'}. The compiler adds the terminator for a string literal used as an array initializer.
  2. net. Adding 3 moves from 'p' to the fourth character, 'n', and %s prints from there to '\0'.
  3. The text has the same characters, but the two representations use different storage locations. The comparison asks whether their addresses match.
  4. struct book {
        char title[20];
        int pages;
        char *author;
    };
  5. bp->pages and (*bp).pages.

Next Step

Complete Worksheet 08, then trace the provided string and struct examples with a memory diagram.