


How to Determine the Size of an Array Passed as a Function Argument in C ?
Nov 09, 2024 am 10:36 AMArray Size Determination in C Function Parameters
Unlike in the main function, determining the size of an array passed as an argument to a function in C using sizeof() requires a reference template. This is because arrays decay to pointers when passed to functions.
Array Decay to Pointers
Consider the following code snippet:
1 |
|
Despite the declaration with square brackets, some_list decays to an integer pointer int* when passed as an argument. As a result, sizeof(some_list) returns the size of a pointer, not the array size.
Reference Template Solution
To determine the size of an array correctly, use a reference template. For example:
1 2 3 4 5 |
|
Exception: Multidimensional Arrays
There is one exception to the array decay rule. Multidimensional arrays retain their dimensionality and are not passed as pointers. Hence, sizeof() can be used directly to determine their size:
1 2 |
|
The above is the detailed content of How to Determine the Size of an Array Passed as a Function Argument in C ?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the types of values returned by c language functions? What determines the return value?

C language function format letter case conversion steps

What are the definitions and calling rules of c language functions and what are the

Where is the return value of the c language function stored in memory?

How does the C Standard Template Library (STL) work?

How do I use algorithms from the STL (sort, find, transform, etc.) efficiently?
