Passing values directly as array to function in C [duplicate]
Asked Answered
R

1

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?

Roentgenology answered 19/9, 2016 at 9:58 Comment(2)
If you need pass an array, you need to have an array.Borodin
just to amend to what @Borodin said, ... only we can decide/control whether it is a named one or unnamed one. but it has to be there.Throttle
T
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.

Throttle answered 19/9, 2016 at 10:0 Comment(17)
Didn't know that.. C11 has interesting features... compiled just fine with gccHector
does this compile?Norven
@artm Ye, I think so. Did you get a failure? I'm interested.Throttle
@ringø that's actually C99.Talya
@ringø Compound LiteralsClintclintock
@SouravGhosh no worries, that works fineNorven
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.Atlee
@Atlee "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.Talya
@Atlee While technically you are very correct, I think OP does not want to create a separate array variable.Throttle
@Talya The question: "without having to create an array just for the sake of passing it".Atlee
@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.Atlee
@Atlee 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.Talya
@Talya 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?"Atlee
@Atlee Well, I took the hint from OP's approach, printArr( {3, 4, 5} );. It's better we ask OP for clarification then, shall we? :)Throttle
@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.Atlee
@Atlee 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.Talya
@Atlee The dupe, however is again based on the previous assumption which does not mention the "NO" part. Just saying. :)Throttle

© 2022 - 2024 — McMap. All rights reserved.