---
title: "Arrays within structures"
subject: "Computer Programming"
chapter: 8
key: "semester_1/computer_programming:arrays-within-structures"
source: https://www.bctengineer.com/q/semester_1/computer_programming/arrays-within-structures
site: BCT Engineering
---

# Arrays within structures

Computer Programming · Chapter 8: Structures

- Asked in: 2071 Shrawan, 4 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/arrays-within-structures
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-8

## Question

Explain about "Arrays within structures" along with programming example.

## Short answer

A structure member can be an array (e.g., int marks[3]). Elements are accessed as s.marks[i]. Initialization uses nested braces like {70,82,64}. Copying the structure copies the entire array inside it.

## Answer

A structure member can itself be an array. The array may be of characters (a string such as `name[20]`) or of numbers (such as marks of several subjects). This groups all the values related to one entity.

```c
struct student {
    char name[20];
    int roll;
    int marks[3];           /* array within the structure */
};
```
Individual elements are reached with the dot and then the index: `s.marks[1]`. The array can be initialised in nested braces: `{"Sita", 5, {70, 82, 64}}`. Copying the structure also copies the array inside it.

```c
#include <stdio.h>

struct student {
    char name[20];
    int roll;
    int marks[3];            /* array inside a structure */
};

int main(void)
{
    struct student s = {"Sita", 5, {70, 82, 64}};
    int total = 0;

    for (int i = 0; i < 3; i++)
        total += s.marks[i];
    printf("Name: %s  Roll: %d\n", s.name, s.roll);
    printf("Marks: %d %d %d  Total = %d  Percentage = %.2f\n",
           s.marks[0], s.marks[1], s.marks[2], total, total / 3.0);
    return 0;
}
```

Output:

```text
Name: Sita  Roll: 5
Marks: 70 82 64  Total = 216  Percentage = 72.00
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does 'array within a structure' mean?
   A. A member of the structure is itself an array
   B. The structure is inside an array
   C. A function returns an array
   D. The array holds structures
   Answer: A. For example name[20] or marks[3]. (Key point: Meaning)

2. How is the second mark of s accessed?
   A. s[1].marks
   B. s.marks[1]
   C. s.marks(1)
   D. marks[1].s
   Answer: B. Dot then index. (Key point: Access)

3. How is the array member initialised?
   A. In square brackets
   B. With parentheses
   C. In nested braces: {"Sita", 5, {70, 82, 64}}
   D. It cannot be initialised
   Answer: C. Inner braces initialise the array member. (Key point: Initialisation)

4. What happens to the array member when the structure is copied?
   A. It is not copied
   B. Only the first element is copied
   C. Copying is not allowed
   D. It is copied too
   Answer: D. The array is part of the structure's data. (Key point: Copying)

5. For marks 70, 82, 64, what are total and percentage?
   A. 216 and 72.00
   B. 216 and 216.00
   C. 72 and 216
   D. 226 and 75.33
   Answer: A. 70+82+64 = 216; 216/3 = 72. (Key point: Sample total)

## Related in Computer Programming

- [Creating 1D and 2D arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aone-two-dim-arrays-created) — Computer Programming, chapter 7, asked 2×
- [Array overflow/underflow; add two arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-overflow-underflow-add) — Computer Programming, chapter 7
- [Two dimensional arrays in FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afortran-2d-array-syntax) — Computer Programming, chapter 11
- [Nested structures with example](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Anested-structure-explain) — Computer Programming, chapter 8, asked 7×
- [Correct program: character arrays](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-char-arrays) — Computer Programming, chapter 7
- [Significance of arrays; multiply 3×3 matrices](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarray-significance-multiply-matrices) — Computer Programming, chapter 7

## Related in other subjects

- [T1 and E1 frame structures and bit rates](https://www.bctengineer.com/question.md?key=semester_8%2Ftelecommunications%3At1-e1-frame-structures) — Telecommunications, chapter 3
- [Roadside structures and landslide stabilisation](https://www.bctengineer.com/question.md?key=semester_6%2Ftransportation_engineering_i%3Aroadside-structures-landslide) — Transportation Engineering I, chapter 5
