#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.
Related
I am getting error during initialization of nested structure containing two members
unsigned int
typedef void (*vfptr)(void);.
The first structure or parent structure contains simply two variables as shown:
typedef struct Stype1 {
unsigned int uiVar;
vfptr vfptr1;
}Stype1;
The second structure contains above structure as array of struct:
typedef struct Stype2 {
Stype1 Stype1inst[4];
}Stype2;
Whenever I try to compile using gcc 12.2.1 I get error:
error: expected expression before ‘{’ token
30 | Stype2inst.Stype1inst[4] = {{1,(vfptr)Vfun1},
I also tried initializing the structures using designated initializes as mentioned here:
https://stackoverflow.com/a/330867/2805824
How to initialize a struct in accordance with C programming language standards
typedef struct Stype2 {
Stype1 Stype1inst[] = {
{.uiVar = 1 , .vfptr1 = Vfun1 }
};
}Stype2;
But I get error:
error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
24 | Stype1 Stype1inst[] = {
|
Also tried compiling with -std=gnu99 and -std=c99 without any luck.
The entire code is pasted below:
#include <stdio.h>
typedef void (*vfptr)(void);
void Vfun1(){};
void Vfun2(){};
void Vfun3(){};
void Vfun4(){};
typedef struct{
unsigned int uiVar;
vfptr vfptr1;
}Stype1;
typedef struct{
Stype1 Stype1inst[4];
}Stype2;
Stype2 Stype2inst;
int main()
{
Stype2inst.Stype1inst[4] = {{1,(vfptr)Vfun1},
{2,(vfptr)Vfun2},
{3,(vfptr)Vfun3},
{4,(vfptr)Vfun4}};
return 0;
}
Structures and arrays can only be initialised at the point of their declaration. In your case, Stype2inst is declared as a structure, so the declaration-with-initialization will be of the form:
Stype2 Stype2inst = { ... };
Here, the ... will be the initial value(s) of the structure's data. That data is an array (with four elements), so we can now 'expand' the form to the following:
Stype2 Stype2inst = { { e0, e1, e2, e3 } };
Each of the elements (e0 thru e3) will itself be a structure (of type Stype1, and will take the form that you have correctly used: {1,(vfptr)Vfun1} (although the cast here is unnecessary).
Putting all this together and adding line-breaks, spaces and indentation for easy reading, we get the following declaration/initialisation:
Stype2 Stype2inst = { // Outer braces are for the "Stype2" structure
{ // Next are for the enclosed array of "Stype1"
{ 1, Vfun1 }, // Each of these is for one element/structure;
{ 2, Vfun2 },
{ 3, Vfun3 }, // Note that you don't need the casts to vfptr
{ 4, Vfun4 }
}
};
Note: Although the much shorter form of initialisation, which you have 'tried' in the comments to the question (and shown below), is valid, it is unclear and potentially ambiguous:
Stype2 Stype2inst = { 1,(vfptr)Vfun1, 2,(vfptr)Vfun2, 3,(vfptr)Vfun3, 4,(vfptr)Vfun4 };
Using this syntax, the clang-cl compiler generates the following diagnostic (four times):
warning : suggest braces around initialization of subobject
[-Wmissing-braces]
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.
It's an extremely simple and useless piece of practice code I'm working with in what is starting to seem like and extremely useless book. I was doing a struct exercise, and upon code compilation I received a handful of errors.
Here's the offending code:
struct fish = {
const char *name;
const char *species;
int teeth;
int age;
};
void catalog(struct fish f)
{
printf("%s is a %s with %i teeth. He is %i.\n", f.name, f.species, f.teeth, f.age);
}
int main()
{
struct fish snappy = {"Snappy", "piranha", 69, 4};
catalog(snappy);
return 0;
}
This is the exact code from the book, minus the struct definition above catalog. I ended up just copy pasting because I started to suspect this book was just dead wrong. The book claimed that the above code should compile and run without the struct even being defined. I've tried putting the struct definition into a header file, and I've tried removing it or adding it to different parts of the code. I get the same exact errors:
snappy.c:8:13: error: expected identifier or ‘(’ before ‘=’ token
struct fish = {
^
snappy.c:16:26: error: parameter 1 (‘f’) has incomplete type
void catalog(struct fish f)
^
snappy.c: In function ‘main’:
snappy.c:24:12: error: variable ‘snappy’ has initializer but incomplete type
struct fish snappy = {"Snappy", "piranha", 69, 4};
^
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:17: error: storage size of ‘snappy’ isn’t known
struct fish snappy = {"Snappy", "piranha", 69, 4};
struct fish = { is wrong in struct declaration. It should be struct fish {. Remove = sign.
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
Okay I know right off the bat this is going to be a stupid question, but I am not seeing why this simple C program is not compiling.
#include <stdio.h>
#include <stdlib.h>
typdef struct CELL *LIST;
struct CELL {
int element;
LIST next;
};
main() {
struct CELL *value;
printf("Hello, World!");
}
I am new to C programming, not to programming in general, but to C. I am familiar with Objective-C, Java, Matlab, and a few others, but for some reason I can not figure this one out. I am trying to compile it using GCC in OS X if that makes a difference. Thanks for your help!
The error message I am getting is
functionTest.c:5: error: expected specifier-qualifier-list before ‘LIST’
functionTest.c:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘struct’
Most importantly: You have misspelled typedef.
Then, at least these days, we normally add a return type to main, like so:
int main()
Also, main is supposed to return the exit status, so:
#include <stdio.h>
#include <stdlib.h>
typedef struct CELL *LIST;
struct CELL {
int element;
LIST next;
};
int main() {
struct CELL *value;
printf("Hello, World!\n");
return 0;
}
The main reason is that you typoed typedef as typdef. However, there are a couple other things you should do:
Add return 0; to the end of main().
Change the signature of main to int main(void)
Did you try to compile it with gcc -Wall -g yourprog.c -o yourbinary ?
I'm getting:
yourprog.c:3:8: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'struct'
yourprog.c:6:5: error: unknown type name 'LIST'
yourprog.c:8:1: warning: return type defaults to 'int' [-Wreturn-type]
yourprog.c: In function 'main':
yourprog.c:9:18: warning: unused variable 'value' [-Wunused-variable]
yourprog.c:11:1: warning: control reaches end of non-void function [-Wreturn-type]
and you mispelled typedef and you should change the signature of main and add a return 0; inside.
By the way, I find your typedef very poor taste. I suggest to code (like Gtk does) something like typedef struct CELL CELL_t and declare CELL_t* value = NULL. because you really want to remember that value is a pointer to CELL_t. In particular, I hate typedef-s like typedef struct CELL* CELL_ptr; because I find very imporrtant (for readability reasons) to quickly understand what is a pointer and what is not a pointer.
Actually I would rather suggest
struct cell_st;
typedef struct cell_st cell_t;
cell_t *value = NULL;
(I do like initializing all pointers to NULL).
Add a return in your main function