2

I wrote a function as :

string par(int a[]){
int s=sizeof(a)/sizeof(*a);
cout<<s<<endl;
/*
    ..do something
*/

}

The main function is written as:

nt main(){
 int a[]={1,5,11,5};
 cout<<sizeof(a)/sizeof(*a)<<endl;
 cout<<par(a)<<endl;

}

The output I get is:

4

1

while I believe it should be the same as I passed the same array. Kindly point out the mistake..Thanks..

Community
  • 1
  • 1
ejjyrex
  • 1,151
  • 1
  • 10
  • 13
  • 1
    Arrays decay into pointers when passed into functions. This may be what you are seeing –  May 02 '13 at 17:57
  • @squiguy: Yes. The main difference is that with C++ you'd use a standard library "vector<>", which *does* give you the size. – paulsm4 May 02 '13 at 18:54

4 Answers4

2

In C++ when pass an array as an argument to a function, actually you're passing a pointer to an array.

Since the size of a pointer and an int is 4 or 8 (depending on ABI, and since you're getting 1 then I guess you have a 32-bit machine) you're getting 4/4 which is 1.

So

int s=sizeof(a)/sizeof(*a); is 1.

You should pass the size as an argument instead of trying to calculate it inside the function:

string par(int a[], int size)
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • Thanks for the comment and the answer ..but help me to solve this ..I mean I need to be able to find the way out to access all elements in the array with the size... – ejjyrex May 02 '13 at 18:01
  • As I stated in my answer, you should pass the size as an argument. – Maroun May 02 '13 at 18:02
  • @downvoter, any comment so I can learn from my mistakes?..... – Maroun May 02 '13 at 18:05
  • nitpick: sizeof(a) is 16 before heading into the function (4 * 4 = 16). –  May 02 '13 at 18:09
  • @0A0D Can you please explain more? I didn't say sizeof(a) is not 16 before heading into the function. – Maroun May 02 '13 at 18:11
  • I was pointing out, for clarity sake and future readers the difference between what is determined inside the function after decay and before going into the function –  May 02 '13 at 18:11
  • @0A0D I don't think this worth a downvote, I didn't say something wrong man, anyway, thanks. – Maroun May 02 '13 at 18:12
  • @paulsm4 didn't mention this either, you didn't downvote him. I think you're downvoting only me :) – Maroun May 02 '13 at 18:18
  • No, I only downvote incomplete or altogether wrong answers. If you feel the need to retaliate, then so be it. –  May 02 '13 at 18:21
  • Actually I don't. Peace. – Maroun May 02 '13 at 18:30
1

You need to pass the size of the array into the function, you cannot compute a size from "x[]":

string par(int a[], int size){

paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

passing string par(int a[]) is actually not passing an array but passing the address of the array i.e. string par(int *a). So the function does not know that an array of 4 elements has been passed. It knows a pointer to integer array has been passed. Hence the function returns 1 as the array size.

abasu
  • 2,454
  • 19
  • 22
1

See Question 6.4 and 6.21 in the C FAQ

Q: Why doesn't sizeof properly report the size of an array when the array is a parameter to a function? I have a test routine

f(char a[10])
{
    int i = sizeof(a);
    printf("%d\n", i);
}

and it prints 4, not 10.

A: The compiler pretends that the array parameter was declared as a pointer (that is, in the example, as char *a; see question 6.4), and sizeof reports the size of the pointer. See also questions 1.24 and 7.28.

This is what 7.28 says:

Q: Why doesn't sizeof tell me the size of the block of memory pointed to by a pointer?

A: sizeof tells you the size of the pointer. There is no portable way to find out the size of a malloc'ed block. (Remember, too, that sizeof operates at compile time, and see also question 7.27.)

  • 1
    This is an over-complicated reply to the simple answer "C doesn't store metadata (like array size) along with the array". All the subroutine sees is a pointer - and that's not enough to determine size. I was perhaps a bit too succint; Maroun Maroun gave an excellent explanation. IMHO... – paulsm4 May 02 '13 at 18:51
  • @paulsm4: Better to give an explanation not just for the OP but for the rest of the Internet. The question was answered here even if it wasn't the accepted one. It explains the differences the OP was seeing and why sizeof reports a different size. –  May 02 '13 at 18:53