i have couple of linked lists in my larger program which i now want to keep in a struct (t_holder).
typedef struct s_list
{
int val;
struct t_list *next;
} t_list;
typedef struct s_holder
{
t_list *a_starts;
// more lists...
} t_holder;
now i try to figure out how i dereference this in my program.
void try_out(t_holder *list_holder, int num)
{
//assigning something to a_starts
list_holder->a_starts->val = num;
}
int main(int argc, char *argv[])
{
t_holder *list_holder;
int num;
num = 42;
list_holder = NULL;
try_out(list_holder, num);
return (0);
}
in the function "try_out" i simlpy try to assign a value to a_starts->val but my debugger shows me ACCESS_ERROR if i declare it like this
list_holder->a_starts->val = num;
For starters this typedef declarations
typedef struct s_list
{
int val;
struct t_list *next; // <===
} t_list;
is incorrect. It seems you mean
typedef struct s_list
{
int val;
struct s_list *next; // <===
} t_list;
As for your other code then you declared a null pointer
t_holder *list_holder;
//...
list_holder = NULL;
So dereferencing the null pointer results in undefined behavior.
You need to write something like the following
t_holder list_holder = { .a_starts = NULL };
//...
try_out( &list_holder, num);
and then within the function something like
void try_out(t_holder *list_holder, int num)
{
t_list *node = malloc( sizeof( *node ) );
node->val = num;
node->next = list_holder->a_starts;
list_holder->a_starts = node;
}
Related
I want to delete the first node and return the value of the deleted node. But I an getting this warning:
warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
example=(**example).next;
So, my code does not work. Can anyone help me to fix this? Thanks.
struct myStruct {
int data;
struct myStruct next;
}
int deleteNode(struct myStruct **example) {
struct myStruct *temporary;
if (temporary == NULL) {
emptyNode(temporary); // this function only returns NULL
}
temporary = *example;
example = (**example).next;
free(temporary);
return (**example).data;
}
This structure declaration contains at least two typos.
struct myStruct
{
int data;
struct myStruct next;
}
The first one is that there is no semicolon after the closing brace. And the second one is that the data member next must have pointer type.
It seems you mean
struct myStruct
{
int data;
struct myStruct *next;
};
As for the error message then in this assignment
example=(**example).next;
the left side hand operand has the type struct myStruct ** while the right hand side operand has the type struct myStruct * and these pointer types are not compatible. So the compiler issues an error.
Nevertheless the function in any case is invalid because you are using uninitialized variables like
struct myStruct *temporary;
if(temporary==NULL)
//...
The function interface is bad.because it is unclear what the function returns in case when it is called for an empty list.
The function can be declared and defined the following way.
int deleteNode( struct myStruct **example, int *data )
{
int success = *example != NULL;
if ( success )
{
struct myStruct *temporary = *example;
*example = ( *example )->next;
*data = temporary->data;
free( temporary );
}
return success;
}
And it can be called as it is shown below
#include <stdio.h>
#include <stdlib.h>
struct myStruct
{
int data;
struct myStruct *next;
};
int deleteNode( struct myStruct **example, int *data )
{
int success = *example != NULL;
if ( success )
{
struct myStruct *temporary = *example;
*example = ( *example )->next;
*data = temporary->data;
free( temporary );
}
return success;
}
int main(void)
{
struct myStruct *head = 0;
// fill the list
int data;
if ( deleteNode( &head, &data ) )
{
printf( "The deleted value is %d\n", data );
}
else
{
puts( "The list is empty." );
}
return 0;
}
I'm writing a custom list with some operations on it but i'm having trouble with the "deferencing pointer to incomplete type error"
here are the struct definitions:
typedef struct TIME_NODE {
int timeout;
int seq_number;
struct time_node* next;
} time_node;
typedef struct TIMEOUT_LIST {
struct time_node* head;
} timeout_list;
And here is one of the functions
void insert(timeout_list *l, int sequence_number, int timeout)
{
int c=0;
struct time_node* temp;
temp = l->head;
if(temp==NULL)
{
add(sequence_number, timeout);
}
else
{
while(temp!=NULL)
{
if(temp->timeout < timeout) {
c++;
}
temp = temp->next;
}
if(c==0)
add(sequence_number, timeout);
else if(c<count())
add_after(sequence_number, timeout, ++c);
else
append(sequence_number, timeout);
}
}
Why can't I deference temp?
This is wrong
struct time_node* temp;
it should be
struct TIME_NODE* temp;
but since you have typedefed it, then this should be enough
time_node* temp;
and you should fix it in the struct TIMEOUT_LIST too.
If you want either syntaxes to work then you have to use the same name for the struct and for the typedef like this
typedef struct time_node {
int timeout;
int seq_number;
struct time_node* next;
} time_node;
and you can even do it this way
typedef struct time_node time_node;
struct time_node {
int timeout;
int seq_number;
time_node* next;
};
typedef struct TIME_NODE {
int timeout;
int seq_number;
struct time_node* next;
} time_node;
That will create a struct type called TIME_NODE (which can be declared using struct TIME_NODE, and then typedefs it to time_node.
You either need to declare the variable using the type struct TIME_NODE*, or time_node*.
this is the function witch is used to print all words in the tries .But some time I am getting error .Can't understand where went wrong ???please help me..thanks a lots.
typedef int boolean;
typedef struct test_struct test_struct_t;
struct test_struct {
boolean end;
int freq;
char* word;
test_struct_t *next;
test_struct_t *child[26];
};
typedef struct trie trie_t;
struct trie {
struct test_struct *root;
int count;
};
void printContent(test_struct_t *head) {
for(int i=0;i<26;i++) {
if(head->child[i]->w!='1') {
if(head->child[i]->end==TRUE) {
printf("%s (%d)\n",head->child[i]->word,head->child[i]->freq);
}
printContent(head->child[i]);
}
}
}
in this line if(head->child[i]->w!='1'){, why there is a w?
maybe you can try this:
if( (head->child[i] != null) && head->child[i]->word!='1'){
typedef char Last_t[MAXL];
typedef char Rest_t[MAXR];
typedef struct NodeTag {
Last_t Last;
Rest_t Rest;
struct NodeTag *Link;
} Node;
typedef struct {
Node *Index[26];
Node *L;
} ContactList;
// parameter to take in a char argument to set it to contact.Last
void INS( Node *cn )
Node contactName;
contactName.Last= cn;
// temp->data=num;
//contactName.Rest=restName;
}
//cant figure out how to pass a char argument
int main(void)
{
INS("David");
}
void INS should be passing an argument of type char *, not Node, if you want the code in your main to work.
To assign it to the member Last, you would have to use strcpy or something similar. This is because you can't assign a pointer to an array. More specifically, you can't assign a char * to char[MAXL].
You could try this:
void INS( char * cn ) {
Node contactName;
strncpy (contactName.Last, cn, MAXL);
}
int main(void){
INS("David");
return 0;
}
But, this doesn't handle errors very well. Here's a way of doing it that's more error-safe:
void INS (char * cn){
Node contactName = {
.Last[0] = 0,
.Rest[0] = 0,
.Link = 0
};
if (cn){
strncpy (contactName.Last, cn, MAXL - 1);
contactName.Last[MAXL - 1] = 0;
}
}
int main (void){
INS ("David");
return 0;
}
I'm new to C. I'm trying to pass a struct list to a function and within that function fill the list. Code is as follows:
#include <stdio.h>
#include <stdlib.h>
struct Abc {
int test;
struct Abc *next;
};
void demo_fill(struct Abc *data);
int main(int argc, char **argv) {
struct Abc *db = NULL;
demo_fill(db);
printf("%d\n",db->test);
return 0;
}
void demo_fill(struct Abc *data) {
int i;
for( i = 0; i < 5; i++ ) {
struct Abc *new;
new = malloc(sizeof(struct Abc));
new->test = i;
new->next = data;
data = new;
}
}
When running this a 'Segmentation fault (core dumped)' error occurs because the struct is still NULL when I try to print the first element. What am I doing wrong?
You're passing the pointer by value. You need to pass a pointer to a pointer if you want the change the value of the caller's pointer:
int main(int argc, char **argv) {
struct Abc *db = NULL;
demo_fill(&db);
printf("%d\n",db->test);
return 0;
}
void demo_fill(struct Abc **data) {
int i;
for( i = 0; i < 5; i++ ) {
struct Abc *new;
new = malloc(sizeof(struct Abc));
new->test = i;
new->next = *data;
*data = new;
}
}
Assigning data to new will have no effect. data is a local copy of the pointer. Pass a double pointer to fix that. Something like this:
void demo_fill(struct Abc** data) {
int i;
for( i = 0; i < 5; i++ ) {
struct Abc *new;
new = malloc(sizeof(struct Abc));
new->test = i;
new->next = *data;
*data = new;
}
}
And of course you will have to pas the pointer to db in main:
demo_fill(&db)
You can pass a pointer to the pointer as the other two answers say, so i just point out an alternative you might also consider: Instead of using a single struct you could use one struct for the list and another for entry-links:
struct link {
int test;
struct link* next;
};
struct list {
struct link* first;
};
void demo_fill(struct list* data);
You can then modify the first entry of the list without thinking about ** syntax.