-1

If we consider the function

int sum(int* nums, size_t len)
{
    int sum = 0;
    for(int i = 0; i < len; i++) sum += nums[i];
    return sum;
}

We could call it using an lvalue as such

int[] nums = {1,2,3};
sum(nums, 3);

But when trying to use an rvalue it doesn't work.

sum({1,2,3}, 3);

Is there a way to get this to work?

mdre
  • 81
  • 7
  • you have a return statement in a void function. Either remove the retrurn or make the sum function's return type `int` – ghilesZ Jan 30 '21 at 10:14
  • You are completely right, sorry, i didn't pay that much attention to the example since that was not the focus but will fix it up rn – mdre Jan 30 '21 at 10:15
  • It doesn't work anyway, because your `sum` function returns `void`. But you could use [compound literals](https://en.cppreference.com/w/c/language/compound_literal): `sum((int[]){1,2,3}, 3);`. You could even wrap `sum` in a macro that automatically determines the length of the array. Or you could just write `1 + 2 + 3`. (I think that what you want is not a frequent use case in C.) – M Oehm Jan 30 '21 at 10:16
  • Well of course this is a minimal example, I actually want to process a char** – mdre Jan 30 '21 at 10:22
  • Fair enough. I can imagine something like `key_in(str, {"red", "green", "blue", NULL})`. – M Oehm Jan 30 '21 at 10:55
  • Well, as I stated in my question, such an approach doesn't work, giving the error "no instance of overloaded function .... arument types are ({...}, int), and compound literals don't work with the IDE – mdre Jan 30 '21 at 12:20
  • What exactly are you trying to achieve? There may be other options. – M Oehm Jan 30 '21 at 12:32
  • Okay, we have a `static const char*` and we want to create that from a set of other `const char *`s. So we have a function taking in a `const char *[]` and the size of that array to return the value. – mdre Jan 30 '21 at 12:51
  • Hm, and you concatenate them? Or just determine the sum of their lengths? I don't really see the need to include the string array in the function call verbatim. (C can do a lot of things, but it is not as terse as newer languages.) – M Oehm Jan 30 '21 at 14:01

1 Answers1

0

You can use a compound literal (which is available in C99 or later) and pass it directly:

$ cat t.c
#include <stdio.h>

int sum(int* nums, size_t len)
{
    int sum = 0;
    for(int i = 0; i < len; i++) sum += nums[i];
    return sum;
}

int main()
{
    int rc = sum((int []){1,2,3}, 3);
    printf("%d\n", rc);
}
$ gcc -Wall -Werror -std=c18 -pedantic t.c
$ ./a.out 
6

The lifetime of the compound literal is its enclosing block - in this case, it's the main function.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • interestingly enough this gives me a "cast to incomplete array type “int []” is not allowed" error – mdre Jan 30 '21 at 10:23
  • What's your compiler and does it support C99? How are you compiling (command/options)? – P.P Jan 30 '21 at 10:24
  • I am just using plain VS. The IDE tells me the aforementioned error, while at build I receive "a parenthesized type followed by an initializer list is a non-standard explicit type conversion syntax" – mdre Jan 30 '21 at 10:27
  • MS doesn't support all the C features. I suspect C99's compound literals is one of them. – P.P Jan 30 '21 at 10:45
  • Hmm, strange. So basically I wanted to create a static const from a function that receives an array. Do you see any other way of doing so given compound literals are not an option? – mdre Jan 30 '21 at 11:37