int a[]={1,3,4,5,7};
    int b[]={2,3,5,6};

    int m=sizeof(a)/sizeof(int);

    int n=sizeof(b)/sizeof(int);
     cout<<m<<" "<<n<<endl;

this code prints 5,4. but when i pass the arrays to some function and then i print the m,n values then it is printing 1,1. i know that they are acting as pointer when i pass then in function and sizeof pointer is 4 bytyes and so of int. so it is 1. but why is it not same in main function ? any specific reason ? thanks alot.

In the main function they are not pointers they are arrays and the size of an array is the sum of the size of each its elements. When you divide by the size of a single element you get the number of elements in the array.

So in this code example

int a[]={1,3,4,5,7};

int* p = a;

int m=sizeof(a)/sizeof(int);

int n=sizeof(p)/sizeof(int);

a is an array with 5 int elements (assuming that an int is 4 bytes) the size of a is 4 * 5 = 20. m is set equal to 20 (size of a) divided by 4 (size of int) which is 5, the number of entries in a.

By contrast p is a pointer it has a size of 4 bytes (on a 32 bit OS) although it points to a the size of a is not part of the information storred in a pointer so n is set equal to 4 (size of a pointer) divided by 4 (size of int) which is 1.

Despite what you see here and there on the internet it is very important to remember that arrays and pointers are not the same thing they are really quite different objects, it is just quite easy to convert an array to a pointer (line 3 of my example).

A minor point unrelated to the question: the cout object and it's selectors only found in C++, not C.

@banfa can you explain a little more ?

secondly, can you tell me how to the size of the array in any other function in which i am passing the array ? condition is that i can't send the size of array from the calling function. okay!

There is no way to know the size of an array that has been passed to a function without also passing the size of the array. The best you can do is to know something special about the array; for example, maybe you make the first element the size of the array, or you set the last element to some special value that no other element will have.

As moschops said in a function

void function(int* p)
{
}

That has been passed some arbitary array then there is not way to know what the size of that array is from the passed parameter. Standard practice is to pass the array size in another function parameter

void function2(int* p, int size)
{
}

If you are not allowed to do that but you are using fixed size arrays then you could

Use a constant to define your array size

#define SIZE (5)

int array[SIZE];

void function3(int* p)
{
  int ix;

  for(ix=0; ix<SIZE; ix++)
  {
    p[ix] = ix;
  }
}

Or declare the function to take a point to an array of the right size

void function4(int (*p)[5])
{
  int count = sizeof(*p)/sizeof(int);
}

can you please clear me when p is giving me the sizeof array and when it will act as a pointer ? in your example, you are saying sizeof(p) is sizeof array it is giving to me, can you please explain little bit ? thanks to you.

When p is int* p it is a pointer to an int, taking sizeof(p) gives the size of a pointer (4).

When p is int (*p)[5] it is a pointer to an array of 5 ints, taking sizeof(p) still returns the size of a pointer (4). Look at my code again, I did not use sizeof(p) in my final example. I used sizeof(*p) i.e. not the size of the pointer p but the size of what p is pointing to.

If p is int* p the sizeof(*p) is still 4 because p is pointing to an int so this is sizeof(int). However if p is int (*p)[5] then p is pointing to an array of 5 ints and sizeof(*p) is the size of an array of 5 ints or 20 because that is what p is pointing to.

In summary in my code p was always a pointer and taking sizeof(p) always returned the size of a pointer. What I changed is what p was pointing to, an array of ints rather than a single int and then I got the size of what p was pointing to not the size of p sizeof(*p).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.