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.
Related
How can i typecast address returned by malloc to struct node type?
when i try to compile the following code it shows error every time regarding change in type.
struct node {
int info;
struct node *link;
};
struct node createnode() {
struct node *n;
n = (struct node *) malloc( sizeof(struct node) );
// error: incompatible types when returning type 'struct node *' but 'struct node' was expected
return n;
}
Your createnode function returns struct node but you return a struct node*
You should change the method signature to make it return a struct node*
This
struct node createnode()
{ ...
means your function returns a struct node, and not a struct node *.
struct node createnode()
{
struct node *n;
n=(struct node *)malloc(sizeof(struct node));
return(n);
}
Note that n is a struct node * - a pointer to a struct node.
You left off the * in your function definition:
struct node *createnode()
{
struct node *n;
n=malloc(sizeof(struct node));
return(n);
}
Note that in C you do not have to cast a void pointer. In fact, you can hide potential problems if you do.
You declared your function as returning a node, but are trying to return a node *. You probably want to change your function declaration. struct node *createnode() ...
Change struct node createnode() to struct node* createnode().
You are trying to return a pointer to node, when you are expected to return a node.
code
#include <stdio.h>
void main()
{
struct Node //defining a node
{
int data; //defining the data member
struct Node *next; //defining a pointer to Node type variable
};
struct Node *head; //declaring a pointer variable 'head' to a Node type variable.
head=NULL; //Since the head pointer now points nothing,so initialised with NULL.
struct Node* temp = (Node*)malloc(sizeof(struct Node));//creating a node and storing its adrs in pointer 'temp'
(*temp).data=2; //node's data part is initialised.
(*temp).next= NULL; //the node points to nothing initially
head=temp; //head is initialised with address of the node,so now it points to the node
printf("%d",temp->data);
}
You should write
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
instead of
struct Node* temp = (Node*)malloc(sizeof(struct Node));
because struct tags are in a different namespace than usual identifiers. So the struct keyword is necessary to specify that.Read this
Moreover include <stdlib.h> else you will receive a warning that you are implicitly declaring malloc.
Ok I am creating an ADT for a singly linked list. I have a Struct name List that stores the pointer to the first node(first item in the list, which is also a struct)and size. The node stores first name and a pointer to the next node. Here are the sturcts:
typedef struct List *ListP;
struct List{
ListP head;
int size;
};
struct node{
char name[20];
nodeP next;
};
First I called malloc to give me memory for struct List:
ListP newList;
newList = malloc(sizeof(struct List)); //should I typecast this to (ListP *)?
if (newList == NULL){
fprintf(stderr, "Memory allocation failed");
}
else{
newList->head = NULL;
newList->size = 0;
}
Then I called malloc again to give me memory for the first node:
struct node *new;
newNode = malloc(sizeof(struct node));
if (newNode == NULL){
fprintf(stderr, "Memory allocation failed");
}
else{
newNode->name = 'Jay';
newNode->next = NULL;
Now that I have my List and a new node, I assigned list->head to the address of the new node;
newList->head = newNode;
Untill this time the compiler doesn't complain. But when I try to access the elements in the first node using the pointer that I have for the list:
name = newList->head->name;
The compiler complains that struct List has no member named 'name'
How can I access the field in struct node, assuming that I only have pointer to the struct List and List->head is pointing to the first node.
Any help would be appreciated.
You declared head as a ListP when it should be of type NodeP assuming that NodeP is a node*.
Try being consistent with names. Here is a suggested revision:
// forward declarations
struct List;
struct Node;
typedef struct List *ListP;
typedef struct Node *NodeP;
struct Node{
char name[20];
NodeP next;
};
struct List{
NodeP head;
int size;
};
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 trying to build a BST and insert nodes in it. However while creating a new node I keep getting exc_bad access error.What can be the reason? Here is my code:
struct Node *node_create(struct BSTree *bst,void *nodeKey, struct Value *nodeVal, struct Node *rightChild, struct Node *leftChild)
{
struct Node *node = malloc(sizeof *node);
nodeKey= malloc (sizeof (bst->key_size));
nodeVal = malloc(sizeof(bst->value_size));
size_t sizeKey = sizeof(nodeKey);
memcpy(node->key, nodeKey, sizeKey); // exc_bad access
size_t sizeVal = sizeof (nodeVal);
memcpy(node->val, nodeVal, sizeVal); // exc_bad access
node->right = rightChild;
node->left = leftChild;
return node;
}
struct Node {
void *key;
struct Value *val;
struct Node *left;
struct Node *right;
};
struct BSTree {
size_t key_size, key_alignment;
size_t value_size, value_alignment;
int (*compare_func)(void *, void *);
struct Node *root;
// ... Maybe some other stuff.
};
struct Value {
char name[10];
int id;
};
Without knowing what Node, looks like, I'd say, even though you've allocated for node, you've not allocated all the members (which appear to be pointers).
Change your code to something like:
// Allocate node
struct Node *node = malloc(sizeof *node);
// Now members
node->key = malloc (sizeof (bst->key_size));
// :
If you are passing in the key and value, then do a memcpy of those values to the above locations. But hard to say without further code...
Without looking at the Node structure, I would guess what you want to do is:
if node is defined as
struct Node {
void *key;
struct Value *val;
struct Node *right;
struct Node *left;
};
then
struct Node *node_create(struct BSTree *bst,void *nodeKey, struct Value *nodeVal, struct Node *rightChild, struct Node *leftChild)
{
struct Node *node = malloc(sizeof *node);
node->key = malloc(bst->key_size); /* No sizeof here */
node->val = malloc(bst->value_size);
memcpy(node->key, nodeKey, bst->key_size);
memcpy(node->val, nodeVal, bst->value_size);
node->right = rightChild;
node->left = leftChild;
return node;
}
As you don't check for the returns of the mallocs (which is a design choice that can e justified), you can even write it simpler that way.
struct Node *node_create(struct BSTree *bst,void *nodeKey, struct Value *nodeVal, struct Node *rightChild, struct Node *leftChild)
{
struct Node *node = malloc(sizeof *node);
node->key = memcpy(malloc(bst->key_size) , nodeKey, bst->key_size);
node->val = memcpy(malloc(bst->value_size), nodeVal, bst->value_size);
node->right = rightChild;
node->left = leftChild;
return node;
}
There are people that cringe at this style but I prefer to not dilute my code too much on redundancies.