I'm new to C and trying to compile this simple code, but it's not working and I'm not sure why. Can anyone help me?
int main(int argc, const char * argv[])
{
struct Node{
int value;
struct Node *next;
};
struct Node* x;
struct Node* y;
struct Node* z;
x = malloc(sizeof(Node));
y = malloc(sizeof(Node));
z = malloc(sizeof(Node));
return 0;
}
The compiler is complaining about the use of an undeclared identifier ‘Node’:
x = malloc(sizeof(Node));
y = malloc(sizeof(Node));
z = malloc(sizeof(Node));
Welcome to SO and the wonderful world of C!
A few pointers for you:
Syntax-ically there's no problem with defining a struct inside a function, but typically it's defined outside so that it can be used in other functions. For example:
main(){
struct nodedef{vars};
add_to_node(node var);
}
add_to_node(node var)
{
// How can I add a to a node when I don't know what that is?
}
The main problem with your code is that you aren't correctly referencing your node later on, if I declaire:
struct me {
int i;
};
Then anytime I reference this type of struct, I have to explicitly say struct again:
struct me myself;
myself = malloc(sizeof(struct me));
myself.i = 5;
The way to avoid this reuse of the struct keyword is to use the typedef:
typedef struct me {
int i;
}m;
m myself;
myself = malloc(sizeof(m));
myself.i = 5;
Last point is anytime you allocate some memory via malloc() make sure you call free() to release that memory:
free(myself);
Or else you'll have a memory leak.
Try sizeof(struct Node) instead.
struct Node should be used to refer to the structure. If you want the code above works, an alternative is typedef-ing the struct Node structure as
typedef struct Node {
int value;
struct Node *next;
} Node;
Related
I don't use the C language since years and now I need it again.
I'm trying to build a Red-Black Tree but I'm stuck at the beginning because I'm missing something about "structs".
Take a look to my "structs" declarations please, they are easy.
This is a header file included in Red_black_tree.c
#define BLACK 0 //defalut color
#define RED 1 //
struct Node { //create a black node by default, to have a red one look at "create_red_node"
struct Node *left = NULL;
struct Node *right= NULL;
int key = 0;
int value = 0;
char color = BLACK;
};
struct Root_t {
struct Node* Root;
};
struct Node* create_node () {
struct Node* black = (Node*) malloc (sizeof(Node));
return black;
}
struct Node* create_red_node () {
struct Node* red = create_node ();
red->color=RED;
return red;
}
Root_t* create_tree () {
struct Root_t* fake=(Root_t*) malloc (sizeof(Root_t));
struct fake->Root->left=create_red_node ();
struct fake->Root->right=create_red_node ();
return fake;
}
I compiled with "gcc Red_black_tree.c -o RBTree".
GCC says something like : "expected ';' at end of declaration list" 7 times, or "must use 'struct' tag to refer to type... ", "expected identifier...".
What do you think, is it good to create RBTree?
In C, you can not assign while you define the members of a struct
Instead of
struct T {
int a = 1;
int b = 2;
};
you need something like
struct T {
int a;
int b;
};
...
struct T t = {.a = 1, .b = 2};
In addition, all your members are set to 0 (NULL is an alias of (void *)0), when you want to allocate memory and initialize all to zero at the same time you can use calloc
struct Node* black = calloc(1, sizeof(*black)); // Dont cast malloc and friends
And here:
struct fake->Root->left=create_red_node();
, you don't want the struct keyword, instead:
fake->Root->left=create_red_node();
Another suggestion: do not hardcode the data of the red-black-tree in the structure, (int key, value;) even if it works you end up with a non reusable container, instead, use a generic pointer to void (void *data;) and a callback to your comparison functions in the implementation.
struct Node *insert(struct Node *root, int (*comparer)(const void *, const void *)) ...
I'm trying to create a singly linked list without implementation. I'm doing this just to gain some deeper understanding on how structures work. I just want to create 2 nodes in the main function and link them. I could do that using typedef in the declaration of structures and by making the implementation, but that's not what I want (I can do that successfully).
I've written some code, but an error occurs at line 27 (the same problem appears at lines 29, 30 and 33). I know the explanation for this problem is rather simple, but I couldn't find it, either on my mind or on the web (or books). I'd appreciate any assistance in this problem. I've been programming just for the last year.
I thought in asking for help in CodeReview, but accordingly, to their FAQ, that wasn't the place to ask for help with this kind of problem.
Errors are:
27:11: error: incompatible types when assigning to type 'struct NODO ' from type 'struct NODO'
E.sig = F;
Same problem with the other lines. Everything is in the same file (.c).
Here's the full code:
#include <stdio.h>
#include <stdlib.h>
struct NODE {
int data;
struct NODE *next;
};
struct LIST {
struct NODE *first;
struct NODE *last;
int size;
};
void main(){
struct NODE E;
struct NODE F;
struct LIST L;
E.data = 1;
E.next = NULL;
F.data = 2;
F.next = NULL;
E.next = F; // line 27
L.first = E; // line 29
L.last = F; // line 30
struct NODE *ptr;
ptr = E; // line 33
while(ptr != NULL){
printf("%i\n", ptr->data);
ptr = ptr->next;
}
}
The problem is that you are trying to assign an object to a pointer. You should assign the address of your node and note the node object itself.
Try E.next = &F; and same for all the others.
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. So you have to provide the address of the memory location .In C address of a variable is derived using & operator.
Modified code :-
#include <stdio.h>
#include <stdlib.h>
struct NODE {
int data;
struct NODE *next;
};
struct LIST {
struct NODE *first;
struct NODE *last;
int size;
};
int main(){
struct NODE E;
struct NODE F;
struct LIST L;
E.data = 1;
E.next = NULL;
F.data = 2;
F.next = NULL;
E.next = &F; // not E.next = F;
L.first = &E; // not L.first = E;
L.last = &F; // not L.last = F;
struct NODE *ptr;
ptr = &E; // not ptr = E;
while(ptr != NULL){
printf("%i\n", ptr->data);
ptr = ptr->next;
}
return 0;
}
Recommended to use int main() instead of void main().
Output :-
1
2
I've found the solution or at least some additional information that led me towards the solution minutes after sending the question to SO.
In Wikipedia (https://en.wikipedia.org/wiki/Struct_(C_programming_language)#Pointers_to_struct) I've found the next snippet of code:
struct point {
int x;
int y;
};
struct point my_point = { 3, 7 };
struct point *p = &my_point; /* To declare and define p as a pointer of type struct point,
and initialize it with the address of my_point. */
My mistake was using the pointer to point to the structure and not to the structure's address. Pointers can point only to addresses (they store its value), they cannot point to objects.
I'm trying to allocate memory for the code,of which i've only included excerpts from the actual program, that follows below, the problem I am having is that i don't know how to allocate memory to the type Key that lies within BStree_node this leads to the issue of segmentation errors when i try to assign values to variables within Key.
typedef int Data_Item;
typedef char* Sub_Key;
typedef struct {Sub_Key key1; Sub_Key key2;} Key;
struct BStree_node{
Key key;
Data_Item data;
struct BStree_node *left, *right;
}
typedef struct BStree_node BStree_node;
typedef BStree_node** BStree;
BStree bs_tree_ini(void){
BStree tempTreePointer;
tempTreePointer = malloc(sizeof(BStree_node*));
BStree_node *tempNode;
tempNode = malloc(sizeof(BStree_node));
tempNode = NULL;
tempTreePointer = &tempNode;
return tempTreePointer;
}
You could initialize your node like this, using calloc to zero the memory to initialize all the fields properly:
BStree_node *init_node()
{
BStree_node *rval = calloc(1,sizeof(BStree_node)); // so all data & pointers are zeroed
return rval;
}
use it like this: init main, and only left. right stays zeroed: no right node for that main node.
int main()
{
BStree_node *head = init_node();
head->left = init_node();
...
return 0;
}
I had written a program in C to implement a simple stack. But I am getting segmentation fault in my program and finding it hard to find out what is wrong. Can any one help,
#include<stdio.h>
#include<stdlib.h>
struct stack_structure{
int stack_array[10];
int stack_pointer;
};
void push_into_stack(struct stack_structure *,int);
int main(){
int no = 8;
struct stack_structure *st;
st->stack_pointer = -1;
push_into_stack(st,no);
return 0;
}
void push_into_stack(struct stack_structure *s,int no){
s -> stack_pointer++;
s -> stack_array[s -> stack_pointer] = no;
}
struct stack_structure *st;
This only creates a pointer to a struct stack_structure. It does not allocate memory for the struct stack_structure itself.
You can try with this:
struct stack_structure st;
st.stack_pointer = -1;
push_into_stack(&st,no);
The other option is to dynamically allocate (and free) that structure:
struct stack_structure *st = malloc(sizeof(struct stack_structure));
...
// when you're done with it
free(st);
See these lines:
struct stack_structure *st;
st->stack_pointer = -1;
You've declared a pointer variable but then you're using it uninitialized. A pointer has to point at something, and this one doesn't have anything to point to. The simplest fix would be to change these lines to:
struct stack_structure st1, *st=&st1;
st->stack_pointer = -1;
You need to malloc some space for the structure:
struct stack_structure *st = malloc(sizeof(struct stack_structure));
It's amazing how even the littlest program can cause so much trouble in C.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node *leftChild;
struct node *rightChild;
} node;
typedef struct tree {
int numNodes;
struct node** nodes;
} tree;
tree *initTree() {
tree* tree = (tree*) malloc(sizeof(tree));
node *node = (node*) malloc(sizeof(node));
tree->nodes[0] = node;
return tree;
}
int main() {
return 0;
}
The compiler says:
main.c: In function 'initTree':
main.c:17: error: expected expression before ')' token
main.c:18: error: expected expression before ')' token
Can you please help?
You're using two variables named tree and node, but you also have structs typedefed as tree and node.
Change your variable names:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node *leftChild;
struct node *rightChild;
} node;
typedef struct tree {
int numNodes;
struct node** nodes;
} tree;
tree *initTree() {
/* in C code (not C++), don't have to cast malloc's return pointer, it's implicitly converted from void* */
tree* atree = malloc(sizeof(tree)); /* different names for variables */
node* anode = malloc(sizeof(node));
atree->nodes[0] = anode;
return atree;
}
int main() {
return 0;
}
tree and node is your case are type names and should not be used as variable names later on.
tree *initTree() {
tree *myTree = (tree*) malloc(sizeof(tree));
node *myNode = (node*) malloc(sizeof(node));
myTree->nodes[0] = myNode;
return myTree;
}
Change (tree*) and (node*) to (struct tree*) and (struct node*). You can't just say tree because that's also a variable.
Change the body of initTree as follows:
tree* myTree = (tree *)malloc(sizeof(tree));
node *myNode = (node *)malloc(sizeof(node));
myTree->nodes[0] = myNode;
return myTree;
Don't use typedef'ed names as variable names, and there is not need to cast malloc(); in C.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node *leftChild;
struct node *rightChild;
} node;
typedef struct tree {
int numNodes;
struct node** nodes;
} tree;
tree *initTree() {
tree->nodes[0] = malloc(sizeof(node));
return malloc(sizeof(tree));
}
int main() {
return 0;
}
I second that Mehrdad's explanation is to the point.
It's not uncommon that in C code you define a variable with the same name as the struct name for instance "node node;". Maybe it is not a good style; it is common in, e.g. linux kernel, code.
The real problem in the original code is that the compiler doesn't know how to interpret "tree" in "(tree*) malloc". According to the compiling error, it is obviously interpreted as a variable.
Apart from the original question, this code, even in it's correct forms will not work, simply due to the fact that tree::nodes (sorry for the C++ notation) as a pointer to a pointer will not point to anything usefull right after a tree as been malloced. So tree->nodes[0] which in the case of ordinary pointers is essentially the same like *(tree->nodes), can't be dereferenced. This is a very strange head for a tree anyway, but you should at least allocate a single node* to initialize that pointer to pointer:
tree *initTree() {
/* in C code (not C++), don't have to cast malloc's return pointer, it's implicitly converted from void* */
tree* atree = malloc(sizeof(struct tree)); /* different names for variables */
/* ... */
/* allocate space for numNodes node*, yet numNodes needs to be set to something beforehand */
atree->nodes = malloc(sizeof(struct node*) * atree->numNodes);
node* anode = malloc(sizeof(struct node));
atree->nodes[0] = anode;
return atree;
}
Interestingly, it does compile cleanly if you simply write the allocations as:
tree *tree = malloc( sizeof *tree );
It is often considered better style to use "sizeof variable"
rather than "sizeof( type )", and in this case the stylistic
convention resolves the syntax error. Personally, I think
this example is a good case demonstrating why typecasts are
generally a bad idea, as the code is much less obfuscated if
written:
struct tree *tree = malloc( sizeof *tree );