Dereferencing pointer error [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm creating a queue datastructure in c.
typedef struct Queue_node{
int value;
struct Queue_Node* next;
};
struct Queue_Node* front = NULL;
struct Queue_Node* R = NULL;
void Enqueue(int x)
{
struct Queue_node* temp = (struct Queue_node*)malloc(sizeof(struct Queue_node*));
temp->value = x;
temp->next = NULL;
if (front == NULL && R == NULL)
{
R = temp;
front = R;
return;
}
R->next = temp;
R = temp;
}
At line 24 (R->next = temp), the compiler tells me:
dereferencing pointer to incomplete type 'struct Queue_node'.
I cant access R->next after the declarations, why?

There are few problems in your code, firstly give alias name while doing typedef.
typedef struct Queue_node{
int value;
struct Queue_Node* next;
}Queue_node;/*put alias name here */
Secondly, malloc() memory allocation is not correct
struct Queue_node* temp = (struct Queue_node*)malloc(sizeof(struct Queue_node*));
It should be(allocate memory equal to size of Queue_node, not Queue_node* )
struct Queue_node* temp = malloc(sizeof(struct Queue_node));
Also avoid casting malloc() result. See this post Do I cast the result of malloc?

struct Queue_node{
int value;
struct Queue_Node* next;
};
or
typedef struct Queue_Node{
int value;
struct Queue_Node* next;
}Queue_Node;
will do it.

While the existing answers already told you that you are missing a name for your typedef, you have a much simpler problem:
typedef struct Queue_node{
int value;
struct Queue_Node* next;
};
What is basically wrong is a simple typo!
You define a struct Queue_node. And inside it you want to use a pointer to struct Queue_Node which does not exist.
In C struct tags, type names or any other identifiers are case sensitive.
Change your type definition like this
struct Queue_Node{
int value;
struct Queue_Node* next;
};
or
typedef struct Queue_Node{
int value;
struct Queue_Node* next;
} Queue_Node;
You also might use Queue_node but it is important to use same way of writing it throughout the whole code.

Related

How should I create a linked list with a generic amount of items per node in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need to expand a linked list in C that is able to take a generic number of (same type) values.
Here is the generic linked list:
struct Node {
int item;
struct Node* next;
};
struct List {
struct Node* head;
struct Node* tail;
};
struct List SLL_new() {
/* construct an empty list */
struct List list;
list.head = NULL;
list.tail = NULL;
return list;
}
I need to modify SLL_new() to take an integer parameter that represents the number of items that will be stored in a single node.
I could do something like:
struct Node {
int item1;
int item2;
int item3;
int item4;
int item5;
struct Node* next;
};
but this probably isn't feasible, as there's no limit on the number of items that can be stored in a node. Is there an easier way to do this?
Use a struct with a flexible array:
struct Node {
struct Node* next;
int items[];
};
Now you can initialize each node with malloc(sizeof(struct Node) + sizeof(int) * n) and access the members with node.items[i].
Update: If you can't use flexible arrays, you can still use this technique. The code changes as follows:
struct Node {
struct Node* next;
};
#define NODE_ACCESS(node, i) (((int*)((node)+1))[i])
Now you can access your integers with NODE_ACCESS(my_node, item_no).
I need to modify SLL_new() to take an integer parameter that represents the number of items that will be stored in a single node.
So do that. You're not asked how the eventual Node allocation will work yet, you just need
struct List {
int node_elements;
struct Node* head;
struct Node* tail;
};
struct List SLL_new(int nelem) {
/* construct an empty list */
struct List list;
list.node_elements = nelem;
list.head = NULL;
list.tail = NULL;
return list;
}
Once you've done that, the other answer's flexible array is how you'll actually use node_elements every time you allocate a new node in this list.

How to implement Stack using Char as argument?

I have this assignment where I have to implement a stack to work with the following struct given by my professor. As you know I don't know how to implement a stack using the following struct.
struct elements{
char word;
struct elements *next;
}
I do know how to do that using the struct below...
struct elements{
struct student data;
struct elements *next;
}
To insert the data into the stack I'd use... I know dat the code below is correct. I do understand what is happening there. But I cannot see how I can do that using char word instead of struct student data; Someone could explain it to me? I'm not getting the idea.
int pushStack(Stack* ptr, struct student info){
Elemn* node = (Elemn*)malloc(sizeof(Elemn);
if(node == NULL && ptr == NULL){
printf("error.");
return 0;
}
node->student = info;
node->next = (*ptr);
*ptr = node;
return 1;
}
Have you tried to replace struct student by char?
Of course in the function header, but also in the Elemn class.

C Data Structure Error [duplicate]

This question already has answers here:
C : typedef struct name {...}; VS typedef struct{...} name;
(6 answers)
Closed 5 years ago.
I am creating a program that uses a basic stack in C. In this I have two structures defined in the heading:
A structure named Node with a string and a pointer to a previous Node as members.
A structure named Stack with a pointer to the last Node as member.
Here are the definitions of these structures in my header file:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
const char* string;
struct Node *prev;
};
typedef struct Stack {
size_t sizeOfStack;
size_t sizeOfElem;
struct Node *last;
};
One method giving me errors is CreateStack():
CreateStack: This function creates a stack (equivalent to a constructor).
(a) Name: CreateStack
(b) Return Type: A pointer to a stack allocated in the heap.
Here is my implementation
Stack* CreateStack() {
Stack* stack = malloc(sizeof(*stack));
if (stack == NULL) {
return NULL;
}//end of if
stack->sizeOfElem = 0;
stack->sizeOfStack = 0;
stack->last = NULL;
return stack;
}//end of CreateStack
But the compiler is spitting this out:
error: 'Stack {aka struct Stack}' has no member named 'last'
stack->last = node;
error: 'Stack {aka struct Stack}' has no member named 'last'
node->prev = stack->last;
error: 'Stack {aka struct Stack}' has no member named 'last'
Node *node = stack->last;
If someone could point out the issue here I would greatly appreciate it. I am confused as to why it is saying last is not a thing, yet prev defined in the same way in the other structure does not raise a flag. Thanks.
Fix the typedefs and it'll compile:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
const char* string;
struct Node *prev;
} Node ;
typedef struct Stack {
size_t sizeOfStack;
size_t sizeOfElem;
struct Node *last;
} Stack;
Stack* CreateStack() {
Stack* stack = malloc(sizeof(*stack));
if (stack == NULL) {
return NULL;
}//end of if
stack->sizeOfElem = 0;
stack->sizeOfStack = 0;
stack->last = NULL;
return stack;
}//end of CreateStack
Your typedef statement is incomplete, as you do not define a name for the type. Write the following.
typedef struct Stack {
size_t sizeOfStack;
size_t sizeOfElem;
struct Node *last;
}Stack;
Note the Stack at the end, which defines now type Stack being equivalent to struct Stack.

Learning C - Compiling error allocating pointers

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;

Inserting Linked List Node into ascending sort singly linked List GCC error: dereferencing pointer to incomplete type

I'm trying to insert a node into the proper place (in order) in a linked list that is sorted in ascending order. I keep getting the GCC error "Error: dereferencing pointer to incomplete type". I've been working off of the code in this stackoverflow post. Below is my code:
typedef struct sNode {
int sid;
struct sNode *next;
}sNode;
sNode* addsNode (struct sNode *headPtr, int pSid)
{
struct sNode *ptr, *root = headPtr;
ptr = malloc (sizeof(struct sNode));
if(headPtr == NULL){ //In other code I've already check for a NULL list, pointer
ptr->sid = pSid;
}
else{
while(headPtr->next != NULL && headPtr->next->sid < pSid){
//while(headPtr->next != NULL){ --> Compiles when uncommented
headPtr = headPtr->next;
}//while
ptr->sid = pSid;
}//else
return root;
}//addsNode
I'm trying to return a pointer to the front of the list so other linked list manipulation can happen once returned. I've been doing some research on StackOverflow and it sounds like it's referring to the struct sNode that is causing the problem. I've seen different posts on using typedef to declare the struct. So I've tried it with and without using typedef to declare it. I've also seen posts suggesting #including & but that didn't work either. I'm using GCC 4.6.3 Any help is appreciated thanks!
typedef struct sNode {
int sid;
struct sNode *next;
};
You must typedef the struct to some name,
typedef struct sNode {
int sid;
struct sNode *next;
} sNode;
for example. Without the name you typedef it to, it's invalid anyway, and the type still must be referred to with the struct keyword.

Resources