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.
Related
I have my struct like below , there can be n number of vendor which can contain n number of test struct.
I am trying to initialize this structure . This is a sample code I am trying , later I want to make it using macros and load the structure like X-macros.
I am also using flexible structure concept as I do not know how many test structs for a vendor are going to be. The data would be in a file , the struct needs to load all that is there. I have created a minimal sample code for SO.
Below is my code.
#include <stdio.h>
typedef struct test{
int a;
int b;
int c;
}test;
typedef struct vendor{
int size;
test t[0];
}vendor;
vendor v[]={
{.size = 1, .t[] = {{1,2,3},}}
};
int main()
{
return 0;
}
I get this error -
a.c:16: error: expected expression before ‘]’ token
a.c:16: error: array index in initializer not of integer type
a.c:16: error: (near initialization for ‘v[0].t’)
a.c:16: error: extra brace group at end of initializer
a.c:16: error: (near initialization for ‘v[0]’)
a.c:16: error: extra brace group at end of initializer
a.c:16: error: (near initialization for ‘v[0]’)
a.c:16: warning: excess elements in struct initializer
a.c:16: warning: (near initialization for ‘v[0]’)
I have tried without flexible struct , no luck so far.
any suggestions on how to init this struct ?
The .t[]= syntax in the initializer is invalid. When using a designated initializer, you only need to specify the name of the member:
.t={1, 2, 3}
However, this still won't work with a flexible array member.
The size of a struct with a flexible array member doesn't include space for the flexible array member, so you can't created a static or automatic instance of it. You need to allocate memory for the struct dynamically:
vendor *v;
void init()
{
v = malloc(sizeof(vendor) + 1 * sizeof(test));
v.size = 1;
v.t = (test){1, 2, 3};
}
int main()
{
init();
return 0;
}
Also, because of the variable size, a struct with a flexible array member cannon be a member of an array.
So I'm following this lesson from the Learn C the Hard Way tutorials and I'm a little stuck.
When I try to compile I get this:
cc -Wall -g ex19.c object.o -o ex19
ex19.c:81:2: warning: initialization from incompatible pointer type [enabled by default]
ex19.c:81:2: warning: (near initialization for 'RoomProto.move') [enabled by default]
ex19.c: In function 'Map_move':
ex19.c:91:7: error: void value not ignored as it ought to be
ex19.c: At top level:
ex19.c:140:2: warning: initialization from incompatible pointer type [enabled by default]
ex19.c:140:2: warning: (near initialization for 'MapProto.move') [enabled by default]
make: *** [ex19] Error 1
From what I understand, this error occurs b/c the function returns next when it expects a void return type. But I thought void *foo() meant returning a void pointer.
Here's the code:
Object RoomProto = {
.move = Room_move,
.attack = Room_attack
};
void *Map_move(void *self, Direction direction) {
Map *map = self;
Room *location = map->location;
Room *next = location->_(move)(location, direction);
if(next)
map->location = next;
return next;
}
Room Struct:
struct Room {
Object proto;
Monster *bad_guy;
struct Room *north;
struct Room *south;
struct Room *east;
struct Room *west;
};
How can I fix this error? I tried to change the return type, but ended up getting another error instead.
As far as I can tell, I copied the code in the tutorial char for char, so unless I made a mistake, the tutorial is incorrect.
#MattMcNabb helped me find the answer
Line 13 in object.h file was:
void *(move) (void *self, Direction direction);
instead of:
void *(*move) (void *self, Direction direction);
I am attempting to write a simple linked list just for practice and to jog my memory a little bit, but I have ran into trouble. I am pretty inexperienced in C, and I don't understand why this isn't working. I am trying to define a Node and a LinkedList struct, but every time I try to compile I get an error saying Node is an unknown type. I'm sure I am missing something, but I can't figure this out. Thanks all!
This is my ll.h file
1 #ifndef ll_h
2 #define ll_h
3
4 #include <stdio.h>
5
6 typedef struct {
7 void *data;
8 Node *next;
9 Node *prev;
10 } Node;
11
12
13 typedef struct {
14 Node *first;
15 Node *last;
16 int size;
17 } LinkedList;
18
19
20 void *getData(LinkedList list, int index);
21 int getSize(LinkedList list);
22 void *deleteNode(LinkedList, int index);
23 void add(LinkedList list, void *data);
24 void freeList(LinkedList list);
25
26 #endif
Errors I am receiveing
cc -Wall -g -c -o ll.o ll.c
In file included from ll.c:3:0:
ll.h:8:5: error: unknown type name ‘Node’
ll.h:9:5: error: unknown type name ‘Node’
ll.c: In function ‘getData’:
ll.c:8:18: error: expected expression before ‘LinkedList’
ll.c:12:7: warning: assignment from incompatible pointer type [enabled by default]
ll.c: In function ‘getSize’:
ll.c:21:12: error: expected expression before ‘LinkedList’
ll.c: In function ‘deleteNode’:
ll.c:26:18: error: expected expression before ‘LinkedList’
ll.c:32:7: warning: assignment from incompatible pointer type [enabled by default]
ll.c:37:12: error: request for member ‘next’ in something not a structure or union
ll.c:38:12: warning: assignment from incompatible pointer type [enabled by default]
ll.c:40:12: error: request for member ‘next’ in something not a structure or union
ll.c: In function ‘add’:
ll.c:52:16: warning: assignment from incompatible pointer type [enabled by default]
ll.c:54:21: warning: assignment from incompatible pointer type [enabled by default]
ll.c: In function ‘freeList’:
ll.c:61:18: error: expected expression before ‘LinkedList’
ll.c:62:18: warning: initialization from incompatible pointer type [enabled by default]
ll.c:65:7: warning: assignment from incompatible pointer type [enabled by default]
ll.c:67:7: warning: assignment from incompatible pointer type [enabled by default]
ll.c:62:11: warning: variable ‘next’ set but not used [-Wunused-but-set-variable]
ll.c:60:9: warning: unused variable ‘i’ [-Wunused-variable]
ll.c: In function ‘main’:
ll.c:85:6: error: ‘LinkedList’ has no member named ‘add’
ll.c:91:10: warning: dereferencing ‘void *’ pointer [enabled by default]
ll.c:91:10: error: void value not ignored as it ought to be
ll.c: In function ‘getSize’:
ll.c:22:1: warning: control reaches end of non-void function [-Wreturn-type]
make: *** [ll.o] Error 1
Use this:
typedef struct Node{
void *data;
struct Node *next; // note here
struct Node *prev;
} Node;
You either need forward declaration, or you need to drop the typedef. You can also use typedef with having the Node twice.
Forward declaration
typedef struct Node Node; // Necessary in C, harmless (but non-idiomatic) in C++
typedef struct {
void *data;
Node *next;
Node *prev;
} Node;
No typedef (Will only work in C++, not C)
struct Node {
void *data;
Node *next;
Node *prev;
};
Using typedef with tag
Works in C and C++, but non-idiomatic for C++.
typedef struct Node {
void *data;
struct Node *next;
struct Node *prev;
} Node;
Posting an Answer for the next reader brought here by Google searching:error: void value not ignored as it ought to be
Remember when trying to implement pseudo-code that function pointers get initialized with the name of the function without any parenthesis. Rookie mistake? Maybe, but I'm not exactly rookie (taught C/C++ Labs at university in mid 90s) and it got me. Had I typed it and not cut-n-pasted, I probably would have caught the error, as I know not to create that code...
Example with declarations supplied for completeness:
// Static menu manager
#define MAX_SELECTIONS 4
#define MAX_PROMPT_LEN 20
typedef struct _selection {
char prompt[MAX_PROMPT_LEN];
void(*function) ( void );
int fn_arg;
} SELECTION ;
typedef struct _menu {
int id;
int num_selections;
SELECTION selection[MAX_SELECTIONS];
} MENU;
enum _menu_ids { MAIN_MENU, DEPOSIT_MENU, WITHDRAWL_MENU, BALANCE_MENU };
enum _menu_keys { DEPOSIT, WITHDRAWL, BALANCE, CHECKING, SAVINGS };
Wrong:
// WRONG
static MENU menu[] = {
{ MAIN_MENU, 4,
{"Perform A Deposit", goto_menu(), DEPOSIT_MENU},
{"Perform a Withdrawl", goto_menu(), WITHDRAWL_MENU },
{"Get Balance", goto_menu(), BALANCE_MENU },
{"Hide Menu", menu_hide(), 0}
}
}
Correct:
// Correct
static MENU menu[] = {
{ MAIN_MENU, 4, {
{"Perform A Deposit", goto_menu, DEPOSIT_MENU},
{"Perform a Withdrawl", goto_menu, WITHDRAWL_MENU },
{"Get Balance", goto_menu, BALANCE_MENU },
{"Hide Menu", menu_hide, 0} }
}
}
Particularly observant readers might notice the additional braces required to initialize the struct's array's members were missing in the pseudo-code. The gcc compiler was kind enough to provide a useful error:MENU: improper number of initializers.
#include <stdio.h>
typedef struct
{
int data;
struct node *next;
}node;
void print(node *head)
{
node *tmp = head;
while (tmp)
{
printf ("%d ", tmp->data);
tmp = tmp->next;
}
}
int main()
{
node arr[5] = {
{1, &arr[1]},
{2, &arr[2]},
{3, &arr[3]},
{4, &arr[4]},
{5, NULL}
};
print(arr);
return 0;
}
Why do i get these warnings while compiling with gcc -Wall ? (even without -Wall, gcc produces the same warnings)
list.c: In function ‘print’:
list.c:15:7: warning: assignment from incompatible pointer type [enabled by default]
list.c: In function ‘main’:
list.c:22:18: warning: initialization from incompatible pointer type [enabled by default]
list.c:22:18: warning: (near initialization for ‘arr[0].next’) [enabled by default]
list.c:23:18: warning: initialization from incompatible pointer type [enabled by default]
list.c:23:18: warning: (near initialization for ‘arr[1].next’) [enabled by default]
list.c:24:18: warning: initialization from incompatible pointer type [enabled by default]
list.c:24:18: warning: (near initialization for ‘arr[2].next’) [enabled by default]
list.c:25:18: warning: initialization from incompatible pointer type [enabled by default]
list.c:25:18: warning: (near initialization for ‘arr[3].next’) [enabled by default]
What #metalhead said was correct. Another probably better way to achieve the same result is
typedef struct _node
{
int data;
struct _node *next;
} node;
After this definition node (without underscore) can simply be used as a type name like e.g. int.
P.S. The underscore is just a standard convention, not a requirement. Any name could have been used in place of _node as long as you replace in both occurrences. However, in c this is a norm and sort of a coding convention that helps the devs to quickly understand _node refers to the node type actually.
You are trying to use struct node inside the definition of node so therefore the compiler doesn't know you mean them to be the same thing. Try forward declaring the struct first:
struct node;
struct node
{
int data;
struct node *next;
};
When i have run the following piece of code:
typedef char *lrfield();
struct lrfields {
char name[26];
lrfield *f;
};
struct lrfields lr_table[] = {
{"pri_tran_code1", pri_tran_code2},
{"sec_tran_code", sec_tran_code},
{"type_code", type_code},
{"sys_seq_nbr", sys_seq_nbr},
{"authorizer", authorizer},
{"void_code", void_code},
{"",0}
};
char *pri_tran_code2()
{
return pri_tran_code;
}
*
*
if(second)
{
for(bp=lr_table; bp->name[0]; bp++)
if(strcmp(bp->name, second)==0)
{
tmpval=bp->f();
break;
}
}
I have got these errors:
error: `pri_tran_code2' undeclared here (not in a function)
error: initializer element is not constant
error: (near initialization for `lr_table[0].f')
error: initializer element is not constant
error: (near initialization for `lr_table[0]')
error: initializer element is not constant
error: (near initialization for `lr_table[1]')
As you can see in the code that i have defined 'pri_tran_code2' above its call. Please help me to solve this error.
Add char *pri_tran_code2(); before you mention this name? Or simply move the whole implementation there. It doesn't matter where you call it, what matters is where you refer to it.
Your declaration is erroneous. To declare a function (function-pointer) type, try this instead:
typedef char *(*lrfield)();