As in topics title. When I write: void fun(int *tab){}
is it the same as writing void fun(int tab[]){}
?
Asked
Active
Viewed 590 times
0

nullpointer
- 245
- 3
- 6
- 15
-
If you don't count the extra character in `[]`... – jrok Nov 02 '13 at 16:49
-
@jrok: what do you mean? What 'extra character'? – nullpointer Nov 02 '13 at 16:50
-
2`*` is one character, `[]` is two... – StoryTeller - Unslander Monica Nov 02 '13 at 16:51
-
@StoryTeller: Oh, right – nullpointer Nov 02 '13 at 16:52
-
For readability if a function is expecting base address of an array, you can write the later one. If its just a pointer, you can write former one. – Sam Nov 02 '13 at 17:09
3 Answers
3
Yes.
void fun(int *tab){}
void fun(int tab[]){}
void fun(int tab[10]){} //whatever the size is
are all the same to the compiler. The array, with the size or not, decays to a pointer when passed as a function argument.
In practice, avoid using the last one, as it may give the implication that the size is known to the function, while actually it's not.

Yu Hao
- 119,891
- 44
- 235
- 294
-
which makes it some of the most misleading syntactic sugar to a novice programmer... – StoryTeller - Unslander Monica Nov 02 '13 at 16:54
-
@YuHao: just one more question before I accept your answer ;) Why would I ever write the last one, I mean: `void fun(int tab[10]){}` - whats the point of doing this? What exactly does it mean? – nullpointer Nov 02 '13 at 17:08
-
1@nullpointer Arrays have a size, like `int arr[10];`, just when an array is passed as function argument, it's converted to a pointer to the first element, so the size is ignored. – Yu Hao Nov 02 '13 at 17:10
2
Yes it is, and no, the second version won't let you figure out the size of the array inside the function. It still decays to a pointer. :)

Luchian Grigore
- 253,575
- 64
- 457
- 625
-
1How could I figure out the size of the array with the first version? – nullpointer Nov 02 '13 at 16:51
-
@nullpointer, You can't. Use `std::array` or `std::vector` or something. – chris Nov 02 '13 at 16:51
-
@nullpointer directly you can't. Use a `std::vector`, pass the size as a parameter, or mark the last element with a special value. – Luchian Grigore Nov 02 '13 at 16:52
-
@nullpointer, you can't do that in the first version either. But some use the second in a misguided attempt to do so. – StoryTeller - Unslander Monica Nov 02 '13 at 16:52
-
@jrok: I always do that. But it was interesting what Luchian Grigore wrote, I thought that there is another 'magic' way of doing this ;) – nullpointer Nov 02 '13 at 16:53
-
@nullpointer also read http://stackoverflow.com/questions/7966964/what-does-this-c-code-mean – Luchian Grigore Nov 02 '13 at 16:54
1
Yes it is same..Whenever you declare an array say int tab[] in your program.. and if you want to pass that array to some other function then you actually pass the base address location of that array i.e. &tab[0], which is pointed by pointer that you use as argument in function, int *tab... This actually is call by reference..

Praful Surve
- 788
- 1
- 10
- 22