> Question: _“What’s the difference between `int` and `float` in C++?”_
> [!info] **Answer**
`int` stores whole numbers without decimals, while `float` stores numbers with fractional parts.
For example:
> - `int x = 5;`
> - `float y = 5.25;`
---
### ❓ Question 2
> Question: _“Why do I need to use semicolons in C++?”_
> [!info] Answer
> Semicolons mark the end of a statement in C++. They help the compiler understand where one command finishes and the next begins. Without it, the compiler throws an error.
- C++ restriction explanation: [https://www.geeksforgeeks.org/why-variable-length-array-were-removed-in-cpp/](https://www.geeksforgeeks.org/why-variable-length-array-were-removed-in-cpp/)
---
## ❓ Question 2
> Question: _“Is it valid to use a Variable Length Array when compiling with GCC? In which situations is it not valid?”_
> [!info] **Answer**
> GCC **does allow** VLAs in C++ mode, but **only as a non-standard compiler extension**.
✔️ **OK in GCC when:**
- Used inside functions (automatic storage):
```cpp
int n = 10;
int arr[n]; // GCC accepts, but non-standard
```
❌ **Not OK when:**
- Writing portable or standards-compliant C++
- Using MSVC, Clang with strict mode, or compilers that reject VLAs
## ❓ Question: Can you somehow preserve an array’s size and use it in functions?
> **Answer**
> Yes — but only if you pass the array in a way that preserves its compile-time size. By default, when you pass a built-in C-style array to a function, it “decays” into a pointer (so size information is lost). ([GeeksforGeeks](https://www.geeksforgeeks.org/cpp/what-is-array-decay-in-c-how-can-it-be-prevented/?utm_source=chatgpt.com "What is Array Decay in C++? How can it be prevented? - GeeksforGeeks"))
If you want to preserve the size, you can pass by reference to an array. Example:
Because `arr` is a reference to an array of 10 ints, the function knows the array’s size at compile time. ([GeeksforGeeks](https://www.geeksforgeeks.org/cpp/pass-array-to-functions-in-cpp/?utm_source=chatgpt.com "Pass Array to Functions in C++ - GeeksforGeeks"))
If you need a function that works for arrays of _any_ compile-time-known size, you can use a template:
```cpp
template <size_t N>
void func(int (&arr)[N]) {
// N is deduced; you can use it inside the function
}
```
This way the function “remembers” the array size. This pattern avoids the “decay to pointer” problem. ([CodeArchPedia.com](https://openillumi.com/en/en-cpp-c-array-decay-type-size-basics/?utm_source=chatgpt.com "C++ Array Decay: Types, Sizes, and Fixing Pointer Conversion Bugs - CodeArchPedia.com"))
Alternatively, many modern C++ codebases avoid raw arrays and instead use container types (e.g. `std::array`, `std::vector`) which carry size information or allow querying their size. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array"))
---
## ❓ Question: How to initialize an array to all zeros? — in 1D, 2D, 3D, etc.
> **Answer**
- For a 1D built-in array in C++, you can zero-initialize like:
```cpp
int arr[5] = {0}; // all 5 elements become 0
```
([GitLab](https://fintechpython.pages.oit.duke.edu/jupyternotebooks/3-CPlusCPlus/14-Built-InArrays/answers/rq-14-answers.html?utm_source=chatgpt.com "CPlusPlus / Built-In Arrays — Programming for Financial Technology"))
- For multi-dimensional (2D, 3D, ...) built-in arrays, you can do nested brace initialization (or rely on partial zero initialization). For example:
```cpp
int mat[3][4] = {0}; // all elements become 0
```
Or for 3D:
```cpp
int cube[2][3][4] = {0};
```
The `{0}` initializes the first element to zero, and all other elements are zero-initialized by default (aggregate initialization rules). ([GitLab](https://fintechpython.pages.oit.duke.edu/jupyternotebooks/3-CPlusCPlus/14-Built-InArrays/answers/rq-14-answers.html?utm_source=chatgpt.com "CPlusPlus / Built-In Arrays — Programming for Financial Technology"))
- Similarly, if you use `std::array`, you can zero-initialize:
```cpp
std::array<int,5> arr = {0};
```
All members will be zero-initialized. ([magodo's blog](https://magodo.github.io/array-string-pointer-reference/?utm_source=chatgpt.com "C++ Array, String, Pointer and Reference"))
So yes — you _can_ initialize multi-dimensional arrays (1D, 2D, 3D, etc.) to zero with a simple initializer (or nested braces).
---
## ❓ Question: Why is there no compile-time error when you access out-of-bounds in a built-in array (i.e. “out of index”)?
> **Answer**
> Because built-in (C-style) arrays in C++ do **not** perform any bounds checking. The language simply allows `arr[i]` for any `i`, and does not verify at runtime whether `i` is valid. Accessing outside the allocated bounds is undefined behavior. ([GitLab](https://fintechpython.pages.oit.duke.edu/jupyternotebooks/3-CPlusCPlus/14-Built-InArrays/answers/rq-14-answers.html?utm_source=chatgpt.com "CPlusPlus / Built-In Arrays — Programming for Financial Technology"))
That means the compiler does not generate an error or warning (in general) if you index beyond the array’s size — it's up to the programmer to ensure correctness. This is a known risk of raw arrays. ([Stack Overflow](https://stackoverflow.com/questions/33319739/why-arent-built-in-arrays-safe?utm_source=chatgpt.com "c++ - Why aren't built-in arrays safe? - Stack Overflow"))
Because of this inherent unsafety (lack of bounds checking, size information lost when passing arrays, no easy way to return raw arrays, etc.), many C++ developers prefer safer alternatives (see below). ([Stack Overflow](https://stackoverflow.com/questions/33319739/why-arent-built-in-arrays-safe?utm_source=chatgpt.com "c++ - Why aren't built-in arrays safe? - Stack Overflow"))
---
## ❓ Question: What is the “standard array” (i.e. `std::array`)? What are the differences between `std::array` and built-in arrays? When is it best to use it?
> **Answer**
> `std::array<T, N>` is a template class in C++ standard library representing a fixed-size array of `N` elements of type `T`. It behaves like a thin wrapper over a built-in array, but with advantages. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array"))
**Differences / Advantages compared to built-in arrays:**
- **No “decay to pointer” when passed to functions**: `std::array` keeps size information. If you pass `std::array<int,5>` to a function by value or by reference, the size is known and preserved. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array"))
- **Supports assignment, copy, move semantics**: Unlike built-in arrays (which are not assignable or copyable as a whole), `std::array` behaves like a regular object. ([Stack Overflow](https://stackoverflow.com/questions/33319739/why-arent-built-in-arrays-safe?utm_source=chatgpt.com "c++ - Why aren't built-in arrays safe? - Stack Overflow"))
- **Has member functions & safer access**: For example, `.at()` performs bounds checking (throws exception on invalid index), while operator `[]` still gives raw access (no bounds check) — unlike built-in arrays where you only get `[]`. ([magodo's blog](https://magodo.github.io/array-string-pointer-reference/?utm_source=chatgpt.com "C++ Array, String, Pointer and Reference"))
- **Interoperability with standard library algorithms**: `std::array` supports iterators, `std::begin()`, `std::end()`, which makes it easier to integrate with STL algorithms. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array"))
**When is `std::array` best to use:**
- When the array size is known at compile time and fixed.
- When you want safer semantics — e.g. ability to copy/assign arrays, pass around by value or reference, avoid the “array decay” problems, and optionally get bounds-checked access via `.at()`.
- When you want to use standard library features (iterators, algorithms) with array data.
For dynamic or runtime-determined sizes (or if size can change), other types like `std::vector`, or (since C++20) `std::span` or dynamic containers are more appropriate. ([xeverous.github.io](https://xeverous.github.io/cpp/tutorials/beginner/08_arrays/03_std_array/?utm_source=chatgpt.com "modern C++ by Xeverous - 03 - std::array"))
---
## What is string in c++? object? class?
## Are strings passed by ref ?
## Are strnigs objects that are in stack? in heap?
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.