If you have your array in scope you can use sizeof to determine its size in bytes and use the division to calculate the number of elements:
#define NUM_OF_ELEMS 10
int arr[NUM_OF_ELEMS];
size_t NumberOfElements = sizeof(arr)/sizeof(arr[0]);
If you receive an array as a function argument or allocate an array in heap you can not determine its size using the sizeof. You'll have to store/pass the size information somehow to be able to use it:
void DoSomethingWithArray(int* arr, int NumOfElems)
{
for(int i = 0; i < NumOfElems; ++i) {
arr[i] = /*...*/
}
}
Answer from Pavel Zhuravlev on Stack OverflowIf you have your array in scope you can use sizeof to determine its size in bytes and use the division to calculate the number of elements:
#define NUM_OF_ELEMS 10
int arr[NUM_OF_ELEMS];
size_t NumberOfElements = sizeof(arr)/sizeof(arr[0]);
If you receive an array as a function argument or allocate an array in heap you can not determine its size using the sizeof. You'll have to store/pass the size information somehow to be able to use it:
void DoSomethingWithArray(int* arr, int NumOfElems)
{
for(int i = 0; i < NumOfElems; ++i) {
arr[i] = /*...*/
}
}
int a[20];
int length;
length = sizeof(a) / sizeof(int);
and you can use another way to make your code not be hard-coded to int
Say if you have an array array
you just need to:
int len = sizeof(array) / sizeof(array[0]);
hi, I'm new in C. I'm having a hard time finding the number of elements in an int array.
Here's what I'm trying to achieve.
I've defined a macro#define MAXSZ 30
Then I've declared an arrayint arr[MAXSZ];
What I wanted to do, is, taking user input to initialize the array up until the user inputs -1. Then get the number of elements.int item;for(int i = 0; i < MAXSZ; i++) {scanf("%d", &item);if (item == -1) break;else arr[i] = item;}
Now, when I'm trying to get the number of elements.
For input12356-1
It shows 30 elements (as it's supposed to).
Output300x7ffe9be05320 -> 10x7ffe9be05324 -> 20x7ffe9be05328 -> 30x7ffe9be0532c -> 50x7ffe9be05330 -> 60x7ffe9be05334 -> 325720x7ffe9be05338 -> -12939427840x7ffe9be0533c -> -1823360x7ffe9be05340 -> -13812227900x7ffe9be05344 -> 325720x7ffe9be05348 -> 0
...
My question is, how do I get rid of the garbage values & only get the number of items that has been given as input?
Count the number of elements in an array in C - Stack Overflow
C: finding the number of elements in an array[] - Stack Overflow
calc. number of elements in array - C++ Forum
c - How do I determine the number of elements in an array? - Stack Overflow
Videos
In C, you can only get the size of statically allocated arrays, i.e.
int array[10];
size = sizeof(array) / sizeof(int);
would give 10.
If your array is declared or passed as int* array, there is no way to determine its size, given this pointer only.
You are most likely doing this inside the function to which you pass the array.
The array decays as pointer to first element So You cannot do so inside the called function.
Perform this calculation before calling the function and pass the size as an function argument.