Defining an array inside a function argument in C - c

Is there a way to define an array inside a function argument in C? Something like this:
void arraysomething(double vec[2]){
//do something...
}
int main(){
arraysomething({1.,2.});
return 0;
}
I searched all over the place and found nothing.

What you're looking for is called a compound literal:
arraysomething((double []){1.,2.});
The syntax looks like an initializer for an array or struct preceeded by what looks like a cast to the type in question.
Because you want to pass in array, the typename given should be double [], which means an array of unspecified size. The actual size isn't needed because it is inferred from the number of elements in the literal.

Related

Multi dimensional array reference in struct in c

If I have a multidimensional array like this:
int arr2d[2][3] = {{0,1,2}, {10,11,12}};
I can pass it to a function like this:
void foobar(int arg[][3])
This is not a call by value, this is call by reference, so just an pointer to the start address, but the compiler still knows it is a 2D array and I'm able to access it like one in the function.
Now how does the same work in a struct?
typedef struct {
int arr2d[][3];
} Foobar_t
First this gives me: error: flexible array member in otherwise empty struct. I can fix this by doing so:
typedef struct {
int dummy;
int arr2d[][3];
} Foobar_t
It will compile without errors or warnings. But when I try to use it like Foobar_t foobar = {1337, arr2d} I get some warnings:
missing braces around initializer
initialization makes integer from pointer without a cast
And when accessing it: subscripted value is neither array nor pointer nor vector.
One dimensional arrays can easily be treated as pointers. But for multi dimensional arrays the compiler needs to know the size of the different dimensions to calculate the offsets correctly. Is there a way without cast (int (*)[3]) and why does the syntax differ from the function parameter?
So this is the work-around I want to avoid:
#include <stdio.h>
static int testArr[2][3] = {{0,1,2},{10,11,12}};
typedef struct {
int *arr2d;
} Foobar_t;
int main( int argc, char** argv ) {
Foobar_t foobar = {(int*)testArr};
int (*arr2d)[3] = (int (*)[3]) foobar.arr2d;
printf("testStruct_0_0: %d\n", arr2d[0][0]);
printf("testStruct_1_0: %d\n", arr2d[1][0]);
return 1337;
}
Edit:
Some comments suggest that reference is not the correct word. Of course in the C language this is implemented by a pointer.
So the TLDR of this questions is: How does the syntax for a pointer type to a multi dimensional array look like.
The answer can already be seen in my work-around code. So that is all, move along, nothing to see here ;) Nevertheless thanks for the replies.
There is no "call by reference" in the C language. Function arguments are always passed by value. Arrays do appear special, since an array decays to a pointer to its first element in most expressions (including function calls). This means that when an array is used as an argument in a function call, a pointer to the first element is passed to the function instead of an array; but it is the value of this pointer which is passed.
In function declarators, array types are adjusted to pointers to appropriate types. This is specific to the semantics of function declarators. Thus, a function declaration like:
void foobar(int arg[][3]);
is adjusted to take a pointer to an array of three ints as an argument:
void foobar(int (*arg)[3]);
In general, a type expression such as int arg[][3] is an incomplete type, since it is impossible to know the size of the array arg[][] without more information.
Structures in C do not allow member types to be specified with incomplete types (with one exception), since there is no way to know the size of the struct without this information. Further, struct specifiers do not make the same adjustment to array types that function declarators do, since structs may actually include array members.
The exception to the incomplete type rule in structs is with flexible array members. The last member of a struct with at least two named members may have an incomplete array type.
The simple solution to the problem in the question is to change the specifier for the struct to use a pointer to an array. Note that here the member .arr2d is not an array, but a pointer to an array of three ints:
typedef struct {
int (*arr2d)[3];
} Foobar_t;
You could try using either int **arr2d, which would allow you to access it via arr2d[x][y] or simply convert it to a one-dimensional array like int *arr2d = malloc(2*3*sizeof(int));.
That way, you'd need to access values like this: arr2d[x*m + y];, where x and y are the same of the previous example, while m is the size of a row.
I'd also suggest you to store both row number and column number of your 2-dimensional array into the struct.
Turns out just looking at the work-around showed me the solution:
typedef struct {
int (*arr2d)[3];
} Foobar_t;
This is the correct type for a pointer to a 2D array. type (*name)[n] also works for function parameters.
So why is the other syntax type name[][n] still valid for function parameters? Probably the conflicts with the flexible array feature of structs keep it from working there.

Implicit declaration of function is invalid in C99

I am new to C language and I am having a problem that I really don't understand. I am trying to get an array from another function but when I try to extract the information, it gives me the following warning:
Implicit declaration of function 'getk_vector_calculation' is invalid in C99
Array initializer must be an initializer list or string literal
Here is the code:
int k_vector_calculation(int krec[3])
{
...
krec [0] = l*u[0]+m*v[0]+o*[0] ;
krec [1] = l*u[1]+m*v[1]+o*[1] ;
krec [2] = l*u[2]+m*v[2]+o*[2] ;
return k_vector_calculation( &krec[3] )
}
int main ()
{
char krec[3] = getk_vector_calculation(&krec[3]);
...
}
in your main() the function you called is getk_vector_calculation() [which is not k_vector_calculation()] and which is not declared or defined before the usage.
To resolve this,
either #include the header file containg the declaration of getk_vector_calculation() in your sorce file. [Considering getk_vector_calculation() is in some other file]
or, add a forward declaration of getk_vector_calculation() before main() and define getk_vector_calculation() somewhere.
To know more about implicit declaration, you can check this question.
EDIT:
As others have pointed out, there are many more problems in your code snippet. But since the question title is limited to implicit declaration error, IMO, this answer should do the needful. The remaining error(s) is(are) altogether a different aspect.
In older versions of C, functions that had not been declared yet were still able to be called, and it was assumed that they returned int and took an unspecified number of arguments. Strictly speaking, in C99, it is not permitted to call a function without declaring it first.
In your case however, you are trying to call a function called getk_vector_calculation but you have defined a function called k_vector_calculation (no get at the beginning).
You are also trying to initialise an array using a function, which is not permitted (in C, functions cannot return arrays). Simply declare the array and call k_vector_calculation as a separate statement, e.g.:
int krec[3] = {0};
k_vector_calculation(krec);
Don't use &krec[3] as this points to an invalid location. Use &krec[0] to provide the address of the first element in the array, or equivalently just krec will do. N.b. also that you declare an array of type char, but your function accepts a pointer to int, and these types are not compatible. Your function also calls itself unconditionally so there is a guaranteed infinite recursion if the snipped out code does not conditionally return. If your function doesn't need to call itself, and it doesn't return a value of any importance, change the return type to void to indicate it has no return value.
Since you are using C99, you can take advantage of using the static keyword in your function's parameter declaration:
void k_vector_calculation(int krec[static 3])
{
// ... other code here ...
krec[0] = l*u[0]+m*v[0]+o*[0];
krec[1] = l*u[1]+m*v[1]+o*[1];
krec[2] = l*u[2]+m*v[2]+o*[2];
}
The above code declares a function that takes as an argument an array of at least 3 int.
Several issues, here:
As Sourav Ghosh pointed out, you define k_vector_calculation(), but then try to call getk_vector_calculation(). You have to use the right names.
You say you want to "get an array from another function" - you just can't do this in C.
You don't show all your code for k_vector_calculation(), but as shown, this function will call itself forever, because the last thing it does is to unconditionally call itself again. If you have a recursive function, you need to give it a way to terminate.
&krec[3] is the address of the fourth element of the array k, which is not want you want to be doing, here, especially since your array only contains 3 elements. To refer to the array itself, just use krec.
char krec[3] = getk_vector_calculation(&krec[3]); is invalid for numerous reasons. One, you can't initialize arrays in this way in C. Two, see point (4) for your argument. Three, even if you could initialize arrays this way in C, you'd be trying to pass an uninitialized array to a function, initialize it in there, and then try to initialize your original array with the result. It just makes no sense.
You also write your functions to work with an array of int, but declare krec in main() as an array of char.
It's not clear what you want k_vector_calculation() to do, but you just can't return arrays in C like that. Probably what you want to do is just pass the array to the function, have the function work on in, and return nothing. For instance:
#include <stdio.h>
void k_vector_calculation(int kvec[])
{
kvec[0] = 1;
kvec[1] = 2;
kvec[2] = 3;
}
int main(void)
{
int kvec[3];
k_vector_calculation(kvec);
for ( int i = 0; i < 3; ++i ) {
printf("kvec[%d] is %d.\n", i, kvec[i]);
}
return 0;
}

Function that returns a multidimensional array

I've a function like this (in a file file_name.c):
char function_name(multi_array[][10])
{
/*change some character of multi_array*/
return multi_array;
}
That takes multi_array, a multidimensional array of characters, changes some characters of the given parameter, and than returns multi_array modified.
In main.c, i call the function like this:
multi_array_in_main = function_name(multi_array_in_main);
But the compiler gives me an error "icompatible type char[10][10] from type char"
What should i do? I'm not very confident with C so i don't know..!
You don't need to return anything.
Change:
char function_name(multi_array[][10])
To:
void function_name(multi_array[][10])
And your code should work fine (function_name will update whatever array it receives as an argument, as long as the dimensions are correct).
Change the function to return void and remove the return statement. The array is actually passed as a pointer to it's first element, so any changes you make to it inside your function actually change the original object in the caller.
void function_name(multi_array[][10])
{
/*change some character of multi_array*/
}
In your function header you declare function to return "char" type, but you return variable of char [][10], which is different type from the one in declaration (first line of your code).
Solution depends on what you really want to do. If you want to return that multiarray, change your function declaration. Also you defined parameter to be array of arrays, but it must be "array of array of char". Long story short, your declaration line should probably look like this:
char[][] function_name(char multi_array[][10])
Also, the changes made in multi_array made by this function will change multi_array even "outside" of the function and therefore you dont really need to return it. So you probably want to write this:
void function_name(char multi_array[][10])
As said, you do not need to return anything. The array is not copied, it is passed to your function as a pointer to the first element of the array. So, any element you change inside the function will be changed also outside because there is only one unique array.
Also if you insist, theoretically, it is possible to define a function returning a pointer to an array which is the closest thing to your original post. The declaration would be:
char (*function_name(char multi_array[][10]))[10] {
...
return(multi_array);
}
It is so ugly, that you will probably prefer to define a new type for it:
typedef char (*multi_array_t)[10];
multi_array_t function_name(multi_array_t multi_array) {
...
}

What's the proper way to define an array of arrays of function pointers?

Let's say I have an array of arrays of function pointers. In other words, I might want to call a matrix transpose function like so, depending upon what dtype my matrix is:
Transp[dtype][index_dtype](A.ia, A.a, B.ia, B.a);
Functions in Transp might look like this:
void transp_i64_i8(const int64_t* ia, const int8_t* a, int64_t* ib, int8_t* b) {
// transpose here
return;
}
except varying the pointer types.
It seems to me that I should declare my function pointer array like so:
void (**Transp)(const void* ia, const void* a, const void* ib, const void* b)[DTYPES_MAX][INDEX_TYPES_MAX] = {
{transp_i8_i8, transp_i8_i16, transp_i8_i32, /* ... */ },
{transp_i16_i8, transp_i16_i16, /* ... */ },
{transp_i32_i8, transp_i32_i16, /* ... */ },
/* ... */
}
Unfortunately this doesn't seem to work:
error: called object ‘Transp[(int)self_m->storage->dtype][(int)((struct YALE_STORAGE *)self_m->storage)->index_dtype]’ is not a function
../../../../ext/nmatrix/nmatrix.c: In function ‘nm_complex_conjugate_bang’:
../../../../ext/nmatrix/nmatrix.c:1910:32: error: subscripted value is neither array nor pointer nor vector
I found one fairly useful reference, but I really need an example for my exact use-case to understand and apply.
So what, exactly, is the correct way to define an array of arrays of function pointers? Specifically, how is the declaration portion written?
(I realize this can be done with a typedef much more easily, but I'm writing a code generator, and would rather not use a typedef.)
You declare it in a similar way to how you would use it, e.g.:
void (*Transp[DTYPES_MAX][INDEX_TYPES_MAX])(const int64_t*,
const int64_t*,
const int64_t*,
const int64_t*);
So the type of a function pointer is
ReturnType (*)(Args);
The type of an array of function pointers is
ReturnType (*[n])(Args);
So an array of arrays of function pointers would be
ReturnType (*[n][m])(Args);
Comparing this to what you have, it looks like you're declaring your array as
ReturnType (**)(Args)[n][m];
That is, a pointer to a pointer to a function that returns an array of arrays. If you remove one of the stars from your variable declaration and move the arrays inside the parentheses, I think this will resolve the problem.
Hope this helps!
If your function pointers all have a fixed number of parameters but different number of parameters or different parameter types, you cannot anyway portably use a single type of function pointers to represent all the function pointers.
In POSIX system you can use an array of array of void * as POSIX guarantees the representation of function pointers and void * is the same. In C actually there is no conversion between function pointers and void * but compilers usually support it.

How does one declare an array of constant function pointers in C?

I need to declare an array of pointers to functions like so:
extern void function1(void);
extern void function2(void);
...
void (*MESSAGE_HANDLERS[])(void) = {
function1,
function2,
...
};
However, I want the the array to be declared as constant -- both the data in the array and the pointer to the data. Unfortunately, I do not recall where to place the const key-word(s).
I'm assuming the actual pointer, MESSAGE_HANDLERS in this case, is already constant because it is declared as an array. On the otherhand, couldn't the function pointers within the array be change at runtime if it is declared as shown?
There is a technique to remember how to build such type. First try to read pointers starting from their name and read from right to left.
How to declare that stuff without help?
Arrays
T t[5];
is an array of 5 T. To make T a function type, you write the return-type to the left, and the parameters to the right:
void t[5](void);
would be an array of 5 functions returning void and taking no parameters. But functions itself can't be stuffed in arrays! They are not objects. Only pointers to them can.
What about
void * t[5](void);
That's still wrong as it would just change the return-type to be a pointer to void. You have to use parentheses:
void (*t[5])(void);
and this will actually work. t is an array of 5 pointers to functions returning void and taking no parameters.
Great! What about an array of pointers to arras? That's very similar. The element type appears at the left, and the dimension at the right. Again, parentheses are needed because otherwise the array would become a multidimensional array of integer pointers:
int (*t[5])[3];
That's it! An array of 5 pointers to arrays of 3 int.
What about functions?
What we have just learned is true about functions too. Let's declare a function taking an int that returns a pointer to another function taking no parameter and returning void:
void (*f(int))(void);
we need parentheses again for he same reason as above. We could now call it, and call the returned function pointed to again.
f(10)();
Returning a pointer to function returning another pointer to function
What about this?
f(10)(true)(3.4);
? In other words, how would a function taking int returning a pointer to a function taking bool returning a pointer to a function taking double and returning void would look like? The answer is that you just nest them:
void (*(*f(int))(bool))(double);
You could do so endless times. Indeed, you can also return a pointer to an array just like you can a pointer to a function:
int (*(*f(int))(bool))[3];
This is a function taking int returning a pointer to a function taking bool returning a pointer to an array of 3 int
What does it have to do with const?
Now that the above explained how to build up complexer types from fundamental types, you can put const at places where you now know where they belong to. Just consider:
T c * c * c ... * c name;
The T is the basic type that we end up pointing to at the end. The c stands for either const or not const. For example
int const * const * name;
will declare name to have the type pointer to a constant pointer to a constant int. You can change name, but you cannot change *name, which would be of type
int const * const
and neither **name, which would be of type
int const
Let's apply this to a function pointer of above:
void (* const t[5])(void);
This would actually declare the array to contain constant pointers. So after creating (and initializing) the array, the pointers are const, because the const appeared after the star. Note that we cannot put a const before the star in this case, since there are no pointers to constant functions. Functions simply can't be const as that would not make sense. So the following is not valid:
void (const * t[5])(void);
Conclusion
The C++ and C way of declaring functions and arrays actually is actually a bit confusing. You have to get your head around it first, but if you understand it, you can write very compact function declarations using it.
In situations like this, do a typedef to name your function signature, that makes it far simpler:
typedef void MESSAGE_HANDLER(void);
with that in place, it should be just:
MESSAGE_HANDLER * const handlers[] = { function1, function2 };
To get the actual content of the array constant.
EDIT: Removed pointer part from the typedef, this really is better (live and learn).
cdecl says:
cdecl> explain void (* const foo[])(void)
declare foo as array of const pointer to function (void) returning void
Is it what you need?
With VisualStudio 2008, I get:
void (* const MESSAGE_HANDLERS[])(void) = {
NULL,
NULL
};
int main ()
{
/* Gives error
'=' : left operand must be l-value
*/
MESSAGE_HANDLERS = NULL;
/* Gives error
l-value specifies const object
*/
MESSAGE_HANDLERS[0] = NULL;
}
I am not sure if this will work in 'C'. it does work in 'C++':
First define MESSAGE_HANDLERS as a type:
typedef void (*MESSAGE_HANDLER)();
Then, use the type definition to declare your array a constant:
MESSAGE_HANDLER const handlers[] = {function1, function2};
The trick is in the typedef, if you can do the same semantically in 'C', it should work too.

Resources