static const struct member [duplicate] - c

This question already has answers here:
Are members of a structure allowed to be static ?
(5 answers)
Closed 4 years ago.
I am setting up a structure for Modbus RTU communication, but i got problems writing the apropriate structure. Here is the structure i got up to now:
#include "stdint.h"
typedef struct TModbusFrameRtu_tag
{
static const uint32_t Start : 28;
uint8_t Address;
uint8_t Function;
uint8_t Data;
uint16_t Crc16;
static const uint32_t End : 28;
}TModbusFrameRtu;
const uint32_t TModbusFrameRtu_tag::Start = 0x0000;
const uint32_t TModbusFrameRtu_tag::End = 0x0000;
But the compiler doesnt like the static const combination. Trying to compile this code the compiler prints
error: expected specifier-qualifier-list before 'static'|
error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token|
error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token|
error: expected identifier before numeric constant|
error: unknown type name 'TModbusFrameRtu'|
I thought i could save some space by declaring the Start and End member static, as they are always going to be the same and will never change for any message that is going to be send. Is this not possible in that combination or how can i solve this?

No, that's not possible.
It doesn't make sense, the members of a struct need to be together in memory, but you somehow want to "pull out" a couple of fields and have them live elsewhere.
Would you expect e.g.
TModbusFrameRtu frame_a, frame_b; // two instances
to have the same value for &frame_a.Start and &frame_b.Start? That's not how things work, at all.
The solution is probably to not include these in the structure at all, but instead make them ordinary constants inside the implementation.

Related

Motive : This code aims to provide default values to structure members inside structure defination [duplicate]

This question already has answers here:
How to initialize a struct in accordance with C programming language standards
(16 answers)
Closed 2 years ago.
Here's my code:
#include<stdio.h>
struct parent_structure
{
char pe_1 = 'a';
};
struct child_structure
{
int ce_1 = 1;
int ce_2 = 2;
int ce_3 = 3;
struct sample_structure
{
int ss_1 = 1 ;
char ss_2 = 'a';
} ce_4 ;
struct parent_structure ce_5;
};
int main()
{
struct child_structure c_1;
printf("size of element : %d",sizeof(c_1));
return 0;
}
ERROR:
test.c:5:15: error: expected ':', ',', ';', '}' or '_attribute_' before '=' token
char pe_1 = 'a';
^
test.c:10:14: error: expected ':', ',', ';', '}' or '_attribute_' before '=' token
int ce_1 = 1;
^
PROBLEMS:
* If i initalize value at the time of defining structure
it gives the error shown above.
I was trying this question for a while and if I initialize values to members of structure like at the time of declaration of structure it shows an error.
Please tell me what is wrong with this?
You cannot give default values to struct members in C. Structures are data types.
Just like the way you declare variables int a; you declare struct variables like struct child_structure c_1;.
Don't confuse struct with struct struct_name, struct itself is not a data type, therefore struct_name is not a variable, and therefore you cannot initialize it as you do.

How do I assign values to an array within a typdef structure in C?

I am trying to assign values to an array within a typedef struct and continually am getting an syntax error.
Error expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
Here is my code:
myfile.h
#define Digit12 0x00u
#define Digit34 0x01u
#define Digit56 0x01u
typedef struct
{
uint8_t trData[3];
} CImageVersion;
myfile.c
CImageVersion oImageVersion; // declare an instance
oImageVersion.trData = { Digit12, Digit34, Digit56};
Later on in the code
otherfile.c
extern CImageVersion oImageVersion;
An arry is a pointer but if i change the assignment to
oImageVersion->trData = { Digit12, Digit34, Digit56};
I get the same error. I am very confused as to what I am doing wrong The error is pointing to directly after the oImageVersion when I assign the values
You can't assign directly to an array. The syntax you're using is only valid when a variable is defined. I.e. you can do this:
CImageVersion oImageVersion = { { Digit12, Digit34, Digit56} };
But not this:
CImageVersion oImageVersion;
oImageVersion.trData = { Digit12, Digit34, Digit56};
If you don't assign the values when the variable is defined, you need to assign to each array element individually:
oImageVersion.trData[0] = Digit12;
oImageVersion.trData[1] = Digit34;
oImageVersion.trData[2] = Digit56;

Assigning a value to structure element out side main [duplicate]

This question already has answers here:
Structure member assignment causing syntax error when not inside a function
(5 answers)
Closed 5 years ago.
I created a structure with two elements and tried to assign a value to one of the structure elements outside main function. But I'm getting error while compiling.
#include <stdio.h>
#include <stdlib.h>
struct node{
char a;
int b;
};
struct node sr;
sr.b = 48;
int main(){
printf("Value:%d",sr.b);
return 0;
}
I'm assigning value after the declaration. Why is this code giving error.
error message
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
You cannot have a statement which needs runtime execution outside main(), i.e., in file scope. It needs to be present inside some block scope, inside a function so as to determine when to be executed.
You can however, use initialization to have the initial values stored for the members of the structure type variable. Something like
struct node sr = {'Z', 1};
will initialize sr.a to 'Z' and sr.b to 1. In case you're only interested in initializing member b, you'll be needing designated initializers, like
struct node sr = { .b = 1 };
Put the following lines inside main() function.
struct node sr;
sr.b = 48;
Why? You cannot have a statement which needs runtime execution outside main(). It needs to be in the scope of a block, for example, inside a function so as to determine when to be executed.

C error: expected '=', ',', ';',

Im working on a PIC24f microcontroller, writing a C code for communication protocol.Im facing an error in function Reade2PW. I thought uint16_t gives the problem so i changed the
"uint16_t" to "int16" & "int16_t" and compiled but the problem is still remains the same i.e it gives the same error. My MPLAB using XC16
compiler. I read XC16 user guide, it can support uint16_t and int16_t.
Here is the screenclip from the XC16 user guide.
How to solve this problem? Your valuable suggestions will be highly appreciated.
Thanks
#include<stdio.h>
#include <stdint.h>
int main(void)
{
..
..
return 0;
}
unsigned uint16_t Reade2PW(unsigned uint16_t rde2pw) //Error here
{
unsigned uint16_t EEPVal;
unsigned char i, *Ptr;
....
}
void SaveE2PW(unsigned uint16_t rde2pw, unsigned uint16_t Cx)//Error here
{
unsigned char i, *Ptr;
..
}
Error Description:
error: expected '=', ',', ';', 'asm' or '__attribute__' before 'Reade2PW'
error: expected ';', ',' or ')' before 'rde2pw'
You have duplicate type specifiers in both your functions:
unsigned uint16_t Reade2PW(unsigned uint16_t rde2pw) //Error here
^^duplicate types here ^^ and here
Remove one. The uint16_t is already unsigned (so the u prefix). So just remove the unsigned from them:
uint16_t Reade2PW(uint16_t rde2pw)
{
uint16_t EEPVal;
// ....
}

How to give a structure with arrays in simulink to a custom C code function?

I'm trying to give a structure to a C function in Simulink. My steps so far:
Included the .h & .c in configuration parameters custom code. My header has a structure defind:
typedef struct mystruct
{ int m;
int *i;
double *x;}mystruct
Now in my MATLAB function under Simulink:
function y = fcn(u)%#codegen
m=int32(1);
i=zeros(10,1,'int32');
x=zeros(10,1);
s.m=m;
s.i=i;
s.x=x;
coder.cstructname(s,'mystruct','extern');
D=int32(0);
D=coder.ceval('accesmystruct',coder.ref(s));
y=10;
If I run the code I get a long error from the code generation that indicates that it can't be compiled in c-code. The error is:
c2_Test2.c
c2_Test2.c(57) : error C2143: syntax error : missing ')' before '*'
c2_Test2.c(57) : error C2081: 'cs_size' : name in formal parameter list illegal
c2_Test2.c(57) : error C2143: syntax error : missing '{' before '*'
c2_Test2.c(57) : error C2059: syntax error : ')'
c2_Test2.c(60) : error C2143: syntax error : missing ')' before '*'
....
This only happens if I declare the two variables i and x as Pointers. If I declare them as scalar in the header and MATLAB function it works.
Does any one see what I'm doing wrong?
Getting the Code to Compile
To get the code to compile I added:
#include "mystruct.h"
in the Simulation Target->Custom Code->Header File section. Adding the required include paths on that pane may be necessary as well.
Compatibility Concerns
After doing the above, the code crashes when running. The issue is that the definition of mystruct is not what MATLAB Coder is expecting.
When you define a MATLAB structure with fixed-size arrays inside of it, the type generated by MATLAB Coder uses static arrays inside of the C struct like:
typedef struct {
int32_T m;
int32_T i[10];
real_T x[10];
} mystruct;
You can see this in the code in the slprj directory if you remove the 'extern' from the coder.cstructname call.
A struct with inline arrays already has memory allocated for the arrays by the C compiler. However, when the fields are pointers, someone needs to allocate the space for the data, which has not been done here.
I see a few options:
Omit the 'extern' and allow MATLAB Coder/Simulink to generate the type definition
Declare your external structure with arrays rather than pointers for i and x
Before writing to or reading from the struct, pass it to another C function that allocates memory for the fields:
function y = fcn(u)
%#codegen
m=int32(1);
i=zeros(10,1,'int32');
x=zeros(10,1);
s = coder.nullcopy(struct('m',m,'i',i,'x',x));
coder.cstructname(s,'mystruct');
coder.ceval('initmystruct',coder.wref(s),int32(numel(i)),int32(numel(x)));
s.m=m;
s.i=i;
s.x=x;
And in C:
/* Example of initmystruct with no error checking */
/* int is the size type assuming it matches int32 */
/* in the MATLAB coder.ceval call */
void initmystruct (mystruct *s, int szi, int szx)
{
s->i = malloc(szi*sizeof(*s->i));
s->x = malloc(szx*sizeof(*s->x));
}
According to the doc for cstructname the header file needs to be specified using the 'Headerfile' input argument.

Resources