Complete example:
#include <stdio.h> void test(int arr[]) { int arrSize = (int)(sizeof(arr) / sizeof(arr[0])); printf("%d\n", arrSize); // 2 (wrong?!) } int main (int argc, const char * argv[]) { int point[3] = {50, 30, 12}; int arrSize = (int)(sizeof(point) / sizeof(point[0])); printf("%d\n", arrSize); // 3 (correct :-) ) test(point); return 0; }
Before passing it to a function, sizeof gives me the correct value. Doing the exact same thing on the exact same array in the function gives weird results. There's one element missing. Why?
sizeof() Operator to Determine the Size of an Array in CIt returns the size of a variable. The sizeof() operator gives the size in the unit of byte. The sizeof() operator is used for any data type such as primitives like int , float , char , and also non-primitives data type as an array , struct .
Arrays are static so you won't be able to change it's size. You'll need to create the linked list data structure.
Passing the array size tells the function where the bounds are so you can choose not to go beyond them. This is inherited from C language, if you use std::array or std::vector you don't need to use array size as a parameter function.
To determine the size of your array in bytes, you can use the sizeof operator: int a[17]; size_t n = sizeof(a); On my computer, ints are 4 bytes long, so n is 68. To determine the number of elements in the array, we can divide the total size of the array by the size of the array element.
Because array is decayed to a pointer when passed as function argument, so sizeof gives you 4 and 8 for 32- and 64-bit platforms respectively. Show activity on this post.
Using sizeof directly to find the size of arrays can result in an error in the code, as array parameters are treated as pointers. Consider the below program. Explanation: This code generates an error as the function fun () receives an array parameter ‘arr []’ and tries to find out the number of elements in arr [] using sizeof operator.
When you pass an array into a function in C, the array decays into a pointer to its first element. When you use sizeof on the parameter, you are taking the size of the pointer, not the array itself.
Because in C, C++, and Objective-C, functions cannot actually have array parameters. They only can have parameters that look like array parameters, but they aren't.
When you pass an array into a function in C, the array decays into a pointer to its first element. When you use sizeof
on the parameter, you are taking the size of the pointer, not the array itself.
If you need the function to know the size of the array, you should pass it as a separate parameter:
void test(int arr[], size_t elems) { /* ... */ } int main(int argc, const char * argv[]) { int point[3] = {50, 30, 12}; /* ... */ test(point, sizeof(point)/sizeof(point[0])); /* ... */ }
Also note that, for a similar reason (taking the sizeof
a pointer), the sizeof(point)/sizeof(point[0])
trick doesn't work for a dynamically allocated array, only an array allocated on the stack.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With