Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trouble in passing structure literal as function parameter

Tags:

c

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?

like image 641
rik Avatar asked Jan 22 '13 10:01

rik


People also ask

How to pass structure as an argument to the function?

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.

What does it mean to pass a structure by value?

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.

What is the disadvantage of passing an entire structure to 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.

How to pass a structure to a function in JavaScript?

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?


2 Answers

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:

  • change the type of 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 ^       ^ */
    
like image 122
hmjd Avatar answered Oct 14 '22 07:10

hmjd


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]

like image 44
sr01853 Avatar answered Oct 14 '22 06:10

sr01853