Please have a look at this program
#include<stdio.h>
#include<string.h>
typedef struct hs_ims_msrp_authority
{
int host_type;
char buf[50];
int port;
}hs_i;
int main()
{
char dom[50];
int i = 10, j = 20;
strcpy(dom, "ine");
fun((hs_i){i, dom, j}); // doesnt work
fun((hs_i){i, "dom", j}); // this works
}
int fun(hs_i c)
{
printf("%d %s %d\n", c.host_type, c.buf, c.port);
}
In call to fun function in main; how come function call work when string literal ("dom") is passed where as when array variable (dom) is passed it doesnt work?
To make variable work should it be typecasted in a specific way? or is there any other way?
How to pass structure as an argument to the functions? Passing of structure to the function can be done in two ways: By passing all the elements to the function individually. By passing the entire structure to the function. In this article, entire structure is passed to the function.
In this program, the whole structure is passed to another function by value. It means the whole structure is passed to another function with all members and their values. So, this structure can be accessed from called function.
The disadvantage is that a copy of an entire structure is created again by wasting the memory. The following program shows how to pass an entire structure as an argument to function. Consider another example, wherein, a C program to demonstrate the passing of an entire structure as an argument to function is explained.
Passing of structure to the function can be done in two ways: By passing all the elements to the function individually. By passing the entire structure to the function. In this article, entire structure is passed to the function. This can be done using call by reference as well as call by value method. How to return a structure from the functions?
The presence of the compound literal is distracting and the cause of the error is the attempt to initialize a char[]
with another char[]
array. The following is illegal:
char dom[50] = "test";
char dom1[50] = dom; /* Line 16, the cause of the error. */
and clang reports the following error:
main.c:16:10: error: array initializer must be an initializer list or string literal
Point 14 in section 6.7.8 Initialization of the C99 standard states:
An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.
So the call with the string literal "dom"
is permitted as it is legal to initialize an array with a string literal, but the call with the char[]
is not permitted.
Possible solutions:
buf
to be a const char*
wrap the buf
member in a struct
which would enable it to be copied. For example:
struct char_array
{
char data[50];
};
typedef struct hs_ims_msrp_authority
{
int host_type;
struct char_array buf;
int port;
} hs_i;
struct char_array dom = { "ine" };
int i = 10, j = 20;
fun((hs_i){i, dom, j});
fun((hs_i){i, { "dom" }, j});
/* Note ^ ^ */
In this case,
fun((hs_i){i, dom, j});
you are just passing the pointer to the string. In other words, you are just passing the
&"ine"[0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With