Skip to main content

C++ Arrays and Structures

Published 1 min readMarkdown

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. Arrays are a fundamental data structure and are often used to store and manipulate a fixed number of elements.

Declaring and Initializing Arrays:

You can declare and initialize an array in C++ using the following syntax

Example:

Now declaration of values:

To access the Elements of the array:

Now, you can change the value of a specific element we can do it by the index of a number:

Storing Multiple Arrays:

Structures:

Structures are also called as the struct is a way to group several related variables into one place. Each variable in the structure is known as the member of the structure.

Steps for Creating a Structure Variable:

  1. Create a structure variable that is myStructure

    struct {
    int myNum;
    string myString;
    } myStructure;

  2. Assign values to members of myStructure

    myStructure.myNum = 1;
    myStructure.myString = "Hello World!";

  3. print members of myStructure

    cout << myStructure.myNum;
    cout << myStructure.myString;

One more example of the structure is:

Thanks for reading,

Mohammed Muqafamuddin.

No comments yet. Be the first to comment.

More from this blog