iOS Development

Course No. 10-152-139

Chapter 11 — Structs

The Setup

  • Let’s design a system that keeps track of our music files. What fields should we use? Here’s a preliminary list.
    • file name
    • artist
    • composer
    • album
    • length
    • audio format
    • bit rate
    • sample rate
    • size
    • year
    • genre
  • Eleven fields and that was just our first try at making the list!
  • Now we have to have variables for these.
  • Wow, that’s a lot of fields. Imagine trying to pass all those around to functions. Would a function with that number of variables be a good idea? Um, no…
  • What could we do?
  • The first answer, before we get to objects, is structs.

Structs

  • A struct is a collection of variables that can be used as a single entity.
  • Here’s a quick example
  • A struct is a custom type that we can define for our apps.
  • Once we have declared our struct here’s how we use it.
    • First, we create a variable.
    • Then we assign the data in the fields of our struct variable. We use the name of the struct variable we declared following by a period. Programmers usually call it a dot.
    • If we want to access one of the fields we use the same notation.
  • The next step is to use another piece of C syntax to make structs easier to use. We use the typedef statement to create a reusable alias to a type. This is how we would use it for the FullName struct.
  • So, what’s the point? I knew you were thinking that.
  • How about this?
  • That’s right, we can pass our struct to a function as one parameter. Here’s how we create it, assign values, and pass it to that function.

Back to the Music Files

  • Now we’ll look at a more complicated example using all those variables for our music files. Let’s make a struct!
  • It’s the same number of variables. However, we can now do things like this.
  • We create the struct and use it like this:
  • That looks pretty good and manageable.

Pointers to structs

  • What if you want to send a struct variable to two functions and have each one change a value in a struct field?
  • Let’s look at this function again.
  • Then call it like this:
  • This is actually making a copy of the struct:
  • Let’s see that in action in a demo. Copy and paste this program into a new project.
  • Changing the name inside the function doesn’t change the first one. This is because it got it’s own copy of the struct. How do we fix this? With pointers and address, of course! Here’s a new version of the program.
  • There are some new wrinkles in this code. The function definition should look like we expect.
  • The parameter is a pointer to a struct of type FullName.
  • When we call this function we send it the address of the struct.
  • So far, so good. But, then we see this:
  • What is this? ->
  • That is a little bit of syntax that was added to C so we didn’t have to do this:
  • We can’t do this because the dot operator happens first.
  • We want to access the firstName variable at the address pointed to by *name. The only way to do that was what we had above.
  • The answer was a new syntax that looks much better.
  • Which one would you rather use? I like the one with the ->!
  • If you happen to need to code functions that change the variables inside a struct you should look at this example once again.