I'm brushing up on the initialization and declaration of pointers in C.
I wrote a piece of code :
struct node
{
int data;
struct node* right=NULL;
struct node* left=NULL;
};
struct node* newNode(int data)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
return temp;
}
which returned an error. The error is :
expected ';' at end of declaration list
struct node* right=NULL;
I then changed the code to :
struct node
{
int data;
struct node* right;
struct node* left;
right = NULL;
left = NULL;
};
struct node* newNode(int data)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
return temp;
}
which returned the same error.
Finally,
I changed the code to :
struct node
{
int data;
struct node* right;
struct node* left;
};
struct node* newNode(int data)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->right=NULL;
temp->left=NULL;
return temp;
}
The above piece of code compiles without any errors. Why is this so ?
You cannot initialize a struct member inside a struct declaration. You have to do it outside, in your code. The struct declaration declares a type, not a variable that can be initialized.
Related
struct Node
{
int data;
struct Node* left, *right;
};
What exactly the meaning of this struct Node *? Is the function below returning a pointer to a Node?
struct Node* newNode(int data)
{
struct Node* node = new(struct Node);
node->data = data;
node->left = node->right = NULL;
return (node);
}
struct Node;
Is the name of your struct.
What exactly the meaning of this struct Node*?
struct Node* node;
You're declaring a pointer to the struct Node that needs to be allocated with some memory. There is no new keyword in C. You need to use malloc() to allocate the required bytes of memory. You can allocate that like this:
struct Node* node = (struct Node *)malloc(sizeof(struct Node));
Is it returning a pointer to a Node?
This line:
return (node);
Returns the type of struct Node* to the function.
I am trying to create doubly linked list with a function to add a node at the end of the list. I have two typedef structs: one for Node and one for the doubly linked list.
In insertion_last() I am getting an error when trying to set list->tail->next to new_node. The error is "pointer to incomplete class type is not allowed" and is referring to DoublyLinkedList. I suppose I have done something wrong in making the structs, but cannot quite figure out why it is not working.
#include <stdio.h>
#include <stdlib.h>
typedef struct NodeStruct
{
int data;
struct Node *next;
struct Node *prev;
} Node;
typedef struct DoublyLinkedListStruct
{
int size;
struct Node *head;
struct Node *tail;
} DoublyLinkedList;
Node* newNode(int data, Node* next, Node* prev)
{
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->data = data;
new_node->next = next;;
new_node->prev = prev;
return new_node;
};
void insertion_beginning(DoublyLinkedList* list, int new_data)
{
Node* new_node = newNode(new_data, list->head, NULL);
list->head = new_node;
if (!list->tail)
{
list->tail = new_node;
}
list->size++;
}
void insertion_last(DoublyLinkedList* list, int new_data)
{
Node *new_node = newNode(new_data, NULL, list->tail);
if(list->tail)
{
list->tail->next = new_node;
}
else
{
list->head = new_node;
}
list->tail = new_node;
list->size++;
}
You define tail and several others fields to have type struct Node *. You don't have any such struct defined.
You want to change those to use struct NodeStruct * instead.
I am getting an error in the constructor for Node inside the sizeof(Node) saying "type name is not allowed" any thought ? Thank you
struct Node {
int data;
Node *next;
};
struct LinkedList {
Node *first;
int size;
};
typedef struct Node Node;
typedef struct LinkedList LinkedList;
//constructor for node
Node* createNode(int data) {
Node * newNode = malloc(sizeOf(Node));
if (data != NULL) {
newNode->data = data;
newNode->next = NULL;
return newNode;
}
return NULL;
}
malloc(sizeOf(Node))
should be
malloc(sizeof(Node))
also I suspect that there are other compiler errors; you have to do
struct Node *
rather than
Node *
struct node {
int x;
struct node *next;
};
void allocateMemory(struct node *some_node) {
some_node = malloc(sizeof(struct node));
}
In another function:
struct node add(struct node *root, struct node *thisNode, int value)
I try to call this:
allocateMemory(thisNode->next);
I get a runtime error. It does nothing.
Yet when I do the same thing as allocateMemory() in the said function, i.e:
thisNode->next = malloc(sizeof(struct node));
It does what it is supposed to do.
What am I doing wrong?
Here in that code :
void allocateMemory(struct node *some_node) {
some_node = malloc(sizeof(struct node));
}
You can write :
void allocateMemory(struct node **some_node) {
*some_node = malloc(sizeof(struct node));
}
And while calling :
allocateMemory(&thisNode->next);
You need to paas pointer to pointer.
When you have pointer, then you can change value which is that pointer pointing to, and when you want to change the actual pointer then you need go one step deeper.
Also function add shouldn't return value but pointer?
struct node {
int x;
struct node *next;
};
void allocateMemory(struct node **some_node) {
*some_node = (struct node*)malloc(sizeof(struct node));
}
struct node* add(struct node *root, struct node *thisNode, int value) {
allocateMemory(&thisNode->next);
thisNode->x = value;
root->next = thisNode;
return thisNode;
}
I have some errors to my code and I still don't understand why it does not work.
I have the following code snippet:
void insertBefore(List *lista, Node **node, Node *newNode)
{
newNode->prev = (*node)->prev;
newNode->next = (*node);
if((*node)->prev == 0)
lista->first = newNode;
else
(*node)->prev->next = newNode;
(*node)->prev = newNode;
}
And I call it as:
insertBefore(lista,lista->first, newNode);
And the error is :
error: dereferencing pointer to incomplete type
What I tried and works(no errors but crashes when I debug):
void insertBefore(List *lista, Node **node, Node *newNode)
{
Node *anotherNode = (*node)->prev;
newNode->prev = (*node)->prev;
newNode->next = (*node);
if((*node)->prev == 0)
lista->first = newNode;
else
anotherNode->next = newNode;
(*node)->prev = newNode;
}
Here are the structures I use:
typedef struct NodeT
{
struct nodeT *prev;
struct nodeT *next;
int key;
}Node;
typedef struct ListT
{
Node *first;
Node *last;
Node *current;
}List;
Now, my question is: is there any issue when the compiler parses? I really don't figure it out.
Watch the capitalization!
typedef struct NodeT /* uppercase N */
{
struct nodeT *prev; /* lowercase N */
NodeT and nodeT are different identifiers