struct FailedTransaction{
OrderNodePtr order;
int failureID;
struct FailedTransaction* next;
struct FailedTransaction* tail;
};
typedef struct FailedTransaction* FailedTransactionPtr;
struct SuccessfulTransaction{
OrderNodePtr order;
struct SuccessfulTransaction* next;
struct SuccessfulTransaction* tail;
};
typedef struct SuccessfulTransaction* SuccessfulTransactionPtr;
struct FinalReport{
FailedTransactionPtr failedTransactions;
SuccessfulTransactionPtr successfulTransactions;
};
struct FinalReport* report = NULL;
This code is declared above main. When accessing
report->successfulTransactions
or
report->failedTransactions
I get undefclared identifier for FailedTransaction and SuccessfulTransaction.
Here is the code that manipulates report
if(report == NULL){
report = malloc(sizeof(struct FinalReport));
report->failedTransactions = NULL;
report->successfulTransactions = NULL;
}
if(outcome){
if(report->successfulTransactions == NULL){
report->successfulTransactions = malloc(sizeof(SuccessfulTransaction));
report->successfulTransactions->order = temp;
report->successfulTransactions->tail = report->successfulTransactions;
}else{
report->successfulTransactions->tail->next = malloc(sizeof(SuccessfulTransaction));
report->successfulTransactions->tail->next->order = temp;
report->successfulTransactions->tail = report->successfulTransactions->tail->next;
}
}else{
if(report->failedTransactions == NULL){
report->failedTransactions = malloc(sizeof(FailedTransaction));
report->failedTransactions->order = temp;
report->failedTransactions->tail = report->failedTransactions;
}else{
report->failedTransactions->tail->next = malloc(sizeof(FailedTransaction));
report->failedTransactions->tail->next->order = temp;
report->failedTransactions->tail = report->failedTransactions->tail->next;
}
report->failedTransactions->failureID = outcome;
}
The errors occur at the first lines after each if statements and else statements.
This is for an assignment and I have been stuck on this for an hour or so (it is due tomorrow night). Can't figure out why it is happening and I can't find anything online. Any help would be appreciated.
This is the header file that contains OrderNodePtr
#ifndef _CONSUMER_
#define _CONSUMER_
struct OrderNode{
char title[250];
int id;
double cost;
char category[250];
struct OrderNode* next;
struct OrderNode* tail;
};
typedef struct OrderNode* OrderNodePtr;
#endif
Try
sizeof(struct FailedTransaction);
Or, make FailedTransaction a typedef:
struct _FailedTransaction;
typedef struct _FailedTransaction FailedTransaction;
struct _FailedTransaction {
OrderNodePtr order;
int failureID;
FailedTransaction* next;
FailedTransaction* tail;
};
Why does C need "struct" keyword and not C++?
Related
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;
}
I'm trying to create a linear linked list.
Seemed pretty simple but even though the code looks fine it won't compile.
Here's the header file and the main. Could you tell me what the problem is?
#include <malloc.h>
typedef int TYP;
typedef struct
{
TYP info;
node_linear_linked_list *next;
} node_linear_linked_list;
void init_linear_linked_list(node_linear_linked_list **manager)
{
*manager = NULL;
}
void push_linear_linked_list(node_linear_linked_list **manager, TYP info)
{
node_linear_linked_list *ptr =
(node_linear_linked_list *)malloc(sizeof(node_linear_linked_list));
ptr->info = info;
ptr->next = *manager;
*manager = ptr;
}
void insert_after_linear_linked_list(node_linear_linked_list *before, TYP info)
{
node_linear_linked_list *ptr =
(node_linear_linked_list *)malloc(sizeof(node_linear_linked_list));
ptr->info = info;
ptr->next = before->next;
before->next = ptr;
}
void pop_linear_linked_list(node_linear_linked_list **manager)
{
node_linear_linked_list *temp_ptr = *manager;
*manager = temp_ptr->next;
free(temp_ptr);
}
void delete_after_linear_linked_list(node_linear_linked_list *before)
{
node_linear_linked_list *temp_ptr = before;
before->next = before->next->next;
free(temp_ptr);
}
here's the main:
#include <malloc.h>
#include "node_linear_linked_list.h"
void main(void)
{
node_linear_linked_list *manager =
(node_linear_linked_list *)malloc(sizeof(node_linear_linked_list));
init_node_linear_linked_list(&manager);
getch();
}
Would appreciate some help. Thanks.
According to the C Standard
5 Tw o declarations of structure, union, or enumerated types which are in different scopes or use different tags declare distinct types. Each declaration of a structure, union, or enumerated type which does not include a tag declares a distinct type.
In this declaration
typedef struct
{
TYP info;
node_linear_linked_list *next;
} node_linear_linked_list;
there is declared an unnamed structure. In this data member declaration
node_linear_linked_list *next;
the name node_linear_linked_list is undefined.
You have to write for example
typedef struct node_linear_linked_list
{
TYP info;
struct node_linear_linked_list *next;
} node_linear_linked_list;
This function
void delete_after_linear_linked_list(node_linear_linked_list *before)
{
node_linear_linked_list *temp_ptr = before;
before->next = before->next->next;
free(temp_ptr);
}
has a bug. I think you mean
void delete_after_linear_linked_list(node_linear_linked_list *before)
{
if ( before && before->next )
{
node_linear_linked_list *temp_ptr = before->next;
before->next = before->next->next;
free(temp_ptr);
}
}
This statement in main
node_linear_linked_list *manager =
(node_linear_linked_list *)malloc(sizeof(node_linear_linked_list));
results in a memory leak because in the following statement
init_node_linear_linked_list(&manager);
the pointer is reassigned.
Also I advice to do a check in the functions whether a pointer passed as the argument is equal to NULL.
I need your help..
I build 3 structs :
song, listofSong and musicAlbum.
I get the message warning in ssh: "assignment from incompatible pointer type"
according to the two rows that I wrote next to them: // problem
typedef struct
{
char* nameSong;
char* nameSinger;
int lenghtOfSong;
struct song *next;
struct song *prev;
} song;
typedef struct
{
song* head;
song* tail;
} listOfsong;
typedef struct
{
int yearOfpublication;
listOfsong* listOfSongs;
int numbersOfSongs;
} musicAlbum;
song* SongCreate()
{
song *songInput =(song*) malloc(sizeof(song));
//free
songInput->nameSong = StringName();
songInput->nameSinger = StringName();
scanf("%d", &songInput->lenghtOfSong);
songInput->next = NULL;
songInput->prev = NULL;
return songInput;
}
void AddSongToList(musicAlbum *pmusicAlbum)
{
if (pmusicAlbum->listOfSongs == NULL)
{
pmusicAlbum->listOfSongs = (listOfsong*)malloc(sizeof(listOfsong));
song *newSong=SongCreate();
pmusicAlbum->listOfSongs->head = newSong;
pmusicAlbum->listOfSongs->tail = newSong;
}
else
{
song *newSong = SongCreate();
newSong->prev = pmusicAlbum->listOfSongs->tail;// problem
pmusicAlbum->listOfSongs->tail->next = newSong; // problem
pmusicAlbum->listOfSongs->tail = newSong;
}
}
anyone knows what does it means?
it is appear in other places in my project
thank you .
Change this
typedef struct
{
char* nameSong;
char* nameSinger;
int lenghtOfSong;
struct song *next;
struct song *prev;
} song;
to
typedef struct song song;
struct song
{
char* nameSong;
char* nameSinger;
int lenghtOfSong;
struct song *next;
struct song *prev;
};
I am try to build a basic linked list in C. I seem to get an error for this code:
typedef struct
{
char letter;
int number;
list_t *next;
}list_t;
char letters[] = {"ABCDEFGH"};
list_t openGame, ruyLopez;
openGame.letter = letters[4];
openGame.number = 4;
openGame.next = &ruyLopez;
ruyLopez.letter = letters[5];
ruyLopez.number = 4;
ruyLopez.next = NULL;
It won't accept my definition in the struct:
list_t *next;
And for the same reason it won't accept:
openGame.next = &ruyLopez;
When you are using list_t *next in your code, the compiler doesn't know what to do with list_t, as you haven't declared it yet. Try this:
typedef struct list {
char letter;
int number;
struct list *next;
} list;
As H2CO3 pointed out in the comments, using _t as an identifier suffix is not a great idea, so don't use list_t.
why did you make it hard on yourself just set openGame and ruzeLopez as pointers and you wont have to use the & (this is the usual way to use linked lists , just don't forget to use -> to access members)
try this code instead :
#include <stdlib.h>
#include <malloc.h>
typedef struct list
{
char letter;
int number;
struct list *next;
}list;
main(void)
{
char letters[] = "ABCDEFGH"; //what were the braces for ?
list *openGame, *ruyLopez;
openGame = ruyLopez = NULL;
openGame = malloc(sizeof(list));
ruyLopez = malloc(sizeof(list));
openGame->letter = letters[4];
openGame->number = 4;
openGame->next = ruyLopez;
ruyLopez->letter = letters[5];
ruyLopez->number = 5;
ruyLopez->next = NULL;
}
Here is a working example without the risk of memory leaks from using malloc to create your structures.
#include <stdlib.h>
typedef struct _list
{
char letter;
int number;
struct _list *next;
} list_type;
int main(void)
{
char letters[] = "ABCDEFGH"; //what were the braces for ?
list_type openGame, ruyLopez;
openGame.letter = letters[4];
openGame.number = 4;
openGame.next = &ruyLopez;
ruyLopez.letter = letters[5];
ruyLopez.number = 5;
ruyLopez.next = NULL;
}
It crys that line:
List_Node * node = (List_Node*) malloc(sizeof(List_Node));
fails on :
1>list.c(31): error C2275: 'List_Node' : illegal use of this type as an expression
1>list.c(8) : see declaration of 'List_Node'
H FILE :
#ifndef _LIST_H
#define _LIST_H
typedef struct List_Node;
typedef struct List_Struct
{
unsigned int count;
struct List_Node * root;
struct List_Node * last;
int SizeOfData;
}List_Struct;
#endif
C_FILE :
typedef struct List_Node
{
void * data;
struct List_Node * next;
}List_Node;
Status List__Add (List_Struct * This,void * const item)
{
Assert(This)
Assert(item)
struct List_Node * node = (List_Node*) malloc(sizeof(List_Node));
IsAllocated(node);
node->data = malloc(This->SizeOfData);
IsAllocated(node->data);
memcpy(node->data,item,This->SizeOfData);
node->next = NULL;
if(NULL == This->root) /*if first item to be added*/
{
This->root= node;
This->last =This->root;
}
else
{
This->last->next = node;
}
return STATUS_OK;
}
The VC compilers only support the C89 standard so variables must be declared at the beginning of a scope, before any other statements.
Change List_Add() to:
Status List__Add (List_Struct * This,void * const item)
{
List_Node* node;
Assert(This)
Assert(item)
/* Don't cast return type of malloc(): #include <stdlib.h> */
node = malloc(sizeof(List_Node));
IsAllocated(node);
...
}
You defined list node as
typedef struct List_Node
then you say struct *List_Node.
The struct is unneccessary.