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?
The presence of the compound literal is distracting and the cause of the error is the attempt to initialize a
char[]with anotherchar[]array. The following is illegal:and clang reports the following error:
Point 14 in section 6.7.8 Initialization of the C99 standard states:
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 thechar[]is not permitted.Possible solutions:
bufto be aconst char*wrap the
bufmember in astructwhich would enable it to be copied. For example: