Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define new function, array, struct etc inside of parameter of function call [duplicate]

If you had a function which took the following:

void foo(char **arr);

How can you do the following:

void foo(char* x[] = { "hello", "my", "friend" });

If this confuses you, in Java we do this by the following:

public void foo(String[] x);

foo(new String[] { "hello", "my", "friend" });

Currently, I do the following in C which I hate because it looks really ugly:

char* myArr[] = 
    { 
        "hello", "my", "friend"
    };

foo(myArr);
like image 634
Hatefiend Avatar asked Dec 30 '25 21:12

Hatefiend


2 Answers

How can you do the following:

void foo(char* x[] = { "hello", "my", "friend" });

You nearly made it ... ;-)

If doing C99 or newer use a compound literal like this:

foo((char *[]){"hello", "my", "friend"});

Mind that the called function (foo() here) has no clue how many elements the pointer array has, so you want to add a final null-pointer as sentinel:

foo((char *[]){"hello", "my", "friend", NULL});

Example:

#include <stdio.h>
#include <stdlib.h> /* for EXIT_xxx macros */


void foo(char **arr)
{
  while (arr && *arr)
  {
    printf("%s\n", *arr);
    ++arr;
  }
}

int main(void)
{
  foo((char *[]){"hello", "my", "friend", NULL}); /* Mind the final NULL. */

  return EXIT_SUCCESS;
}

This will print:

hello
my
friend

The compound literal is valid until the scope it got defined in is left (main() here). If you want to make sure it gets removed from the stack immediately after its usage put braces around the call to foo() creating a local scope/block:

int main(void)
{
  {
    foo((char *[]){"hello", "my", "friend", NULL}); /* Mind the final NULL. */
  }

  /* The compound literal passed to foo() is already deallocated here, had been 
     removed from the stack. */

  ...
like image 89
alk Avatar answered Jan 02 '26 13:01

alk


Java and C are different languages with different idioms.

If it were me, I'd refrain from trying [too hard] to coerce C into "Java-like". Embrace each language on its own merits.

For your first example, the "ugly" one, you could use a CPP [C preprocessor] macro--a concept that does not exist in Java:

#define FOO(_av...) \
    do { \
        char *myArr[] = { _av, NULL }; \
        foo(myArr); \
    } while (0)

FOO("hello", "my", "friend");

But, this would probably be regarded by many as "too cute". Better to create a table of some sort.

Whenever Java does a new it is doing a heap allocation [which is slow]. That's partly because everything has to be "on the heap", more or less.

C can do this with malloc, but a good C programmer will try to avoid unnecessary heap allocations because it has globals, statics, and function scoped variables.

like image 31
Craig Estey Avatar answered Jan 02 '26 13:01

Craig Estey