How to find the size of an array (from a pointer pointing to the first element array)?

Cover Image for How to find the size of an array (from a pointer pointing to the first element array)?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Find the Size of an Array 📏 (from a Pointer Pointing to the First Element of the Array)

So, you've come across this interesting question: How can you find the size of an array from a pointer pointing to the first element? 🤔

The Context: 📝

Let's take a look at this code snippet to understand the context:

int main() 
{
    int days[] = {1,2,3,4,5};
    int *ptr = days;
    printf("%u\n", sizeof(days));
    printf("%u\n", sizeof(ptr));

    return 0;
}

In this code, we have an array called days which holds 5 integers. We also have a pointer called ptr that points to the first element of the days array.

The two printf statements are used to display the size of the days array and the size of the ptr pointer respectively.

The Problem: ❗

The question arises: Is there a way to find out the size of the array that ptr is pointing to, instead of just giving its size (which is four bytes on a 32-bit system)? 🤷‍♂️

The Solution: ✅

Unfortunately, there is no way to directly find the size of an array from a pointer pointing to its first element. 😕

However, we can use a workaround by calculating the size indirectly. One way to achieve this is by dividing the size of the pointer by the size of the data type the pointer points to.

For instance, if ptr is a pointer to an int, we can calculate the size of the array days as follows:

size_t arraySize = sizeof(days) / sizeof(int);

Putting It All Together: 💡

Let's modify our original code snippet to include the calculation for the size of the array:

#include <stdio.h>

int main() 
{
    int days[] = {1,2,3,4,5};
    int *ptr = days;
    size_t arraySize = sizeof(days) / sizeof(int); // Calculate the size of the array

    printf("%u\n", arraySize); // Display the size of the array
    printf("%u\n", sizeof(ptr)); // Display the size of the pointer

    return 0;
}

When we run this modified code, it calculates and displays the size of the days array using the arraySize variable.

Conclusion: 🎉

Although we cannot directly find the size of an array from a pointer pointing to its first element, we can use the size of the pointer and the size of the data type to calculate it indirectly.

Now you have the knowledge to overcome this common challenge when working with arrays and pointers in C. Happy coding! 💻

Your Turn: ✏️

Have you ever encountered a different way to find the size of an array from a pointer? Share your thoughts and experiences in the comments below. Let's learn together! 🌟


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello