Initialization of an array in a structure - c

I have declared 2 structure :
typedef struct
{
int a;
int b;
}ma_Struct;
typedef struct
{
int x;
ma_Struct tab[2];
}maStruct_2;
The goal is to initialise an instance of maStruct_2 so what i did is:
int main()
{
ma_Struct elm1={0,1};
ma_Struct elm2={1,2};
ma_Struct tab_Elm[2]={elm1,elm2};
maStruct_2 maStruct_2_Instance={1,tab_Elm};
return 0;
}
but i got the warning missing braces around initializer,i tried this syntax
maStruct_2 maStruct_2_Instance={1,{tab_Elm}};
but the same warning appears.
Could you please help me

In C, you cannot initialize an array by using another array name as initializer.
So the error has nothing to do with structs as such, nor does it have anything to do with scope or constant expressions.
Fix your code like this:
maStruct_2 maStruct_2_Instance = {1, {elm1, elm2}};

Related

How can I properly assign a value to an array (of structure type) element?

#include <stdio.h>
struct virus
{
char signature[25];
int size;
}v[2];
int main(void) {
static v[0] = {"Yankee",1813};
static v[1] = {"Doodle",2813};
int i;
for(i=0;i<=1;i++)
{
printf("%s %d\n",v[i].signature,v[i].size);
}
return 0;
}
I am getting the compiler error in this C code.
Error: Declaration syntax in function main()
I am guessing that there is some error in v[2], as it is associated with extern class whereas, v[0] and v[1] are associated with static class.
But, I am not sure that is this the only reason or some other ?
Edit : I have edited the code by removing the wrong syntax.
There is no error in declaration of v[2], the problem is later.
You've written
static struct v[0] = {"Yankee",1813};
which attempts to define a 0-sized array, which is not allowed by default C standard.
That said, the syntax is also horribly wrong. You don't have a proper type there, remember, struct itself is not a type, it's a keyword. struct <something> is actually a type.
Then, from the logical point of view, you probably don't want a new variable altogether. In case you want to use the array elements from the v, just use the variable name, that's all. Something like
#include <stdio.h>
struct virus
{
char signature[25];
int size;
}v[2] = { {"Yankee",1813}, {"Doodle",2813}}; //get it initialized, job done
int main(void) {
int i;
for(i=0;i<=1;i++)
{
printf("%s %d\n",v[i].signature,v[i].size);
}
return 0;
}
will do the job in much better way, IMHO.
EDIT:
In case, you're interested in assigning individual elements (not initialization), well, you cannot use a brace-enclosed initializer for that purpose, it's not meant to be RHS operand for an assignment. You need to use a compound literal for that purpose, something like
v[0] = (struct virus){"Yankee",1813};
v[1] = (struct virus){"Doodle",2813};
will also do the job.
Don't mix up struct definitions with variable declarations, that's sloppy practice.
Instead, use a typedef:
typedef struct
{
char signature[25];
int size;
} virus_t;
Then you can declare variables of this type as you please:
static virus_t v[2] =
{
{"Yankee",1813},
{"Doodle",2813}
};
Or with designated initializers:
static virus_t v[2] =
{
[0] = {"Yankee",1813},
[1] = {"Doodle",2813}
};

Confusing about the scope of the variables in C

Code 1
#include <stdio.h>
int T;
int main()
{
struct T{ double x;};
printf("%d", sizeof(T));
return 0;
}
Output: 4
Code 2
#include <stdio.h>
struct T{ double x;};
int main()
{
int T;
printf("%d", sizeof(T));
return 0;
}
Output: 4
For the code 1, I think that the output is the size of the global variable(int T) that's why it gives 4.
But for the code 2, it gives output 4, according to the global variable logic, it should be output 8 for struct T{ double x;};.
Here, the global variable concept is not correct - I think.
Can anyone please explain this why the output looks like?
The problem here isn't the scope but how structures tags live in a different namespace than other symbols.
Because of this when you do sizeof(T) you always get the variable T.
To get a structure tag in C you need the struct keyword. As in sizeof(struct T).
Things would be different if you used typedef:
struct T
{
double x;
} T;
Then you would have a type named T in the "normal" namespace. On the other hand that type would conflict with the variable with the same name so you would get a compiler error instead.

defining structure variable during declaration

I have been trying something in structures in C.
struct val{
unsigned int a : 1 ;
}store[100] ;
Now I want to initialize all the array members value to 1. That is, I want all the array members to have their variable a assigned to 1. I can use a loop for that but how can I do this during the declaration?
struct val
{
unsigned int a=1 : 1 ;
}store[100];
How can i achieve this? The above syntax is coming out to be wrong in code::blocks.
struct val
{
unsigned int a : 1;
}store[100];
I initially thought that what you were doing was a bitfield initialization. Further research though suggested I am wrong, and that you are trying to use gcc's designated initializers. What you want to do, is not possible that way. I found another way in your code that you can do it, though:
typedef struct {
unsigned int a;
} val;
then, where you want to initialize the array, you will do something like that:
val values[100] = {[0].a = 1};
This works by exploiting this behavior of gcc:
If the same field is initialized multiple times, it has the value from the last initialization. If any such overridden initialization has side-effect, it is unspecified whether the side-effect happens or not. Currently, GCC discards them and issues a warning.
My Test Program follows:
#include <stdio.h>
struct val {
unsigned int a;
};
int
main (void)
{
struct val value[100] = {[0].a = 1};
printf ("A random val's value is %d\n", value[40].a);
return 0;
}
Compiles and works cleanly on my GCC 4.9.1.
It's not possible.
You can use a loop, or, if by any chance you want to initialize all struct's data members to a specific value, you can also use memset:
struct val {
unsigned int a : 1 ;
} store[N];
memset(store, value, N * sizeof(struct val));
Before declaring a struct variable you can't initialize its member. Initialization can be done as
struct val{
unsigned int a : 1;
}store[100] = {[0].a = 1} ; // Designated initializer
but it will initialize a member of other elements of store to 0. To initialize member a of all elements of store you need a loop.

What is the difference between structure and function scope in C?

Consider this piece of code
int main(void)
{
typedef struct {
int i;
} s;
struct {
s s;
} t;
return 0;
}
It compiles fine. Now take a look at this one
int main(void)
{
typedef struct {
int i;
} s;
s s;
return 0;
}
This code will not compile -
ā€˜sā€™ redeclared as different kind of symbol.
Question: Why is it correct to have "s s;" as a declaration inside a structure, but not correct to have this definition inside a function?
In upper example member s is a local to struct. You cannot use it without using t.s syntax, so there is no conflict with structure type s.
In lower example structure type s, and variable s are in the same scope, so it is unclear which you are referring to.
As a struct member, the identifier s is unambiguous, because you'll always address it as somestruct.s or someptr->s.

Initializing a 2D array inside a struct error

When I initialize this array inside my struct. I get the error message -
syntax error : '{'.
Unexpected token(s) preceding '{'; skipping apparent function body.
int array[8][2] = {{3,6},{3,10},{3,14},{8,4}, {8,8},{8,12},{8,16},{12,2}};
I'm not sure what is wrong as I copied the syntax from my textbook.
Declaration is typedef struct _array *Array;
You cannot initialize a variable inside a struct declaration, doesn't matter if an array or int. Yet, you can initialize the array in the struct initialization.
struct foo {
int x;
int array[8][2];
};
struct foo foovar = {1, {{3,6},{3,10},{3,14},{8,4}, {8,8},{8,12},{8,16},{12,2}}};

Resources