How to Pass a Constant Array Literal to a Function That Takes a Pointer Without Using a Variable C/C++

How to pass a constant array literal to a function that takes a pointer without using a variable C/C++?

You can do it in C99 (but not ANSI C (C90) or any current variant of C++) with compound literals. See section 6.5.2.5 of the C99 standard for the gory details. Here's an example:

// f is a static array of at least 4 floats
void foo(float f[static 4])
{
...
}

int main(void)
{
foo((float[4]){1.0f, 2.0f, 3.0f, 4.0f}); // OK
foo((float[5]){1.0f, 2.0f, 3.0f, 4.0f, 5.0f}); // also OK, fifth element is ignored
foo((float[3]){1.0f, 2.0f, 3.0f}); // error, although the GCC doesn't complain
return 0;
}

GCC also provides this as an extension to C90. If you compile with -std=gnu90 (the default), -std=c99, or -std=gnu99, it will compile; if you compile with -std=c90, it will not.

C - Send array to function without declaring it

Yes. You can. In C99/11 you can do by using compound literals:

C11: 6.5.2.5 Compound literals:

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

print_int_array((int[]){1, 1, 2, 3, 5}, 5);  

99) Note that this differs from a cast expression. For example, a cast specifies a conversion to scalar types
or void only, and the result of a cast expression is not an lvalue.

C initialize pointer to array literal without extra variable

In C99 and C11, you can use a compound literal to achieve what is requested in the question title, namely initialize a pointer so it points to an array literal without any extra variables:

int *numList = (int []){ 1, 2, 3 };

The array that numList points to has the same scope and lifetime as the pointer itself — it lasts for the block it is defined in if it is inside a function, or for the duration of the program if it is outside any function.

If you were so misguided as to create static int *numList = (int []){ 1, 2, 3}; inside a function, you'd be setting yourself up for a world of hurt — don't do it. (At file scope, outside any function, it doesn't present any problem.) The pointer would be initialized no later than when the function is first called, but there's no guarantee that what it points to has static duration and subsequent calls could be pointing at garbage.

Define new function, array, struct etc inside of parameter of function call


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. */

...

Literal array in C

This syntax is called array initializer. Therefore, it can be used only when you define your array.

C11 (n1570), § 6.7.9 Initialization

initializer:
assignment-expression
{ initializer-list }
{ initializer-list , }

However, in C99, it is possible to do it with compound literals:

mote((char[]){0x00, 0x01, 0x03});

How to pass array to function without variable instantiation, in C++

It can't be done in the current C++, as defined by C++03.

The feature you are looking for is called "compound literals". It is present in C language, as defined by C99 (with C-specific capabilities, of course), but not in C++.

A similar feature is planned for C++ as well, but it is not there yet.

passing mixed const integer ASCII values and const char array in C and passing const int without previous declaration

Answering the first your question i can say you can do it using the compound literal.. For example

#include <stdio.h>

void f( const int a[], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
}

int main(void)
{
f( ( int[] ) { 1, 2, 3 }, 3 );
}

But you can not have an array with elements of different types.

So for such a construction {1,2,3,"test"} you need to split the string literal into separate characters like {1,2,3, 't', 'e', 's', 't'}

Why this code works ? memcpy on a constant array

This

(int[3]) { 7, 8, 9 }

is a compound literal of the type int[3] that used as a function argument is implicitly converted to a pointer to its first element.

On the other hand (The C Standard, 6.3.2.3 Pointers)

1 A pointer to void may be converted to or from a pointer to any
object type. A pointer to any object type may be converted to a
pointer to void and back again; the result shall compare equal to the
original pointer.

and

2 For any qualifier q, a pointer to a non-q-qualified type may be
converted to a pointer to the q-qualified version of the type; the
values stored in the original and converted pointers shall compare
equal.

So in this call

memcpy(tab, (int[3]) { 7, 8, 9 }, sizeof(int) * 3);

the compound literal is converted at first to a pointer of the type int * to its first element that then is converted to the type void * according to the first quote and then to the type const void * according to the second quote.

The qualifier const means that the pointed object will not be changed within the function. It does not mean that the expression used as an argument is a constant expression.



Related Topics



Leave a reply



Submit