ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
and the original &storeit is correct, since the makelist function needs to modify the storeit pointer. However, a cleaner way would be to return the pointer from the function, i.e.,
Code:
int main()
{
NODE *storeit = makelist();
....
}
NODE *makelist(void)
{
NODE *new;
... code to make a list ...
return new;
}
Yeah Dmail in the original function it returns success and failure in the return. More so because that is how I am handling failures in the main program. The function loads data from a file and the success or failure of that is what is checked for.
Well, there is no "easy answer," and certainly no "one right answer."
But here's my take on the situation ...
A program whose purpose is to generate a linked-list should always succeed, or else it should throw an exception.
(Pardon me if I have just eclipsed the bounds of your present homework assignment, but ...) The "set of possible ending-conditions" of any function should be limited to only those which are plausibly un-exceptional. In other words:
"If it makes perfect sense that" your function might return an empty list," then your list-construction function might return null to indicate this condition.
But "if such an outcome is always erroneous," it should instead throw an exception: it should not 'return' at all!
In other words, there are three reasonable outcomes to any such function (not just two...):
It returns a result that reflects the 'result' of what it set out to do ... or...
It returns an indication that it (plausibly) "had nothing to do," or...
It throws an exception. (As in, "none of the above.")
When a function finds that it "cannot proceed," it is much better to throw an exception than to return a value (a value which makes no sense...) to the caller, thereby forcing the caller to "deal with the problem." After all, the caller, who is no 'subject-matter expert' in whatever-it-is that you are supposed to be doing, has no objective way to know what it's supposed to do with "the fact that you just returned 'null.'"
If the main() function expects the calling function to allocate the memory then the pointer is going to be read/write and so the address of the pointer is needed, hence:
I'm not certain that I fully agree with the exception argument. Exceptions have their uses in languages where they are supported by the syntax, but since they are not supported by C I would argue that a return value is more useful (not to mention expected). With a return value a calling function can then respond to the situation with a meaningful message to the user.
By the way this is not homework but just something personal im working on or have been working on for awhile now. Ive decided that i would just return the pointer to the list created. or a Null when it fails.
sundialsvcs have you been on a late night drinking session again?
As graemef pointed out C does not support exceptions so there is only two methods.
/me hides
By the way this is not homework but just something personal im working on or have been working on for awhile now. Ive decided that i would just return the pointer to the list created. or a Null when it fails.
This is probably the best choice. It is the "standard" way to create an "object".
The other way, passing the address of a pointer is best used when you need to return more than one distinct value from the function or if you're writing a library and you want a consistent API (e.g. functions return TRUE/FALSE whenever possible). This method is widely used in Xlib, for example. Note, though, that Xlib functions which allocate memory (rather than operate on the structure) usually return a pointer (e.g. XAllocSizeHints).
Well that fixed the original problem with creating the list but now I need to create functions that modify the list like adding and removing objects. Im thinking that it may just be easier to create a external pointer instead of passing the address around.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.