Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When we apply sizeof to an array name it returns the number of bytes in the array i.e. Number of elements multiplied by the size of individual element.
When sizeof is applied to a pointer it returns the size of a pointer i.e. 4 byte on 32 bit OS.
Since the array name points to the first element of the array, i.e array name is also a kind of pointer. So how the compiler\sizeof differentiate that the operand\parameter passed is an array not a pointer?
Posted

sizeof is an operator[^], not a function. Thus it "behaves" context dependent. If you download an open source c/c++ compiler and check for yourself, you will notice, that there are many actual sizeof operators declared, and in some cases it is more a macro, than a real operator.
 
Share this answer
 
Comments
Alok_Bond 15-Jul-13 6:42am    
I know sizeof is an operator not a function. My question is how does it differentiate b\w an array name and a pointer.
For Example : If we have int ar[] = {1,2,3,4,5}; , sizeof(arr) will return 5*4 = 20;
but If we have int *ptr; sizeof(ptr) returns 4. How does sizeof differentiate b\w the two.
Zoltán Zörgő 15-Jul-13 6:56am    
I know what you mean. But you tried to get it in the wrong way. It is the same as when you use addition (+) operator on a float, and int or a pointer. The compiler is aware of the type of the object you are using the operator on. This is why you can't use sizeof in the cases mentioned on the link I have given. You can not use it on dynamically allocated arrays for example, only on statically declared ones: because it has to be evaluable at compile time - although in general it is not evaluated on compile time.
This is like asking: "How the compiler differentiate between an array declaration and a pointer one?"
You know, the compiler parses the source code and it is able to differentiate the different arguments of sizeof operator because the programming language is not ambiguous.
 
Share this answer
 
Quote:
Since the array name points to the first element of the array, i.e array name is also a kind of pointer.

Yes and no. Yes, in many ways, you can use the array name like a pointer. But no, that doesn't make it a pointer. There are a couple of important distinctions:

1. You cannot assign another array to an array name .
2. As a consequence, you cannot allocate an array on the heap.
3. Likewise, you cannot delete or free an array name.

An array is a type that shares some of the behaviour of a pointer, in the same way that int variables and pointers share some of their arithmetic operators. They're still quite different types.


P.S.:
A pointer is like a street sign in a city. An array is like a street. While the street may start at the position of the street sign, that doesn't make the sign a street. And if you query the size of the sign, or the size of the street, you will notice a difference! ;)
 
Share this answer
 
v2
Comments
nv3 15-Jul-13 9:13am    
Nicely put!
You can treat this problem this way: builtin C array types have an automatic type conversion operator that can convert the array into a typed pointer (that points to the array).
An expression that evaluates to an array or array reference (for example the identifier of the array itself is one such expression) behaves as an array by default but this array expression can be automatically casted into a typed pointer by the compiler if the context requires that. In case of sizeof the compiler isn't forced to do the cast.

A conversion operator in action:
C++
class MyArray
{
public:
    MyArray()
    {
        memset(&m_Arr, 0, sizeof(m_Arr));
    }

    // conversion operators
    operator int*()
    {
        return m_Arr;
    }
    operator const int*() const
    {
        return m_Arr;
    }

    int& operator[](int index)
    {
        return m_Arr[5];
    }
    const int& operator[](int index) const
    {
        return m_Arr[5];
    }
private:
    int m_Arr[5];
};

void dojob(const int* p)
{
    printf("%p", p);
}

void funcc()
{
    MyArray arr;
    // the MyArray class can be casted to int* and const int*
    // by the compiler thanks to the conversion operators
    int* p = arr;
    dojob(arr);
}


EDIT: A type conversion operator (that I have written into my class) can convert any type to any other type but the conversion is used by the compiler implicitly only if it is necessary or if you ask the compiler to do so with a static_cast.

A pointer and an array are 2 different types, just there is a builtin auto conversion from array to typed pointer. Its also an accident that you can use both types as an array with the subscript (indexing []) operator. There is also pointer to array conversion in some cases:
C++
void tfunc(int arr[5])
{
}

void Test()
{
    int *p = NULL;
    tfunc(p);
}

This is not a real conversion because when you declare a function parameter as an array, then it is only a pointer and the size I specified doesn't really matter. sizeof(arr) would be the same as the size of a pointer.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900