Bharath
Bharath

Reputation: 169

Initializing structure values in another structure

I am trying to initialize values to a structure using another structure through this code:

struct freq
{
    char temp[20];
    int count=0;
};
struct test
{
    char input[100];
    struct freq words[20];
    int len;
}testdb[1] = 
{
    { "ram is playing.he likes playing", { { "ram", 1 }, { "is", 1 }, {"playing", 2 }, { "he", 1 }, { "like", 1 } }, 5 } 
};

but I get an error that cannot convert from initializer list to freq.
What is the solution to this?

Upvotes: 2

Views: 46

Answers (1)

Vlad
Vlad

Reputation: 9481

You have an error in the first struct. I assume you are writing in C. C doesn't have default values for struct members.

This compiles perfectly:

struct freq
{
    char temp[20];
    int count;
};
struct test
{
    char input[100];
    struct freq words[20];
    int len;
}testdb[1] = 
{
    { "ram is playing.he likes playing", { { "ram", 1 }, { "is", 1 }, {"playing", 2 }, { "he", 1 }, { "like", 1 } }, 5 } 
};

Upvotes: 1

Related Questions