Initializing a 2D array inside a struct error - c

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}}};

Related

Why doesn't struct exist after declaration inside of function?

I've tried a lot of simple examples of this and haven't gotten any to work.
My goal is to have a function which declares a struct internally, sets the values of the struct, and then returns the struct.
struct getData(void){
typedef struct{
int count1;
int count2;
} MyStruct;
MyStruct myData;
myData.count1 = 5;
myData.count2 = 6;
return myData;
};
int main(void) {
struct myData = getData()
printf("count1: %i", myData.count1);
printf("count2: %i", myData.count2);
}
Every example I've found does something similar to this, but for some reason it's not finding my struct called MyStruct. Exact error is:
error: expected identifier or ‘(’ before ‘void’
struct getData(void){
^~~~
The error I keep getting makes me think it doesn't like the struct inside the function.
Your problem seems to be a confusion regarding the usage of the struct keyword. You don't do struct myData to declare a variable named myData that is of struct type, because there isn't really a struct type. What you do is struct myData <SOMETHING> to define <SOMETHING> as being a new data type named struct myData. You can then say struct myData dat;, thereby declaring that dat is a variable of type struct myData.
You're also demonstrating the same confusion at the top, where you have struct getData(void)... you're attempting to declare getData as a function returning a struct, but you'd really have to do something like struct myData getData(void) to declare a function returning type struct myData.
I think the more standard way to write what you're after is this, using an independent definition of the struct, and then using a pointer for the function call. As written your code seems to have some scope issues (declaring a struct typedef inside a function, hmmm...) [Incorrect->] plus I don't believe structs can be passed around that way in C, I'm pretty sure you have to use pointers. [Edit oops this is incorrect! See comments]
#include<stdio.h>
typedef struct {
int count1;
int count2;
} MyStruct;
void getData (MyStruct* myDataPtr) {
myDataPtr->count1 = 5; //note pointer access notation ->
myDataPtr->count2 = 6;
};
int main(void) {
MyStruct myData;
MyStruct* sPtr = &myData; //create pointer to struct, assign to myDaya
getData(sPtr); //pass pointer to function
printf("count1: %i \n", myData.count1);
printf("count2: %i \n", myData.count2);
}
Outputs:
count1: 5
count2: 6

error: expected specifier-qualifier-list before when creating a struct array

I'm learning C language and I have a problem.
I have a struct and I initialize it with {0}:
struct first
{
char a[5];
int b;
}first= {0};
And I'm trying to create a array of this struct in another struct with each element in the array has the value of {0}:
struct second
{
first f_array[20];
};
However, when I compile, it says error: expected specifier-qualifier-list before 'first'.
Anyone knows how to create a struct array in other struct like what I'm trying to do?
This happens because the compiler can't resolve the type of first unless you include the keyword struct in the declaration of the array like this :
struct first f_array[20];
Or use typedef :
typedef struct {
char a[5];
int b;
} first;
struct second {
first f_array[20];
};
struct second obj = {{0}};
Using typedef means you no longer have to write struct all over the place and makes your code more cleaner.

Initialization of an array in a structure

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}};

Structs in C confusion

I have a c file that starts with a struct I am calling stringtable, looks like this
struct stringtable {
int table[];
int numElements = 15;
};
And I have a header for it that has this typedef
typedef stringtable *stringtable_ref;
When I compile with gcc I get the errors:
expected identifier or '(' before '[' token
expected ':' before 'int'
like I have declared the struct wrong. I have done structs in C like this before so my question is: Am I making a mistake declaring my struct? Does it need to have a tag before the semicolon? Are there only certain places I am allowed to declare a struct?
struct stringtable {
int table[];
int numElements = 15;
};
A flexible array member like int table[]; can only be the last member of a struct (with at least one more member).
And you can't assign a default value to a member in a struct declaration, C doesn't support that.
Unless you have a static member in your struct, you cannot initialize the members upon declaration.
You need to create an instance of your struct before initalizing the members:
struct stringtable str_table;
str_table.numElements = 15;
//etc
I would also expect that you need in the header typedef struct stringtable *stringtable_ref

Initialize structs issues on C

I'm getting this error during compilation:
"c:\command_line.h(17): error C2143: syntax error : missing ';' before '*'
Note: C++ does not support default-int
command_line.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int"
This my code:
command_line.h
typedef struct symbol
{
char* sym_type;
unsigned short address;
}symbol;
typedef struct symbol_map
{
char** p_arr_keys;
symbol* p_arr_values;
int item_count;
int array_mode;
int copy_keys;
}symbol_map;
typedef struct params
{
int data_counter;
int code_counter;
int line_counter;
int command_len;
int error_counter;
int warning_counter;
symbol_map* p_symbol_map; // (This is line 17- from the error msg)
char* p_last_symbol
}params;
main.c
params config;
config.code_counter = 0;
config.data_counter = 0;
config.line_counter = 0;
config.command_len = 0;
config.command_first_char = EMPTY;
config.error_counter = 0;
config.warning_counter = 0;
config.p_last_symbol = NULL;
config.p_symbol_map = {NULL}; // (This is line 17- from the error msg)
Any idea what's wrong with this initialization?
config.p_symbol_map = {NULL};
A construct you have used config.p_symbol_map = {NULL} is a static initialization and is only allowed in a variable declaration. If you want to assign a NULL to p_symbol_map you can simply config.p_symbol_map = NULL.
A valid case for static initialization applied to a symbol structure would be something like this:
symbol sym = {
NULL,
0x42
};
updated:
Btw, you are missing a semicolon after char* p_last_symbol in a structure definition.
typedef struct params
{
int data_counter;
int
int line_counter;
int command_len;
int error_counter;
int warning_counter;
symbol_map* p_symbol_map; // (This is line 17- from the error msg)
char* p_last_symbol <----- need to add ; here
}params;
You are not initializing a structure, you are initializing a pointer to a structure.
struct foo {
int a;
int b;
};
struct foo bar = {.a = 0, .b = 1};
But if you are declaring a pointer to it, then you don't have a memory block to initialize at first. So you can't use {} idiom to initialize a pointer to struct;
struct foo *bar;
bar = NULL
Something is wrong there, you are assigning to p_last_symbol member which you didn't declare in params. Also, p_symbol_map is a pointer to something, you can either initialize it with NULL or make it point to something else (such as memory allocated with malloc).
There is no need for {} around NULL. Just using NULL or nullptr would do. Further, what is p_symbol_map? There is no such thing in your declaration of params.
Also, in line 17, it should be wrtten as:
struct symbol_map* p_symbol_map. This is the reason there is an error on line 17.
Hope this helped.
You cant do it like that. You have to initialize the config AFAIK. You can do this since you are assigning a pointer and not a struct.
config.p_symbol_map = NULL;

Resources