In java is it possible and if not how to calculate in c?
-
Can you provide more specifics like what kind of array? array of chars? ints? How did you declare the array? ie. char x[] = { 'a', 'b', 'c' }; ? – hopia Jan 18 '12 at 19:23
-
It can be any kind of array, that is not a problem. Take for example char x[]= {'a','b','c' }; – Pisto Jan 18 '12 at 19:26
-
There is no reason to calculate a length in Java as its a field of the array type. – Peter Lawrey Jan 18 '12 at 20:15
5 Answers
if it's really array (not a pointer), you can do sizeof(arr)/sizeof(*arr)

- 5,438
- 1
- 16
- 22
-
1That don't work if `arr` is a formal parameter to a function. – Basile Starynkevitch Jan 18 '12 at 20:07
-
You can't evaluate the array's length in the function if the array was passed as parameter. – Gangnus Jan 18 '12 at 20:49
-
2Array parameter in function is not really array. it's a pointer. the compiler translate `int arr[]` to `int *arr`. (that's also why you don't need to write the size of the array) – asaelr Jan 18 '12 at 21:02
There is no way to calculate that. in C (not C++, which has std::array
and std::vector
) an array is transmitted as its pointer, which you might increase by some offset. So you really don't know the runtime size of an "array", except by some conventions.
In particular for formal arrays, there is no way to know the size of the actual array passed
e.g. as void f(int arr[]) { /*...*/ }
unless you give a static dimension.
Likewise, with an external array declared as extern int xarr[];
you cannot get its dimension with sizeof(xarr)/sizeof(xarr[0])
.

- 223,805
- 18
- 296
- 547
-
No, `sizeof(a)/sizeof(a[O])` does not work when `a` is a formal parameter. So Asaelr is wrong! – Basile Starynkevitch Jan 18 '12 at 20:06
-
What is a formal parameter? You mean a function parameter? You simply can't pass arrays to functions. `int toto(int A[])` is exactly the same as `int toto(int * A)`. So a function never "sees" an array. – Jens Gustedt Jan 18 '12 at 22:14
-
But `void toto[int a[]) { /*...*/ }` is perfectly valid syntax. What do you call an array; why should `a` don't be called a formal parameter array since it has the syntax of an array? (I do know that it is implemented as a pointer). – Basile Starynkevitch Jan 19 '12 at 06:02
-
Besides your typo it is perfectly valid syntax. But the standard prescribes that if you put an array declaration as a function parameter, this is interpreted as a pointer parameter in the way I indicated. The array syntax in function parameters has just a different semantic than when you apply it in a variable declaration. – Jens Gustedt Jan 19 '12 at 06:56
-
Again, what is the meaning of array for you in C? For me, it is just the syntax `arr[]` ! – Basile Starynkevitch Jan 19 '12 at 08:06
-
then you are not in phase neither with the standard nor with common practice. An array is a *variable* that is declared as such. For function parameter the "innermost" dimension always falls back to a pointer definition. So inside the function you will always see a pointer to the first element of the object, and the semantic of such a beast is simply different of what would happen if you declared an array *inside* the function. – Jens Gustedt Jan 19 '12 at 08:54
-
flexible array in structures are permitted in C99, and they are absolutely not variables. See http://stackoverflow.com/questions/246977/flexible-array-members-in-c-bad – Basile Starynkevitch Jan 19 '12 at 08:58
-
@Jens: I do know the difference between arrays and pointers in C (since I'm working inside GCC and MELT). I am only playing on words. I insist that on some occasions in C, you use an array syntax without knowing the runtime dimension (i.e. the size in memory) of that array. – Basile Starynkevitch Jan 19 '12 at 09:00
In Java, the length of a primitive array is array.length
, while the length of an ArrayList
(and most other collections) is arrayList.size()
In C, the length of an array is sizeof(array) / sizeof(array[0])
, but this is nearly useless since you can't pass arrays as arguments (they degenerate to pointers). The normal way to find the size of an array in C is to pass it as an extra argument to the function, or sometimes to terminate it with a sentinel value (eg. strings are \0
terminated)

- 6,768
- 2
- 27
- 35
-
I also used array.length but my professor said that it is wrong which is why this question came up. – Pisto Jan 18 '12 at 19:22
-
2`array.length` is right for primitive arrays, but if you were using `ArrayList`s you should use the `size()` method instead. – sverre Jan 18 '12 at 19:25
-
In C, there is no way of calculating the size of array if you have only a pointer to it. You must store it in separate variable.
In fact you HAVE TO keep the size of an array in separate variable because you have to allocate memory if you want to use dynamic-size array.
And if you want to use fixed-size array you know it's size by the time you're writing your code so why not use #define, variable or const to store it?
Java is totally different language than C and the philosophy of programming is different-you should always keep that in mind.
In Java, you should use array.length, look here for example: http://www.roseindia.net/help/java/a/java-array-length.shtml

- 33,079
- 4
- 27
- 32
Simply, there is no possibility if you recieve only a pointer. That's why main
has an argc
argument. It defines the number of entries in argv
. If you have an array "datatype" (actually the same as a pointer, but the behaviour depends on the context), you can use
int[] arr = new int[10];
sizeof(arr)/sizeof(int) // or whatever type is contained in ``arr``

- 16,299
- 28
- 108
- 203
-
@JensGustedt Sorry, you're right. Doesn't the same routine work in *C* using `malloc`? – Niklas R Jan 18 '12 at 20:05
-
no, not as easily. In any case your syntax is completely out of scope for C as well as for C++. Even if you'd put `int arr[]`, which would be the correct one, you can't initialize an array with a pointer. You can declare an array on the stack simply as `int a[10]` and for this then the trick that you mention would work. An answer in the same spirit but with broader coverage was already given by asaelr before yours. – Jens Gustedt Jan 19 '12 at 10:21