This question already has answers here:
Why can't we initialize members inside a structure?
(6 answers)
Closed 7 years ago.
Can you have a structure in C that has elements of that same structure? My first attempt at implementing a binary search tree in C is the following:
#include <stdio.h>
struct binary_tree_node {
int value;
struct binary_tree_node *left = null;
struct binary_tree_node *right = null;
};
main() {
struct binary_tree_node t;
t.value = 12;
struct binary_tree_node y;
y.value = 44;
t.left = &y;
}
I can't figure out what's wrong with this code, any help would be appreciated. I realize there are other questions on binary search implementations in C, but I'm trying to figure this out from scratch with my own code (and some guidance of course). Thanks!
Remove the = null in your struct declaration. You can declare the self-reference, but you cannot set it.
This is the error message on gcc 4:
test.c:6: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
test.c: In function ‘main’:
test.c:18: error: ‘struct binary_tree_node’ has no member named ‘left’
Firstly, you null is NULL in C.
Secondly, you cannot set a value to an element in a struct inside the struct definition.
So, it would look something like this:
#include <stdio.h>
struct binary_tree_node {
int value;
struct binary_tree_node *left;
struct binary_tree_node *right;
};
main() {
struct binary_tree_node t;
t.left = NULL;
t.right = NULL;
t.value = 12;
struct binary_tree_node y;
y.left = NULL;
t.right = NULL;
y.value = 44;
t.left = &y;
}
Or, you can create a function to make left and right NULL,
#include <stdio.h>
struct binary_tree_node {
int value;
struct binary_tree_node *left;
struct binary_tree_node *right;
};
void make_null(struct binary_tree_node *x) {
x->left = NULL;
x->right = NULL;
}
main() {
struct binary_tree_node t;
make_null(&t)
t.value = 12;
struct binary_tree_node y;
make_null(&y);
y.value = 44;
t.left = &y;
}
You cannot define the values inside the struct when defining the struct. This code snippet may benefit your project:
typedef struct binary_tree_node
{
int value;
binary_tree left;
binary_tree right;
} binary_tree_node, *binary_tree;
#define DATA(T) ((T)->value)
#define LEFT(T) ((T)->left)
#define RIGHT(T) ((T)->right)
Related
I am new to C and I can't get this thing to compile properly, can you help me?
struct tagNode
{
int v, f;
struct tagNode *next;
struct tagNode( int _v )
{
v = _v;
next = NULL;
}
};
expected identifier or ‘(’ before ‘int’:
struct tagNode( int _v )
expected identifier or ‘(’ before ‘int’: struct tagNode( int _v )
this is because of the unexpected struct when you compile in C++
A valid version of your code in C++ can be:
struct tagNode
{
int v, f;
tagNode *next;
tagNode (int _v)
{
v = _v;
next = NULL;
}
};
but
what about f ?
the constructor can also be tagNode(int _v) : v(_v), next(NULL) {}
and you have a pointer so I recommend you to look at the rule of three
There is no constructor/methods in C so the equivalent of your code in C can be :
#include <stdlib.h>
struct tagNode
{
int v, f;
struct tagNode * next;
};
struct tagNode * alloc(int _v)
{
struct tagNode * r = malloc(sizeof(struct tagNode));
r->v = _v;
r->next = NULL;
return r;
}
In the marked line I get an error Error - expected expression
#include <stdlib.h>
struct list_head {
struct list_head *next, *prev;
};
struct program_struct {
const char *name;
struct list_head node;
};
typedef struct program_struct program_t;
struct task_t {
program_t blocked_list;
};
int main() {
struct task_t *p = malloc(sizeof(*p));
p->blocked_list.name = NULL;
p->blocked_list.node = {&(p->blocked_list.node), &(p->blocked_list.node)}; //error
return 0;
}
I know I can replace this line with
p->blocked_list.node.next = &(p->blocked_list.node);
p->blocked_list.node.prev = &(p->blocked_list.node);
But can I make it work using {} like I tried in the first piece of code?
Initialization is allowed only when you define a variable. So, you can't use initializers in assignment.
You can instead use C99's compound literals:
p->blocked_list.node = (struct list_head) {&(p->blocked_list.node), &(p->blocked_list.node)}; //error
I am new to C programming and as a mini project I decided to try to implement a stack in C using OOP style structure in a file GenericStack.h as shown below:
void _GENERICSTACK0001(void *,void *);
void *_GENERICSTACK0002(void *);
int _GENERICSTACK0003(void *);
typedef struct
{
struct GenericStackNode *next;
void *data;
int type;
}GenericStackNode;
typedef struct
{
struct GenericStackNode *top;
int count;
void (*add)(void *,void *);
void *(*pop)(void *);
int (*hasNext)(void *);
int (*getCount)(void *);
}GenericStack;
GenericStack newGenericStack()
{
GenericStack *genStack = malloc(sizeof(GenericStack));
genStack->add = _GENERICSTACK0001;
genStack->pop = _GENERICSTACK0002;
genStack->hasNext = _GENERICSTACK0003;
genStack->getCount = _GENERICSTACK0003;
genStack->top=NULL;
genStack->count = 0;
return *genStack;
}
void _GENERICSTACK0001(void *self,void *data)//add
{
GenericStack *genStack = self;
if(genStack->top == NULL)
{
genStack->top = malloc(sizeof(GenericStackNode));
genStack->top->next = NULL;
genStack->top->type = 0;
genStack->top->data = data;
}
else
{
GenericStackNode *temp = malloc(sizeof(GenericStackNode));
temp->next = genStack->top;
temp->type = 0;
temp->data = data;
genStack->top = temp;
genStack->count++;
}
}
void *_GENERICSTACK0002(void *self)//pop
{
GenericStack *genStack = self;
void *data = NULL;
if(genStack->top == NULL)
{
return data;
}
else
{
GenericStackNode *temp = genStack->top;
genStack->top = genStack->top->next;
data = temp->data;
free(temp);
genStack->count--;
return data;
}
}
int _GENERICSTACK0003(void *self)
{
GenericStack *genStack = self;
return genStack->count;
}
All I need to know is why (among many others) I get the specific error:
GenericStack.h:41:16: error: dereferencing pointer to incomplete type
genStack->top->type = 0;
I have checked the other answers on stackoverflow concerning "dereferencing pointer to incomplete type" but I cant seem to understand.
You're getting an error from GenericStack, but you have a problem in both GenericStack and GenericStackNode.
In C, struct X and X are different types. When you write:
struct GenericStackNode *next;
it declares a type called struct GenericStackNode (and a member which is a pointer to that type). This type is incomplete because you have not provided the struct definition.
The type could be completed by providing a struct definition later, but you never do that. Instead, you define an unnamed struct and typedef GenericStackNode to it , but that has no effect on struct GenericStackNode.
Then, struct GenericStackNode *top; still uses this same incomplete type, not the struct you defined above.
Assuming you meant for this pointer to be a pointer to the same type of struct it's contained in, you could use this pattern for both of your structs:
typedef struct X X;
struct X
{
X *ptr;
};
Often people combine the typedef with the struct definition but I find it clearer to have them separate.
You already type-defined GenericStackNode as a type, there is no need for struct GenericStackNode anymore, just GenericStackNode :
typedef struct
{
struct GenericStackNode *top;
...
}
should be only
typedef struct
{
GenericStackNode *top;
...
}
also , you can't use GenericStackNode when you still havn't defined it yet :
typedef struct
{
struct GenericStackNode *next;
void *data;
int type;
} GenericStackNode ;
you can write :
typedef struct GenericStackNode
{
struct GenericStackNode *next;
void *data;
int type;
} GenericStackNode ;
struct FailedTransaction{
OrderNodePtr order;
int failureID;
struct FailedTransaction* next;
struct FailedTransaction* tail;
};
typedef struct FailedTransaction* FailedTransactionPtr;
struct SuccessfulTransaction{
OrderNodePtr order;
struct SuccessfulTransaction* next;
struct SuccessfulTransaction* tail;
};
typedef struct SuccessfulTransaction* SuccessfulTransactionPtr;
struct FinalReport{
FailedTransactionPtr failedTransactions;
SuccessfulTransactionPtr successfulTransactions;
};
struct FinalReport* report = NULL;
This code is declared above main. When accessing
report->successfulTransactions
or
report->failedTransactions
I get undefclared identifier for FailedTransaction and SuccessfulTransaction.
Here is the code that manipulates report
if(report == NULL){
report = malloc(sizeof(struct FinalReport));
report->failedTransactions = NULL;
report->successfulTransactions = NULL;
}
if(outcome){
if(report->successfulTransactions == NULL){
report->successfulTransactions = malloc(sizeof(SuccessfulTransaction));
report->successfulTransactions->order = temp;
report->successfulTransactions->tail = report->successfulTransactions;
}else{
report->successfulTransactions->tail->next = malloc(sizeof(SuccessfulTransaction));
report->successfulTransactions->tail->next->order = temp;
report->successfulTransactions->tail = report->successfulTransactions->tail->next;
}
}else{
if(report->failedTransactions == NULL){
report->failedTransactions = malloc(sizeof(FailedTransaction));
report->failedTransactions->order = temp;
report->failedTransactions->tail = report->failedTransactions;
}else{
report->failedTransactions->tail->next = malloc(sizeof(FailedTransaction));
report->failedTransactions->tail->next->order = temp;
report->failedTransactions->tail = report->failedTransactions->tail->next;
}
report->failedTransactions->failureID = outcome;
}
The errors occur at the first lines after each if statements and else statements.
This is for an assignment and I have been stuck on this for an hour or so (it is due tomorrow night). Can't figure out why it is happening and I can't find anything online. Any help would be appreciated.
This is the header file that contains OrderNodePtr
#ifndef _CONSUMER_
#define _CONSUMER_
struct OrderNode{
char title[250];
int id;
double cost;
char category[250];
struct OrderNode* next;
struct OrderNode* tail;
};
typedef struct OrderNode* OrderNodePtr;
#endif
Try
sizeof(struct FailedTransaction);
Or, make FailedTransaction a typedef:
struct _FailedTransaction;
typedef struct _FailedTransaction FailedTransaction;
struct _FailedTransaction {
OrderNodePtr order;
int failureID;
FailedTransaction* next;
FailedTransaction* tail;
};
Why does C need "struct" keyword and not C++?
So im getting a "Line 51: error: dereferencing pointer to incomplete type" when running this:
int main(void)
{
Tree * testTree;
testADT * data1;
testTree = createTree(compare,destroy);
data1 = malloc(sizeof(testADT));
data1->val = 10;
/* Line 51 */ addToTree(testTree,testTree->root,data1);
destroyBinTree(testTree);
return(0);
}
my addToTreeFunction:
TreeNode * addToTree(Tree * theTree,TreeNode * theTreeNode, TreeDataTypePtr data)
{
TreeNode * newNode;
if(isEmpty(theTree))
{
newNode = malloc(sizeof(TreeNode));
newNode->height = 0;
newNode->data = data;
theTree->root = newNode;
return theTree->root;
}else{
if(theTree->compare(theTreeNode->data,data) == 1) /* shows root data is smaller */
{
theTreeNode->right = addToTree(theTree,theTreeNode->right,data);
}else
if(theTree->compare(theTreeNode->data,data) == 0) /* shows root data is larger */
{
theTreeNode->left = addToTree(theTree,theTreeNode->left,data);
}
}
return theTreeNode;
}
my typedefs and structs:
struct tADT{
int val;
};
typedef struct tADT testADT;
typedef void * TreeDataTypePtr;
Could anyone provide some insight into whats going on? Thanks-in advance!
Edit: This is in my module (.c)
struct AvlNode{
void * data;
struct AvlNode * left;
struct AvlNode * right;
int height;
};
struct AvlTree{
int (*compare) (TreeDataTypePtr data1, TreeDataTypePtr data2);
void (*destroy) (TreeDataTypePtr data);
struct AvlNode * root;
};
this is in the header (.h)
struct AvlTreeNode;
struct AvlTree;
typedef struct AvlTree Tree;
typedef struct AvlNode TreeNode;
typedef void * TreeDataTypePtr;
Problem fixed by defining all structs/typedefs in header:
typedef struct AvlTree Tree;
typedef struct AvlNode TreeNode;
typedef void * TreeDataTypePtr;
struct AvlNode{
void * data;
struct AvlNode * left;
struct AvlNode * right;
int height;
};
struct AvlTree{
int (*compare) (TreeDataTypePtr data1, TreeDataTypePtr data2);
void (*destroy) (TreeDataTypePtr data);
struct AvlNode * root;
};
Are you defining "Tree" somewhere in one of your header files? Can line 51 of the failing module see that header?
You must define "Tree" :)
===================== ADDENDUM ====================
Thank you for updating your post with your "definition" of Tree:
// .h file
struct AvlTreeNode;
struct AvlTree;
typedef struct AvlTree Tree;
...
But the fact remains - these are both "incomplete types".
You say you defined AvlTree and AvlTreeNode "in your module (.c)".
Q: Which .c translation unit?
Q: Is it defined in that translation unit before you try to use it?
Q: Is it used in any other translation unit?
Q: Why the duplicate/redundant/confusing typedef aliases?
Q: Why, for heaven's sake, don't you just define it in your .h file?
testTree->root dereferences testTree, which is of type Tree, which isn't defined anywhere.
The error suggests that you have a forward declaration of Tree, but not a full definition of its corresponding structure. That is why you can declare a pointer to Tree, but you are not allowed to dereference its members.
Make sure that the compilation unit that contains main has a #include at the top for the header file that contains the definition of struct Tree, this will fix this problem.