---
title: "Objective-C : Structures"  
description: "Structs are a C construct that allows for grouping of items into one variable."  
author: "Anonymous User"  
published: 2015-07-13  
updated: 2026-05-13  
canonical: https://www.mindstick.com/articles/1819/objective-c-structures  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 4 minutes  

---

# Objective-C : Structures

*Previouslywe learn how to use [directives](https://www.mindstick.com/blog/44/directives-in-asp-dot-net) in ObjC:* [Objective-C : Property and Synthesize directive](http://www.mindstick.com/blog/10918/Objective%20C%20Property%20and%20Synthesize%20directive#.VaN7Rli6bIU)

Objective-C arrays allow you to define type of [variables](https://www.mindstick.com/articles/715/php-variables) thatcan hold several data items of the same kind but [structure](https://www.mindstick.com/forum/1188/how-to-create-structure-in-c) is anotheruser-defined data type available in Objective-C [programming](https://yourviews.mindstick.com/view/88478/vibe-coding-the-rise-of-ai-assisted-programming) which allows you tocombine data items of different kinds.

Structuresare used to represent a record, Suppose you want to keep track of your books ina library. You might want to track the following [attributes](https://www.mindstick.com/articles/13105/2-attributes-that-make-your-odoo-ecommerce-theme-productive-and-engaging) about each book:

- Title
- Author
- Subject
- Book ID

##### Defininga Structure

To define astructure, you must use the **struct** statement. The struct statementdefines a new data type, with more than one member for [your program](https://answers.mindstick.com/qa/33494/how-do-you-find-any-view-element-into-your-program-explain-for-example). The formatof the struct statement is this:

```
struct [structuretag]{    member definition;    member definition;    ...    member definition;} [one or more structure variables]; 
```

The **structuretag** is optional and each member [definition](https://yourviews.mindstick.com/view/70598/false-furore-over-the-leadership-definition-statement-of-army-chief-bipin-rawat) is a normal variable definition,such as int i; or float f; or any other valid variable definition. At the endof the structure's definition, before the final semicolon, you can specify oneor more structure variables but it is optional. Here is the way you woulddeclare the Book structure:

```
struct Books{    NSString *title;    NSString *author;    NSString *subject;    int  book_id;} book; 
```

##### Structure (structs)

Structs area C construct that allows for grouping of items into one variable. For example,if you wanted to store a date, you could use three separate variables for theday, month, and year. This approach would work if you had to store one or twodates. But if you’re writing a calendar [application](https://www.mindstick.com/articles/12824/calculator-application-in-android), for example, having tokeep track of which month belonged with which year would be a nightmare.Fortunately, C structs allow you to group these values into one type. Thesyntax for a date struct might look like this:

```
struct date {int month; int day; int year;};
```

You can thendefine a variable of this type as struct date today, yesterday;. However, wecan use our friend the typedef statement to make this code a bit neater:

```
typedef struct {int month; int day; int year;} Date;Date today, yesterday; 
```

To accessvalues within a struct, you use the dot operator. To set the month, you mightwrite today.month = 5;. To get a value, the same [procedure](https://www.mindstick.com/blog/304484/explain-the-sql-stored-procedures) applies: int year =today.year;.

In action,it might look like this:

```
#import <Foundation/Foundation.h>int main (int argc, char *argv[]){    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];        typedef struct {int month; int day; int year;} Date;    Date today;    today.month = 5;    today.day = 22;    today.year = 2011;    NSLog(@"Today's date is %i/%i/%i", today.month, today.day, today.year);    [pool drain];    return 0;}
```

After execution the result will look like this:

```
2015-07-13 04:36:58.342 demo[11911] Today's date is 5/22/2011
```

##### InitializingStructures

The fastestway to initialize a structure is to enclose the values in a pair of braces:

*Date today = {5,22, 2011};*You can list fewer values, and they will be filled inuntil the end of the values you list. The remaining values will be undefined.

*Date today ={5};*This sets the month to 5, but leaves the remaining fields withgarbage values from RAM.

You can alsoinitialize the values out of order:

*Date today ={.day = 22, .month = 5; .year = 2011};*

##### StructsWithin Structs

C allows youto put structs with structs. For example, Apple provides the CGRect data type,which defines a data type by being a struct containing a CGPoint and CGSize:

```
struct CGRect {    CGPoint origin;    CGSize size;};typedef struct CGRect CGRect;
```

\

You can thenaccess the values by chaining the dot operator. To get the width, you wouldwrite *float width =rect.size.width;*

\

*Next, we will learn about:* Objective-C : Unions

---

Original Source: https://www.mindstick.com/articles/1819/objective-c-structures

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
