Wednesday, January 15, 2025
HomeProgrammingHow do I determine the size of my array in C?

How do I determine the size of my array in C?

In C, you can determine the size of an array using the sizeof operator. However, the method depends on whether the array is static (fixed size) or dynamic (allocated with malloc).

  1. For static arrays:
    c
    int arr[10];
    size_t size = sizeof(arr) / sizeof(arr[0]); // Calculates number of elements

    Here, sizeof(arr) gives the total memory size of the array, and sizeof(arr[0]) gives the size of one element.

  2. For dynamic arrays (allocated with malloc), you must manually track the size since sizeof only gives the size of the pointer.
RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x