Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The question is a bit broad, but...</p> <p>A struct is an <em>aggregate</em> or <em>composite</em> data type, used for representing entities that are described by multiple <em>attributes</em> of potentially different types. Some examples:</p> <ul> <li>A point in 3-D space, represented by 3 real-valued coordinates x, y, and z;</li> <li>A mailing address, represented by a street name, house or apartment number, city, state, ZIP code;</li> <li>A line item in an invoice, represented by a part name or number, unit cost, quantity, and subtotal;</li> <li>A node in a tree, represented by a key, data value, left child, and right child;</li> </ul> <p>etc., etc., etc. </p> <p>Let's look at the mailing address as a concrete example. We could define our mailing address type as follows:</p> <pre><code>struct Address { char *streetName; int buildingNumber; // House, apt building, office building, etc. char *aptNumber; // Handles apt and suite #s like K103, B-2, etc. char *city; char state[3]; int zip; }; </code></pre> <p>We'd create an instance of that struct like so:</p> <pre><code>struct Address newAddress; </code></pre> <p>and a pointer to that instance as:</p> <pre><code>struct Address *addrPtr = &amp;newAddress; </code></pre> <p>and access each of its fields using either the <code>.</code> or <code>-&gt;</code> operator depending on whether we're dealing with a struct instance or a pointer to a struct:</p> <pre><code>newAddress.streetName = strdup("Elm"); addrPtr-&gt;buildingNumber = 100; ... </code></pre> <p>Another way to look at structs is something like a database <em>record</em> composed of multiple <em>fields</em>. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload