1

I tried to assign a struct variable values from an array of the same type of structs using = operator, like below:

struct s {
    int dummy;
    char somechar[8];
} array_of_s[10];

void f(void)
{
    struct s a = array_of_s[0];  // this line gives me error message
}

int main(void)
{
    f();
    return 0;
}

I'm getting an error message and it says:

a value of type "struct s" cannot be used to initialize an entity of type "struct s"C/C++(144)

But if I hand over array as an argument like below it works like a charm:

struct s {
    int dummy;
    char somechar[8];
} array_of_s[10];

void f(struct s array_as_parameter[])
{
    struct s a = array_as_parameter[0];
}

int main(void)
{
    f(array_of_s);
    return 0;
}

Why is the first code illegal? Shouldn't I be able to access to external array variable and assign it?

1
  • 1
    Try using an assignment instead of initialization. Commented Jul 16, 2021 at 20:28

2 Answers 2

1

I don't get it.

gcc -Wall -Wextra -o main main.c compiles just fine.

#include <stdio.h>

struct s {
    int dummy;
    char somechar[8];
} array_of_s[10] = {{123, "abc"}};

void f(void)
{
    struct s a = array_of_s[0];
    printf("%d, %s\n", a.dummy, a.somechar);
}

int main(void)
{
    f();
    return 0;
}

The output:

123, abc

On the other hand, VSCode shows me that error message too.

It is a bug, have a look at this: https://github.com/microsoft/vscode-cpptools/issues/3212

Sign up to request clarification or add additional context in comments.

6 Comments

@Clifford I have to apologize, i missunderstood your comment. You were right from the begin. I have no clue, what i have read, my answer to your comment was not (that) fine.
No need. I assume by "I don't get it" you mean you don't get the error, rather then meaning "I don't understand it" which is how I read it first.
I had a look at the code and could not see anything wrong. I've tested it on my system and it ran as expected. 'I don't get it' means: I see nothing wrong here, it must be something different. And that difference is that what you have mentioned in your first comment to my answer (but already deleted it).
As you said, it looks like it's a bug in VS, pure and simple.
The OP should note that the link you posted has a solution at github.com/microsoft/vscode-cpptools, but it may be easier to ignore the message and trust the compiler.
|
0

The clue is in the error message - you are not assigning you are initialising.

Change to :

struct s a ;
a = array_of_s[0]; 

That said I have never seen an error message like that (with C/C++(144) on the end - they are two different languages - that makes little sense). Moreover it compiles fine in GCC - what compiler (and version) are you using? Is this in fact not a compiler diagnostic but a pre-parse performed by your IDE/Editor?

3 Comments

I have to apologize, i missunderstood your comment. You were right from the begin. I have no clue, what i have read, my answer to your comment was not (that) fine.
Looks like that error message "C/C++" is coming from a Microsoft product, and unlike you and me, Microsoft does tend to believe that "C/C++" is one language. And per the discussion linked to from @ErdalKüçük's answer, it looks like this is a pre-parse being performed by an IDE, and a buggy one at that.
@SteveSummit Like Clifford mentioned in a comment to my answer above, there exists a workaround for that specific error message.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.