I'm getting a strange compiler error initializing a struct.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
struct RadarData
{
unsigned int messageID : 32;
unsigned int time : 32;
float az;
float el;
};
struct RadarData sendData;
sendData.az = 25;
sendData.el = 10;
sendData.messageID = 1;
sendData.time = 100;
This looks fine to me according to a few different tutorials, but on two different machines, I'm getting the following error when compiling:
testserver.c:15:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
testserver.c:16:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
testserver.c:17:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
testserver.c:18:9: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘.’ token
Why am I getting this error?
sendData.az = 25;
Statements like this must be inside a function. If you want to initialize the struct, there's a different syntax for that:
struct RadarData sendData = { 25, 10, 1, 100 };
If I'm looking at your code right (and that's the complete relevant code), then you're placing statements outside of a function. That's not right.
Related
I'm studying about structures, and having some error. I cannot figure out why. Please help me out.
#include <stdio.h>
struct data{
int x;
float y;
float z;
} info;
info.x = 100;
struct data *ptr;
ptr = &info;
(*ptr).y = 5.5;
(*ptr).z = 1.0;
int main(void)
{
printf("The first member's value is %d.\n", info.x);
printf("The second member's value is %.1f.\n", info.y);
printf("The third member's value is %.1f.\n", info.z);
return 0;
}
The compiler error is
exercise112.c:10:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
info.x = 100;
^
exercise112.c:13:1: warning: data definition has no type or storage class
ptr = &info;
^~~
exercise112.c:13:1: warning: type defaults to ‘int’ in declaration of ‘ptr’ [-Wimplicit-int]
exercise112.c:13:1: error: conflicting types for ‘ptr’
exercise112.c:12:14: note: previous declaration of ‘ptr’ was here
struct data *ptr;
^~~
exercise112.c:13:7: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
ptr = &info;
^
exercise112.c:13:7: error: initializer element is not computable at load time
exercise112.c:15:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
(*ptr).y = 5.5;
^
exercise112.c:16:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
(*ptr).z = 1.0;
^
Did I do definition, declaration, initialization or anything wrong?
I cannot figure out what the error messages are saying. what do they
mean?
This question already has answers here:
How to initialize a struct in accordance with C programming language standards
(16 answers)
Closed 4 years ago.
typedef struct {
char fielda[ 2 ][ FIELD_A_MAX + 1 ];
bool fieldb = false;
bool fieldc = false;
sem_t fieldd;
} Set;
I get the error:
error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
bool fieldb = false;
What's my mistake here?
You cannot initialize fields inside a type definition. All initialization has to happen at the time of declaring a variable of the defined type:
typedef struct {
char fielda[ 2 ][ FIELD_A_MAX + 1 ];
bool fieldb;
bool fieldc;
sem_t fieldd;
} Set;
...
Set s = {.fieldb = false, .fieldc = false};
Unfortunately, the initializing sequence needs to be repeated each time. To avoid this, you could make a function to initialize Set:
void init_Set(Set* s) {
s->fieldb = false;
s->fieldc = false;
...
}
Now the initialization code is in a single place. You need to invoke this code for each Set structure that you allocate.
In C is not possible to initialize members in structures.
I'm getting the error when compiling with gcc -Wall -std=c99:
pokerhand.c:20:17: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
Card *cards = malloc(sizeof(Card)*5);
here is my code where the error is happening
typedef struct card
{
char suit;
char *face;
} Card;
typedef struct hand
{
Card *cards = malloc(sizeof(Card)*5);
char *result;
} Hand;
all I have before these structs is header includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
You can't write code inside a struct declaration. That is wrong.
I bet this would solve the error
typedef struct hand
{
Card *cards;
char *result;
} Hand;
And later you can allocate to it when you have proper variable declared with that type.
Also this would work
typedef struct hand
{
Card cards[5];
char *result;
} Hand;
If you think that each hand would contain 5 card every single time then yes you can add it like this.
In the first case you need to allocate the cards and then free it when you are done working with it.
You can't "do things" with the struct members when you define the struct.
So Card *cards = malloc(sizeof(Card)*5); makes no sense, and the compiler issues a diagnostic.
One solution is to build an init_card function, that takes a struct card* as an input parameter; and you perform your initialisation there. If you also build a corresponding free_card function you'll end up with something that scales up remarkably well.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
typedef struct {
int tos;
char stackarr[];
}STACK;
STACK paren;
paren.tos = -1;
void push()
{
paren.tos++;
paren.stackarr[tos] = '(';
}
This is giving me the following error:
error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
paren.tos = -1;
^
In function ‘push’:
error: ‘tos’ undeclared (first use in this function)
I'm a beginner and have no idea why I'm getting this error. Any ideas?
You cannot perform an assignment outside a function; only initialization is allowed (demo):
STACK paren = {.tos = -1};
With this part out of the way, your approach is not going to work: flexible members, i.e. char stackarr[] at the end of the struct, do not work in statically allocated space; you need to use dynamic allocation with them. See this Q&A for an illustration of how to use flexible struct members.
Alternatively, you can pre-allocate the max number of elements to stackarr, i.e.
typedef struct {
int tos;
char stackarr[MAX_STACK];
} STACK;
STACK paren = {.tos = -1};
The obvious limitation to this approach is that the stack cannot grow past its preallocation limit.
I am getting strange compilation errors from trying to create a struct in C.
Here is my code:
#define ALIGNMENT 8
/* rounds up to the nearest multiple of ALIGNMENT */
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)
#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
#define BLK_HDR_SIZE ALIGN(sizeof(blockHdr))
typdef struct header {
size_t size;
blockHdr *next_p;
blockHdr *prior_p;
} blockHdr;
This is the error message:
mm.c:49:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘struct’
typdef struct header {
^
make: *** [mm.o] Error 1
I am baffled by this error. Is there something wrong with my code, or is there a more serious issue?
You have a typo in typdef. Next, you'll get an error about blockHdr not being defined.
The correct definition is:
typedef struct header {
size_t size;
struct header *next_p;
struct header *prior_p;
} blockHdr;
You can't use the typedef before it is declared. You have to use the actual structure name.
I think that that instead of
typdef struct header
it should be
typedef struct header