0

Possible Duplicate:
How to find the sizeof(a pointer pointing to an array)

I want to pass an array to a function such that if I use the sizeof operator on array inside the function body, I should get the actual size of the array and not that of a pointer. For e.g -

int main()
{
    int arr[] = {1,2,3,4,5,6,7,8,9,0};

    printf("Array size = %d\n", sizeof(arr)/sizeof(int)); ---> O/P = 40/4 = 10

    func(arr);
}

void func(int arr[])
{
    printf("Array size = %d\n", sizeof(arr)/sizeof(int)); --->O/P = 4/4 = 1
}

The printf statement in the main function prints 10 while the one in func prints 1. Is it possible to do so in C language?

EDIT: I know other alternatives like wrapping the array into a struct etc. Actually, I was trying to solve a problem given on TopCoders site and the requirement of one problem is that I have to write a function(requirement given below). Now I am confused that how I am going to calculate the size of the int[] donations array inside int maxDonations(int[] donations) function.

Definition
Class:  BadNeighbors
Method: maxDonations
Parameters: int[]
Returns:    int
Method signature:   int maxDonations(int[] donations)
(be sure your method is public)

If you want, I can post the entire question.

Community
  • 1
  • 1
Ravi Gupta
  • 6,258
  • 17
  • 56
  • 79
  • Dupe, undoubtedly many times, no it's not possible but there are only a million other ways to accomplish what you want. – Chris Lutz Mar 04 '12 at 07:45
  • @ChrisLutz: I have edited the question to include what I am trying to accomplish. – Ravi Gupta Mar 04 '12 at 08:03
  • 2
    the syntax of the problem specification clearly show that they are not talking about C – Jens Gustedt Mar 04 '12 at 08:17
  • 1
    Did you choose the language on the top right corner of the problem description? I guess you didn't, I remember topcoder's default language is Java. – pjhades Mar 04 '12 at 09:11

3 Answers3

3

A cheap way would be to wrap the array into a struct and pass that (or a pointer to the struct).

However, I would recommend passing an additional size_t argument that specifies the size of the array. This would make your function more flexible.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • I would also prefer passing extra argument but the problem statement says that I have to write such function. See the EDIT section, I have posted the requirement also. – Ravi Gupta Mar 04 '12 at 08:02
0

In C you cannot pass an array as a parameter to a function directly. You can pass a structure, a pointer or some other numeric value, but not an array.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
0

Based on the constraints in your question, this question is impossible to answer.

In C, sizeof is an operator, rather than a function: you correctly mention this in your question. However, C operators are evaluated at compile time, rather than runtime, which means that you cannot use sizeof in a function in the way you want to - at compile time the compiler cannot tell how func() is going to be called.

Looking at the problem as set, I am assuming it is this one:

http://www.cs.duke.edu/csed/algoprobs/neighbors.html

The required method signature on that page is:

public int maxDonations(int[] donations)

although for some reason, you've omitted the public keyword in your question. This keyword indicates that the language in question is not C, and this example solution looks very much as if it is written in Java, in which language this is a non-problem, arrays have a length member.

I'd suggest you clarify the requirements for the question from your teacher/website/whatever to check what is really expected.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
Tim
  • 9,171
  • 33
  • 51