I created a function which pass a value to struct in a header structure file, however wrong value returned when I inspect it by other functions, the is code as following:
int ListInit(List* sList, Key key1)
{
sList->count = 0;
sList = NULL;
sList->ckey = key1; /* I need this to by stored in the header file which is below*/
return SUCCESS;
}
Header file
____________________________
typedef struct CarList
{
unsigned ckey; /*in this variable sould be stored then retrieved by the below function*/
} List;
int ListInsert(List *sList){
unsigned key = sList->ckey /* it returns wrong values*/
switch(key){
case 0:{
MakeCompare(sList->temp, sList break;
}
}
}
teach yourself as you can, when you get stuck ask and do not shy
sList = NULL; // set the poiner to NULL
This actually doesn't change anything of the list itself, only the pointer that previously pointed to it now points to nothing.
sList->ckey = key1 // dereference
Since sList is NULL at this point this will invariably create an access violation.
I need the exact declaration of the structure if I am to help you.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Car *temp;
int ListInit(List *sList, Key key1, int(*MakeCompare)(Car *, Car *), int(*ModelCompare)(Car *, Car *),
int(*YearCompare)(Car *, Car *), int(*RegCompare)(Car *, Car *))
{
and the following is the description of what to do with ListInit function:
int ListInit(List*, Key, int(*MakeCompare) (Car*, Car*),
* int(*ModelCompare) (Car*, Car*),
* int(*YearCompare) (Car*, Car*),
* int(*RegCompare) (Car*, Car*));
* - this function *must* be used to initialise a List
* object before any other interface routine can be
* safely applied to the List
* - accepts a pointer to an existing List variable, and
* initialises it to represent a "new empty list"
* - the user must specify what field is to be used for the ordering
* - the next 4 parameters are the function pointers to compare 2 car
* structures for each of the Keys. You will need to keep this somewhere
* in your structure so you can call the appropriate function when needed.
* The functions must return -1 if the first is less than the second, 0 if
* they are equal and 1 otherwise. You must write 4 separate functions
* to be passed in for the 4 different field orderings.
* - this function returns true if successful, and false
* if initialisation was unsuccessful
_____________________
if any help please;
thanks in advaned
teach yourself as you can, when you get stuck ask and do not shy
It doesn't make any sense. This is why you should ALWAYS, without exception, use typedefs when working with function pointers. This is also the reason why you should ALWAYS think about every cast you do. Casting basically should be kept to the absolute minimum.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Hi again, following are the instructions of how Insert function should work;
Note: the previous declaration of Comparsion functions gave me error message "Redeclartion of function and did not work till I changed them to the previous states"
* int ListInsert(List*, char *, char *, char *, char *);
* - accepts a pointer to an existing List variable,
* and 4 pointers to strings that represent the information
* of the car to be added to the List. You will use the compare
* function for the default ordering passed in at the ListInit stage
* to find the correct place to insert the new structure. Ie. if
* the Key is REG then the function passed in must compare the
* registration of each structure.
* - this function returns true if successful, and false
* if unsuccessful
*
teach yourself as you can, when you get stuck ask and do not shy
I think I now see the reason why this is called a list. It's tedious to implement using an array. Look into double-linked lists, they are more helpful in this case.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
you are right list of array,, this the first part and the second part we will implement link-list with same instructions,
how can I get help from double-linked lists.
I am really in stuck using that function pointers and Insert function, I asked my teacher he said you need first to allocat a location for new entry and check wether the list is empty or it needs to put the new node at tail or head, I got stuck to solve that out.
Could you give outlines to start at least with correct track please , as you saw I lose truck with cast and it's a hint that it was bad start with this subject.
do the best if you can please, and thanks to have responded me
teach yourself as you can, when you get stuck ask and do not shy
The problem is that it is complicated to insert an element at an arbitrary position in a normal array. This is why you should use a linked list.
A linked list is basically two structures. A list structure:
struct List {
Node *head;
Node *tail;
// if you want you can add a count:
int count;
};
and a Node structure:
struct Node {
Node *prev;
DataType data;
Node *next;
};
This is a double-linked list because it has both a prev and a next pointer. Single-linked lists only have the next pointer. This lets them save space, but they are slower and harder to implement.
Linked lists are perfect to wrap in classes. I've attached an example linked list I've made a while ago. It's the simplest llinked list class I could think of that fulfills the following criteria:
Hide the Node structure from the user and provide abstract access via an iterator.
Expose a subset of the STL linked list function: clear, push/pop_back/front, insert, remove, begin, end
Provide a fully functional iterator class
Let it store any type through the use of templates.
The code is not commented so you'll have to ask me if you have questions.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
It's not very hard to convert it to C code. You can't hide things from the user anymore, but the data and the way it works remains the same.
For the array:
When you need to insert an element, search for the insert position in the array. Then use memmove(PointerToLocationWhereToInsert+1, PointerToLocationWhereToInsert, ByteSizeOfElementsRemaining) to create space for the new element. Then insert the element.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.