![]() |
|
#1
|
||||
|
||||
yet another linked list programhello.
IM trying to build my second linked list and as usual im stuck at some really simple errors which I cannot seem to find.Heres the main source file 'list.c': CPP / C++ / C Code:
Heres the header file list.h: CPP / C++ / C Code:
And heres the output (the errors): Code:
Would love to hear from you guys!! __________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. |
||||
|
#2
|
|||
|
|||
Re: yet another linked list programOther than your somewhat "interesting" styles, you appear to have some difficulties in your understanding of how lists work.
Note: while(read_head -> next != NULL) read_head++; You can't acheive the result that you seek by incrementing the pointer! What you need to do is: CPP / C++ / C Code:
You can't "increment" the item like you do when the storage is contiquous, as is the case in an array. Here, the "next" item may point to any address, even an address prior to the "current" item...so, incrementing an item pointer becomes a rather obvious no-no. You also malloc, without checking to see that you are returned a valid pointer. There are a lot of other "issues" with your code, but work through them one at a time until you find them... I strongly encourage you to follow a typical, standardize coding style. This includes the use of braces around every conditional and every looping construct even if they are "single line" and not "multi line" blocks of code. By separating your code, you also make the boundaries of your blocks clear to others. :davis: |
|
#3
|
||||
|
||||
Re: yet another linked list programQuote:
Quote:
But my major concern is the issue of non.compatible types which is giving me a lot of headache and I cannot seem to understand why it is so. Hope to hear from people, __________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. |
|
#4
|
|||
|
|||
Re: yet another linked list programQuote:
Code:
You have an assignment statement in your header file. Since your header file is included near the beginning of your list.c file, the statement appears outside all functions. That is not allowed in C. Assignment statements must appear inside functions. As a side note: using a global "size" variable is a terrible way to implement a linked list. Look for good examples in books or on the web. Do any of them use a global variable like "size" that is used by the list functions? See footnote. As for all of the "incompatible pointer type" messages: your typedef is incorrect for any usage that I can think of. You can change it to something like the following CPP / C++ / C Code:
or CPP / C++ / C Code:
Moving the assignment statement from the header file into main() and changing the typedef to one of the forms that I showed should give you something that compiles. Regards, Dave Footnote: In general it is not recommended that you include any code or data definitions in a header file that will be #included in other files. It is not "wrong" according to the rules of the language, but you will find that it leads to fatal compile errors when projects have more than one file that includes that header. Use of global variables is also not "wrong" according to the rules of the language, and, in fact, there are places where it is appropriate to use global variables. However... Most experienced programmers will tell you that code that depends on inappropriate use of globals (like your "size") is an indicator of poor design. Many of us can also recall places where trying to debug anything other than trivial code (other people's code, that is) is much more difficult with globals. If people try to give you advice for which you don't see the point, you are free to challenge it and to ignore it, of course. |
|
#5
|
|||
|
|||
Re: yet another linked list programQuote:
Can't seem to find it? Look in the code that you posted! CPP / C++ / C Code:
It shouldn't be difficult to grep for "read_head++" and you'll easily arrive at the line noted above... :davis: |
|
#6
|
|||
|
|||
Re: yet another linked list programQuote:
When I first looked at the code, I simply commented that line out as being useless anyway. ...meaning, that even if it "did work" that it was worthless anyway in the context of a list, particularly one where the data is supposed to be a signed int and where a perfectly valid entry might be a -1 or any other valid signed int... Rather, I chose to focus on the issues of "how a list works" rather than on the "what is needed to compile" issue(s). I find it incredibly interesting to see what elements in a body of code are tuned into by various responders. :davis: |
|
#7
|
|||
|
|||
Re: yet another linked list programQuote:
Now how can you write so much code with no testing? What's the fun in that? Next it was the excess of pointers with confusing names. Why not start with the append() and destroy() and something in main() to get going and SEE how you declarations are working out before moving on. I have to start minimally and add on one thing at a time ,,, testing at each change. Howard; |
|
#8
|
|||
|
|||
Re: yet another linked list programQuote:
Davis had articulated (very well) concerns about the lack of good design, and I agree with his comments. Quote:
Sometimes when someone posts "bad code", our first reaction is to want to tell him/her to throw everything out and start all over. (And maybe that is all the advice we should give---maybe even to the extent of showing a working example with good design practices.) However... Sometimes it helps to know exactly where the error messages came from and what could be done to correct those particular problems. After all is said and done, sometimes even a well-crafted design could have bits and pieces of code that would cause error messages like the ones shown in the original post. If no one had explained where they came from, then seeing them again in the future could be pretty frustrating. I just tried to show what caused them and what could be done to correct them. Understanding why certain things don't work can be an important part of the learning process too, I'm thinking. My statement about "your first major concern" was in direct response to the original poster's comment that the "first major concern" was the other group of error messages (since there were so many of them, I guess). My point was that all of the messages were important (and indicated fatal errors), and I would start with the first one and explain and correct each and every one. (But that's just me; I'm funny that way.) Maybe it's not worthwhile to get "bad code" to compile without compiler errors, but once you start trying to run that code, you can (maybe) learn about what the design problems are (not to say the implementation errors), and (maybe) even learn some things about the proper way to implement a list. Or, maybe, not... Bottom line: People have different ways of learning and people have different ways of teaching. That's what an open forum is all about, right? Regards, Dave |
|
#9
|
|||
|
|||
Re: yet another linked list programQuote:
It is interesting that you bring up this point. Have you experienced XP and/or Agile or perhaps Scrum? There are those who put forth that there should be no code written until the test for it has been written and that running the code should initially fail the test (since the code has not yet been modified to pass the test). It is an incredibly "fun" way to produce code, but it is, in my experiences, rarely adopted due to the distinct "cultural differences" compared to "conventional" coding mindsets. Perhaps you will take a moment to try it out if you haven't already? That is, write the test first, then write the code to pass the test. That "requirement" is completed when the code successfully passes the test. The obvious benefit is that a series of tests are generated that can always be ran against the code so that one is freed of fear of change in such a way that any new changes that "break" something are quickly found when the tests are ran. Very cool in practice and very easy to adopt when the players are open-minded enough to give it a reasonable trial period. :davis: |
|
#10
|
|||
|
|||
Re: yet another linked list programQuote:
I guess maybe that's the way my team works though. (me, myself, I, and those other guys in there) For example, when we started looking into this one (for practice) we quickly commented out everything except the structure, the main() and append(). . In our minds, it would be a waste of time to even look at anything else if that was not working properly. . So the first task was to get a menu in main() to call an append() and freelist() working. I (we) just had them print a line at first. Next was to bring in the code to append() that gets a list allocated. Next was to write a freelist(). And then I have to back up sometimes and make improvements like bringing the global 'int size' into main() and learning to pass address... (tricky) I have been trying to do less global and macro declaration as has been suggested. It was a little tougher at first, but I'm getting better at it and with practice am continually getting a better feel for pointers and referencing... For me printing values and comparing to what I expected really helps a lot. It makes the screen a little trashy while building, but it's neat to see results, especially when they ARE what I expected! (sometimes) It's like hitting a mini-lottery! I hesitate on posting what I have. There are ,after all, MANY good examples laying around here. But here's how I went about getting this started (using Aijaz's declarations): CPP / C++ / C Code:
Howard; |
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 14:12 |
| linked list error message | Krandygrl00 | C++ Forum | 4 | 22-Jun-2005 15:13 |
| search linked list | itsmecathys | C++ Forum | 20 | 18-Apr-2005 02:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The