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.
Related
As part of a function that receives a struct ifreq *ifr argument, if I declare struct sockaddr_in name;, the program compiles, but if I name the variable struct sockaddr_in ifr_addr;, it fails with the following error:
code.c:244:24: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
struct sockaddr_in ifr_addr;
^
code.c:244:24: error: expected expression before ‘.’ token
Makefile:2: recipe for target 'all' failed
make: *** [all] Error 1
The ifreq struct is declared as below. I know that the struct has a field with the same name than the problematic variable. How is that a problem in C?
struct ifreq {
char ifr_name[IFNAMSIZ]; /* Interface name */
union {
struct sockaddr ifr_addr;
struct sockaddr ifr_dstaddr;
struct sockaddr ifr_broadaddr;
struct sockaddr ifr_netmask;
struct sockaddr ifr_hwaddr;
short ifr_flags;
int ifr_ifindex;
int ifr_metric;
int ifr_mtu;
struct ifmap ifr_map;
char ifr_slave[IFNAMSIZ];
char ifr_newname[IFNAMSIZ];
char *ifr_data;
};
};
http://man7.org/linux/man-pages/man7/netdevice.7.html
When you get an error like that, it almost invariably means that a header, in this case probably one of the system headers, has defined a macro with the same name as you chose for your variable, but with an expansion that is not valid as an identifier.
You wouldn't see a problem if the header defines:
#define ifr_addr pwr_address
You do see a problem if, as you note in a comment, the expansion (in include/uapi/linux/if.h, near line 258) is:
#define ifr_addr ifr_ifru.ifru_addr
The macro is meant to make it easier to access elements of a union without having to specify the union member name each time. At times like this, you find yourself asking — was it worth it? (I see this in the code base I work on daily — a lot. It's hard, sometimes, to work out what the code is accessing.)
Although it would be possible to use:
#undef ifr_addr
before defining your own variable with that name, doing so is treading on thin ice. It's best to accept that the name is preempted and use something else, annoying though it is. A possibility is to use ifr_srcaddr, to match/contrast ifr_dstaddr.
.h file with header for creating a list in c:
#ifndef SO605_GC
#define SO605_GC
#include <stddef.h>
#define MEMSIZE 4096*1024*1024
typedef struct free_node {
size_t size;
struct free_node *next;
} free_node_t;
typedef *free_node_t mem_free_t;
void *aloca(size_t size);
void libera(void *ptr);
#endif
When I compile the error occurs:
aloca.h:14:10: error: expected identifier or ‘(’ before ‘free_node_t’
typedef *free_node_t mem_free_t;
How to solve this?
You have a syntax error in
typedef *free_node_t mem_free_t;
which should be
typedef free_node_t *mem_free_t;
but please do not typedef pointers. Also I believe that the suffix _t is typically reserved.
Note too that 4096*1024*1024 will not fit a 32-bit variable, whatever it is for, and probably will not multiply as you think it will.
#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
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