Initialize structs issues on C - 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;

Related

Move a struct through (void *) function argument back to struct

I am trying to send a struct through a function, taking (void *) as a singular argument, and then signing it to a struct of the same type (as in main/before function) inside the function. This results in error every single way I attempt to do it. Anyone knows how to do this?
struct a
{
SDL_Surface *s;
};
struct b
{
SDL_Window *window;
};
struct struct_of_structs
{
struct a *struct_a;
struct b *struct_b;
};
void loop(void *arg)
{
struct_of_structs s_structs;
// how do I assign arg to s_structs?
s_structs = arg; // error
s_structs = *arg; // error
s_structs = &arg; // error
*s_structs = arg; // error
&s_structs = arg; // error
*s_structs = *arg; // error
*s_structs = &arg; // error
&s_structs = *arg; // error
&s_structs = &arg; // error
}
int main()
{
struct struct_of_structs struct_struct;
struct a a_struct;
struct b b_struct;
struct_struct.a = a_struct;
struct_struct.b = b_struct;
loop(&struct_struct);
}
Edit I have uploaded my original code at pastebin. Whatever order of pointer or casting I add to the "arg" inside the loop, I get the error:
error: expected expression
arguments = (*(loop_args*)arg);
error: use of undeclared identifier 'loop_args'
in every way I add the casting. Doing struct loop_args arguments* = ((loop_args*)arg) does not work, etc, etc. It always fails with "expected expression blah blah".
P.S. adding your solution to a random main.c using the dummy code worked and I learned something new, thanks a lot!
s_structs = (*(struct_of_structs *)arg);
should do the trick.
basically what happens is you send a pointer to a struct but the function receives it as a void pointer, so to go from a void * to a struct you can't dereference a void *, so you cast the argument to a struct_of_structs pointer, and dereference it which will result in what you are looking for.
You need to create a pointer, not a new struct.
void loop(void *arg)
{
struct_of_structs *ps_structs =0;
// how do I assign arg to s_structs?
ps_structs = (struct_of_structs *) arg; // error
}
With the help of all the answers as well as the comments, I solved it!
struct_of_structs s_structs* = (struct struct_of_structs*)arg;
I cannot simply cast it to a struct of type "struct_of_structs", need to cast it to a type that is a struct aka "struct struct_of_structs"! Thanks everyone!

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

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

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

What does "request for member '*******' in something not a structure or union" mean?

Is there an easy explanation for what this error means?
request for member '*******' in something not a structure or union
I've encountered it several times in the time that I've been learning C, but I haven't got a clue as to what it means.
It also happens if you're trying to access an instance when you have a pointer, and vice versa:
struct foo
{
int x, y, z;
};
struct foo a, *b = &a;
b.x = 12; /* This will generate the error, should be b->x or (*b).x */
As pointed out in a comment, this can be made excruciating if someone goes and typedefs a pointer, i.e. includes the * in a typedef, like so:
typedef struct foo* Foo;
Because then you get code that looks like it's dealing with instances, when in fact it's dealing with pointers:
Foo a_foo = get_a_brand_new_foo();
a_foo->field = FANTASTIC_VALUE;
Note how the above looks as if it should be written a_foo.field, but that would fail since Foo is a pointer to struct. I strongly recommend against typedef:ed pointers in C. Pointers are important, don't hide your asterisks. Let them shine.
You are trying to access a member of a structure, but in something that is not a structure. For example:
struct {
int a;
int b;
} foo;
int fum;
fum.d = 5;
It may also happen in the following case:
eg. if we consider the push function of a stack:
typedef struct stack
{
int a[20];
int head;
}stack;
void push(stack **s)
{
int data;
printf("Enter data:");
scanf("%d",&(*s->a[++*s->head])); /* this is where the error is*/
}
main()
{
stack *s;
s=(stack *)calloc(1,sizeof(stack));
s->head=-1;
push(&s);
return 0;
}
The error is in the push function and in the commented line. The pointer s has to be included within the parentheses. The correct code:
scanf("%d",&( (*s)->a[++(*s)->head]));
I have enumerated possibly all cases where this error may occur in code and its comments below. Please add to it, if you come across more cases.
#include<stdio.h>
#include<malloc.h>
typedef struct AStruct TypedefedStruct;
struct AStruct
{
int member;
};
void main()
{
/* Case 1
============================================================================
Use (->) operator to access structure member with structure pointer, instead
of dot (.) operator.
*/
struct AStruct *aStructObjPtr = (struct AStruct *)malloc(sizeof(struct AStruct));
//aStructObjPtr.member = 1; //Error: request for member ‘member’ in something not
//a structure or union.
//It should be as below.
aStructObjPtr->member = 1;
printf("%d",aStructObjPtr->member); //1
/* Case 2
============================================================================
We can use dot (.) operator with struct variable to access its members, but
not with with struct pointer. But we have to ensure we dont forget to wrap
pointer variable inside brackets.
*/
//*aStructObjPtr.member = 2; //Error, should be as below.
(*aStructObjPtr).member = 2;
printf("%d",(*aStructObjPtr).member); //2
/* Case 3
=============================================================================
Use (->) operator to access structure member with typedefed structure pointer,
instead of dot (.) operator.
*/
TypedefedStruct *typedefStructObjPtr = (TypedefedStruct *)malloc(sizeof(TypedefedStruct));
//typedefStructObjPtr.member=3; //Error, should be as below.
typedefStructObjPtr->member=3;
printf("%d",typedefStructObjPtr->member); //3
/* Case 4
============================================================================
We can use dot (.) operator with struct variable to access its members, but
not with with struct pointer. But we have to ensure we dont forget to wrap
pointer variable inside brackets.
*/
//*typedefStructObjPtr.member = 4; //Error, should be as below.
(*typedefStructObjPtr).member=4;
printf("%d",(*typedefStructObjPtr).member); //4
/* Case 5
============================================================================
We have to be extra carefull when dealing with pointer to pointers to
ensure that we follow all above rules.
We need to be double carefull while putting brackets around pointers.
*/
//5.1. Access via struct_ptrptr and ->
struct AStruct **aStructObjPtrPtr = &aStructObjPtr;
//*aStructObjPtrPtr->member = 5; //Error, should be as below.
(*aStructObjPtrPtr)->member = 5;
printf("%d",(*aStructObjPtrPtr)->member); //5
//5.2. Access via struct_ptrptr and .
//**aStructObjPtrPtr.member = 6; //Error, should be as below.
(**aStructObjPtrPtr).member = 6;
printf("%d",(**aStructObjPtrPtr).member); //6
//5.3. Access via typedefed_strct_ptrptr and ->
TypedefedStruct **typedefStructObjPtrPtr = &typedefStructObjPtr;
//*typedefStructObjPtrPtr->member = 7; //Error, should be as below.
(*typedefStructObjPtrPtr)->member = 7;
printf("%d",(*typedefStructObjPtrPtr)->member); //7
//5.4. Access via typedefed_strct_ptrptr and .
//**typedefStructObjPtrPtr->member = 8; //Error, should be as below.
(**typedefStructObjPtrPtr).member = 8;
printf("%d",(**typedefStructObjPtrPtr).member); //8
//5.5. All cases 5.1 to 5.4 will fail if you include incorrect number of *
// Below are examples of such usage of incorrect number *, correspnding
// to int values assigned to them
//(aStructObjPtrPtr)->member = 5; //Error
//(*aStructObjPtrPtr).member = 6; //Error
//(typedefStructObjPtrPtr)->member = 7; //Error
//(*typedefStructObjPtrPtr).member = 8; //Error
}
The underlying ideas are straight:
Use . with structure variable. (Cases 2 and 4)
Use -> with pointer to structure. (Cases 1 and 3)
If you reach structure variable or pointer to structure variable by following pointer, then wrap the pointer inside bracket: (*ptr). and (*ptr)-> vs *ptr. and *ptr-> (All cases except case 1)
If you are reaching by following pointers, ensure you have correctly reached pointer to struct or struct whichever is desired. (Case 5, especially 5.5)
It may means that you forgot include a header file that define this struct/union.
For example:
foo.h file:
typedef union
{
struct
{
uint8_t FIFO_BYTES_AVAILABLE : 4;
uint8_t STATE : 3;
uint8_t CHIP_RDY : 1;
};
uint8_t status;
} RF_CHIP_STATUS_t;
RF_CHIP_STATUS_t getStatus();
main.c file:
.
.
.
if (getStatus().CHIP_RDY) /* This will generate the error, you must add the #include "foo.h" */
.
.
.
can also appear if:
struct foo { int x, int y, int z }foo;
foo.x=12
instead of
struct foo { int x; int y; int z; }foo;
foo.x=12
I saw this when I was trying to access the members.
My struct was this:
struct test {
int a;
int b;
};
struct test testvar;
Normally we access structure members as
testvar.a;
testvar.b;
I mistook testvar to be a pointer and did this.
testvar->a;
That's when I saw this error.
request for member ‘a’ in something not a structure or union
My ridiculous experience is that I incorrectly put '.' instead of ','.
printf("%c". ch);

Resources