8

I have a function which accepts an integer array as argument and print it.

void printArray(int arr[3])
{
   int i;
   for(i=0; i<3; ++i)
   {
      printf("\n%d", arr[i]);
   }
}

Is there a way to pass the values of the array like this

printArray( {3, 4, 5} );

if I know the values before hand without having to create an array just for the sake of passing it to the function?

J...S
  • 5,079
  • 1
  • 20
  • 35

1 Answers1

8

TL;DR The functions expects a (pointer to) an array as input argument, so you have to pass one. There's no way you can call this without an array.

That said, if you meant to ask "without creating an additional array variable", that is certainly possible. You can achieve that using something called a compound literal. Something like:

 printArr( (int []){3, 4, 5} );

should work fine.

To quote C11, chapter §6.5.2.5

[In C99, chapter §6.5.2.5/p4]

A postfix expression that consists of a parenthesized type name followed by a brace-enclosed list of initializers is a compound literal. It provides an unnamed object whose value is given by the initializer list.

That said, printArr() and printArray() are not same, but I believe that's just a typo in your snippet.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    Didn't know that.. C11 has interesting features... compiled just fine with `gcc` – Déjà vu Sep 19 '16 at 10:03
  • does this compile? – artm Sep 19 '16 at 10:04
  • @artm Ye, I think so. Did you get a failure? I'm interested. – Sourav Ghosh Sep 19 '16 at 10:04
  • 1
    @ringø that's actually C99. – Quentin Sep 19 '16 at 10:05
  • @ringø [Compound Literals](https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html) – LPs Sep 19 '16 at 10:07
  • @SouravGhosh no worries, that works fine – artm Sep 19 '16 at 10:08
  • This is just syntactic sugar for creating an array beforehand. Using a compound literal still creates an array. OP is asking how to do this without an array. – 2501 Sep 19 '16 at 10:09
  • 1
    @2501 "Is there a way to pass the values of the array like this" sounds like the question to which a compound literal is the answer. The array has to exist *somewhere*. – Quentin Sep 19 '16 at 10:09
  • 1
    @2501 While technically you are very correct, I think OP does not want to create a separate __array variable__. – Sourav Ghosh Sep 19 '16 at 10:10
  • 1
    @Quentin The question: "**without having to create an array just for the sake of passing it**". – 2501 Sep 19 '16 at 10:10
  • @SouravGhosh At no point is a variable mentioned. OP is clearly asking for a solution without an array. Using a compound literal is literally the exact opposite of what OP wants, because: it is creating an array for the sole purpose of passing it. – 2501 Sep 19 '16 at 10:11
  • 4
    @2501 Without an array at all, this is strictly impossible. You can very well go ahead and post an answer stating that if you reckon that is actually what OP is after. – Quentin Sep 19 '16 at 10:12
  • @Quentin I don't reckon or assume anything. My statements are factual based on the question. You are actually assuming OP wants to avoid a variable name, which is what a compound literal solves, but it doesn't solve OP's question, which is: "*if I know the values before hand without having to create an array just for the sake of passing it to the function?*" – 2501 Sep 19 '16 at 10:13
  • @2501 Well, I took the hint from OP's approach, `printArr( {3, 4, 5} );`. It's better we ask OP for clarification then, shall we? :) – Sourav Ghosh Sep 19 '16 at 10:13
  • @SouravGhosh OP op doesn't realize that compound literals create an array. OP is looking for a syntactic solution that doesn't exist. Given the question *without an array*, you answer is misleading. – 2501 Sep 19 '16 at 10:15
  • @2501 That's exactly what I mean. This answer is based on the assumption that the question is about syntax only, not a requirement to avoid creating any array at all. You have a valid point, but it is fundamentally at odds with this answer, and should be presented on its own rather than piggybacked. – Quentin Sep 19 '16 at 10:18
  • @2501 The dupe, however is again based on the previous assumption which does not mention the "NO" part. Just saying. :) – Sourav Ghosh Sep 19 '16 at 10:28