Array Basics
Explore how arrays function in C by understanding their scope, memory allocation in .data, .bss, and stack sections, and how to calculate array sizes using sizeof and macros. This lesson clarifies the behavior of global and local arrays and provides foundational knowledge for effective array management.
We'll cover the following...
We'll cover the following...
Introduction
Up until this point, we used standalone variables. In this lesson, we’ll apply the information presented in the first four chapters to arrays.
Scope
The scope of arrays follows the same rules presented for variables.
There’s a global scope and a local scope:
- Arrays in the global scope are visible in the entire program.
- Arrays in the local scope are only visible inside the function where they are declared.
- Global arrays are stored inside the
.dataor.bsssections if they are initialized or not. - Local, non-static arrays are stored on the stack and cleaned before the function returns during the stack cleanup process.
- Local, static arrays are stored inside the
.dataor.bssand are only visible in the function where they are declared (globals with limited scope).
See the following code, where we define a bunch of arrays. We will explore the sections in which they are stored. There are comments on ...