Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Why Do We Need Array Size as an Argument? | Function Arguments Specification
C++ Functions
course content

Зміст курсу

C++ Functions

C++ Functions

1. Introduction
2. Function Arguments Specification
3. Function Return Values Specification
4. Some Advanced Topics

Why Do We Need Array Size as an Argument?

Завдання

Why is it necessary to provide the size of the array as an additional argument when using dynamic arrays? The crucial factor is that technically, we are passing not the entire array but the pointer to its first value.

Remember

The compiler does not have information about the size of the array, which can lead to accessing memory beyond the array's boundaries, resulting in unexpected garbage values.

Let's solve a simple task to illustrate it:

  • Pass the dynamic array as the first argument of the function.
  • Call the function inside the main() block and pass the pointer at the first element of the array as the first argument.

Look at the result! Pay attention to the last two values!

Завдання

Why is it necessary to provide the size of the array as an additional argument when using dynamic arrays? The crucial factor is that technically, we are passing not the entire array but the pointer to its first value.

Remember

The compiler does not have information about the size of the array, which can lead to accessing memory beyond the array's boundaries, resulting in unexpected garbage values.

Let's solve a simple task to illustrate it:

  • Pass the dynamic array as the first argument of the function.
  • Call the function inside the main() block and pass the pointer at the first element of the array as the first argument.

Look at the result! Pay attention to the last two values!

Все було зрозуміло?

Секція 2. Розділ 6
toggle bottom row

Why Do We Need Array Size as an Argument?

Завдання

Why is it necessary to provide the size of the array as an additional argument when using dynamic arrays? The crucial factor is that technically, we are passing not the entire array but the pointer to its first value.

Remember

The compiler does not have information about the size of the array, which can lead to accessing memory beyond the array's boundaries, resulting in unexpected garbage values.

Let's solve a simple task to illustrate it:

  • Pass the dynamic array as the first argument of the function.
  • Call the function inside the main() block and pass the pointer at the first element of the array as the first argument.

Look at the result! Pay attention to the last two values!

Завдання

Why is it necessary to provide the size of the array as an additional argument when using dynamic arrays? The crucial factor is that technically, we are passing not the entire array but the pointer to its first value.

Remember

The compiler does not have information about the size of the array, which can lead to accessing memory beyond the array's boundaries, resulting in unexpected garbage values.

Let's solve a simple task to illustrate it:

  • Pass the dynamic array as the first argument of the function.
  • Call the function inside the main() block and pass the pointer at the first element of the array as the first argument.

Look at the result! Pay attention to the last two values!

Все було зрозуміло?

Секція 2. Розділ 6
toggle bottom row

Why Do We Need Array Size as an Argument?

Завдання

Why is it necessary to provide the size of the array as an additional argument when using dynamic arrays? The crucial factor is that technically, we are passing not the entire array but the pointer to its first value.

Remember

The compiler does not have information about the size of the array, which can lead to accessing memory beyond the array's boundaries, resulting in unexpected garbage values.

Let's solve a simple task to illustrate it:

  • Pass the dynamic array as the first argument of the function.
  • Call the function inside the main() block and pass the pointer at the first element of the array as the first argument.

Look at the result! Pay attention to the last two values!

Завдання

Why is it necessary to provide the size of the array as an additional argument when using dynamic arrays? The crucial factor is that technically, we are passing not the entire array but the pointer to its first value.

Remember

The compiler does not have information about the size of the array, which can lead to accessing memory beyond the array's boundaries, resulting in unexpected garbage values.

Let's solve a simple task to illustrate it:

  • Pass the dynamic array as the first argument of the function.
  • Call the function inside the main() block and pass the pointer at the first element of the array as the first argument.

Look at the result! Pay attention to the last two values!

Все було зрозуміло?

Завдання

Why is it necessary to provide the size of the array as an additional argument when using dynamic arrays? The crucial factor is that technically, we are passing not the entire array but the pointer to its first value.

Remember

The compiler does not have information about the size of the array, which can lead to accessing memory beyond the array's boundaries, resulting in unexpected garbage values.

Let's solve a simple task to illustrate it:

  • Pass the dynamic array as the first argument of the function.
  • Call the function inside the main() block and pass the pointer at the first element of the array as the first argument.

Look at the result! Pay attention to the last two values!

Секція 2. Розділ 6
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt