> For the complete documentation index, see [llms.txt](https://mr-poston-1.gitbook.io/c++/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mr-poston-1.gitbook.io/c++/2.-going-beyond-the-basics/2.3-structs/2.3.2-defining-and-accessing-structs.md).

# 2.3.2 Defining and Accessing Structs

Structs help take the place of some classes by storing related member variables. The main use of a struct is to group similar data together under one variable type.

### Basic Format for Structs

Structs are generally defined to store multiple data elements in a single format. Here is the basic syntax for a struct.

```
struct structName {
    memberType1 memberName1;
    memberType2 memberName2;
};
```

There are several things to notice about the definition. First, the definition begins by using the key word `struct`. After that, the name of the struct is given. In this course, you will see structs names starting in lowercase letters and using camel case.

After the name is a set of curly brackets and inside of these you will see the different data types and variable names.

One thing to notice is that the struct definition ends with a curly bracket and a semicolon, `;`. The semicolon is critical.

Structs are typically defined at the top of the program and need to be defined before they can be used.

### Example: Student struct

Let’s take a look at a specific example. If you wanted to create a student struct that can store the student’s name and their grade, it would look like this:

```
struct student{
    string name;
    int grade;
};
```

Notice in this example that it declares two member variables by giving a variable type and a name.

All values and the struct itself are public by default.

### Using a Struct

Once created, you can use a struct like you would use another variable. Access for assignment and updates to the member variables is done through a dot access:

```
structName variable;
variable.memberName1 = value;
variable.memberName2 = value;
```

Using the student struct from above as an example, here is how you would assign and access a member variable.

```
student s;
s.name = "Tracy";
s.grade = 12;

cout << s.name << " is in grade " << s.grade << endl;
```

Notice in the example that the student is declared and then values are defined without having to instantiate any object. Structs behave like a class in Java, but are much quicker and easier to implement. As a result, you will find that you will use structs often in your programming.

### [Try This Example](https://replit.com/@Poston/232-Defining-and-Accessing-Structs#main.cpp)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mr-poston-1.gitbook.io/c++/2.-going-beyond-the-basics/2.3-structs/2.3.2-defining-and-accessing-structs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
