Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm working on C language.
I have a pointer and I wanna create a new struct beginning with the address. Here is my way to do it and I can pass the compile but when I run it, it gets bus error and segment error.
struct node{
int value;
int value2
struct node *next;
}
int main(){
struct node a = {0, 0, NULL};
void *p = (void*)(&a + 1);
struct node *ptr = (struct node *)(p);
//These two statements below cause the problem.
(*ptr).value = 100;
(*ptr).next = NULL;
}
Could someone help me?
Try this, assuming you can only use statically defined memory and want a linked list:
struct node{
int value;
int value2
struct node *next;
};
#define LEN 10
static struct node arr[LEN] = {0};
int main(){
// Initialize the array of nodes
for (int i = 0; i < LEN; i++) {
struct node * const ptr = &a[i];
ptr->value = 100;
ptr->next = (i == (LEN-1)) ? NULL : &a[i+1];
}
}
In your code p points to a memory location that passes the address of a which is an integer, it is undefined behavior to access it anyway.
What you want is allocating the memory instead, e.g.:
struct node *ptr = malloc(sizeof *ptr);
ptr->value = 100;
ptr->next = NULL;
Related
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 6 years ago.
Improve this question
I am trying to compile my code and I am getting the Expected ';' identifier or '(' token before 'void' error on C:10:1.. can't seem to find the typo if someone can help me out I would appreciate it! I have provided my code down below I am sure it is just a silly mistake I made somewhere.
#include <stdio.h>
#include <stdlib.h>
//setting up my binary converter struct
struct bin
{
int data;
struct bin *link;
}
void append(struct bin **, int); //setting up append value
void reverse(struct bin**); //reverse values to convert to binary
void display(struct bin *); //so we can display
int main(void)
{
int nu,i; //global vars
struct bin *p;
p = NULL; //setting up p pointer to NULL to make sure it has no sense of being some other value
printf("Enter Value: ");
scanf("%d", &nu);
while (nu != 0)
{
i = nu % 2;
append (&p, i);
nu /= 2;
}
reverse(&p);
printf("Value in Binary: ");
display(p);
}
//setting up our append function now
void append(struct bin **q, int nu)
{
struct bin *temp,*r;
temp = *q;
if(*q == NULL) //making sure q pointer is null
{
temp = (struct bin *)malloc(sizeof(struct bin));
temp -> data = nu;
temp -> link = NULL;
*q = temp;
}
else
{
temp = *q;
while (temp -> link != NULL)
{
temp = temp -> link;
}
r = (struct bin *) malloc(sizeof(struct bin));
r -> data = nu;
r -> link = NULL;
temp -> link = r;
}
//setting up our reverse function to show in binary values
void reverse(struct bin **x)
{
struct bin *q, *r, *s;
q = *x;
r = NULL;
while (q != NULL)
{
s = r;
r = q;
q = q -> link;
r -> link = s;
}
*x = r;
}
//setting up our display function
void display(struct bin *q)
{
while (q != NULL)
{
printf("%d", q -> data);
q = q -> link;
}
}
You need to add semicolon (;) after declaration of your struct:
struct bin
{
int data;
struct bin *link;
};
Also, your main() function should return something. Add return 0; at the end of it.
And one more thing I noticed. Here:
int nu,i; //global vars
The comment does not make any sense, as nu and i are not global variables. They are local variables, because they are declared in the scope of your main() function.
EDIT:
The latter two comments obviously did not cause the compiling error, but I thought it would be a good think to mention them anyway.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Managing my linked-list is not working. My program just crashes after I set the value "next" to point on previous "head" of the list and after I search and find an element. What can I do?
The program compiles and starts, but it just crashes returning a random address from the memory.
I've tried to change the function to return the pointer of the new "head" instead of being void, but the result is the same.
By verifying where the program stops i found out that it stops doing "new node->next = (*head)" and on previous attempts with the instruction "return last".
I've tried changing almost completely the function just to understand the problem, but even if i pass a pointer to and already allocated list in the main the address does not work and it crashes.
Just to understand whats going on assume that the the program enters for sure in the if with the condition "type_temp=='A'"
This is the main:
#include"devices.h"
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define LINE_LENGTH 80
#define COMMAND_LENGTH 30
int main(int argc, char** argvs)
{
FILE *fp;
struct Type_A** devices_A = NULL;
struct Type_B** devices_B = NULL;
struct Type_C** devices_C = NULL;
struct Request_Type_C** requests_devices_C = NULL;
int system_power, usable_power,
solar_system_power,solar_system_power_temp;
int id_temp,power_level_temp;
int power_level_normal_temp, power_level_low_temp;
char type_temp;
char file_line[LINE_LENGTH], command[COMMAND_LENGTH];
char *sub_line;
fp = fopen("input1.txt","r");
if(fp == NULL)
{
printf("Error opening the file\n");
return 1;
}
if(fgets(file_line,sizeof(file_line),fp) == NULL)
{
printf("Error reading the first line\n");
return 1;
}
sub_line = strtok(file_line, " ");
strcpy(command,sub_line);
sub_line = strtok(NULL, " ");
system_power = atoi(sub_line);
usable_power = system_power;
while(fgets(file_line,sizeof(file_line),fp) != NULL)
{
sub_line = strtok(file_line, " ");
strcpy(command,sub_line);
if(strcmp(command,"DEVICE_CONNECTED") == 0)
{
sub_line = strtok(NULL, " ");
id_temp = atoi(sub_line);
sub_line = strtok(NULL, " ");
type_temp = *sub_line;
if(type_temp == 'A')
{
sub_line = strtok(NULL, " ");
power_level_normal_temp = atoi(sub_line);
sub_line = strtok(NULL, " ");
power_level_low_temp = atoi(sub_line);
//function with the problem 1
add_device_a(devices_A,id_temp,type_temp,
power_level_normal_temp,power_level_low_temp,0,0);
//function with the problem 2
add_device_a_to_system(devices_A,id_temp,&usable_power);
}
.
.
.
This is the first function with the problem:
void add_device_a(struct Type_A** head, int id, char type,
int power_level_normal, int power_level_low,
int connected, int consume)
{
//allocate the new node
struct Type_A *new_node = (struct Type_A*) malloc(sizeof(struct Type_A));
//put in the data
new_node->id = id;
new_node->type = type;
new_node->power_level_normal = power_level_normal;
new_node->power_level_low = power_level_low;
new_node->connected = connected;
new_node->consume = consume;
//setting the next of the new node
new_node->next = (*head); <-instruction that generates the problem
crashing the program
//move the head to point to the new node
(*head) = new_node;
return;
} //end add_device_a
This is the second function that generates the problem:
int add_device_a_to_system(struct Type_A** head, int id, int* usable_power)
{
struct Type_A *node = find_device_a_by_id(head, id);` <-instruction
that generates the problem
.
.
.
The true function that generates the problem
struct Type_A *find_device_a_by_id(struct Type_A** head, int id)
{
//used for traverse the list
struct Type_A *last = *head;
//check if last is truly the last node
while(last != NULL)
{
//if the id is equal return the pointer to that node
if(last->id == id) {
return last; **<-instruction that makes the program crash**
}
//else keep going with the next
last = last->next;
}
//didn't find an id equal to the one passed ad parameter
return NULL;
}//end of *find_device_a_by_id
Adding an element to the list should add the element on the linked list as first and modify the pointer to that entry.
Finding an element should return a pointer to that specific entry on the linked-list.
You are defining a pointer to a pointer to your struct:
struct Type_A** devices_A = NULL;
Right now it does not point to a valid struct Type_A* as you haven't created one to point it to. So when you pass this into your function add_device_a and it dereferences that variable, you invoke undefined behavior, in this case probably a segfault.
What you probably actually want is to define struct Type_A* devices_A = NULL; And then when you pass it into your function, pass in the address of that pointer:
add_device_a(&devices_A,id_temp,type_temp,
power_level_normal_temp,power_level_low_temp,0,0);
You probably want to declare devices_A as struct Type_A* devices_A = NULL (not as a double pointer), and then call add_device_a() using add_device_a(&devices_A, ....
The way you're doing it is confusing and requires that you allocate space for devices_A, also in this context dereferencing *head when devices_A is NULL is making the program read from address NULL which is undefined behavior and may cause crashes.
You can also see this question about how to manage linked lists properly.
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 3 years ago.
Improve this question
It just shows me: [1] 10531 Segmentation fault.
I don't what is the problem of my code!(I have defined a list and add an element into the list. But it just failed and I don't know how to change it.)
#include <stdio.h>
#include <stdlib.h>
typedef struct ll {
int val;
struct ll *next;
} ll_t;
void copylist(ll_t* list, int* array, int length) {
int i;
for (i = 0; i < length; i++) {
array[i] = list->val;
list = list->next;
}
}
int main() {
ll_t *list = NULL;
int *array = NULL;
list = malloc(sizeof (ll_t));
list->val = 1;
list->next = NULL;
ll_t *add1 = NULL;
add1 = malloc(sizeof (ll_t));
add1->val = 2;
add1->next = NULL;
list->next = add1;
int j;
copylist(list, array, 2);
for (j = 0; j < 2; j++) {
printf("the content of array is: %d\n", array[j]);
}
return 1;
}
Currently all that you're passing as an array is a null pointer because you instantiate it as
int *array = NULL;
instead of
int *array = malloc(2 * sizeof(int));
which allocates enough space for two integers on the heap. (two being the amount of items you're using in this test).
Also, You should use
int i = 0;
while(list != NULL) {
array[i] = list->val;
list = list->next;
i++;
}
instead of passing the length value, as you know when you reach the end of a linked list when you reach a null value. If someone were to input the wrong length by accident in your program it would lead to another segmentation fault.
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.
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.