LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-14-2007, 06:38 PM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Linked list C


If I create a function to generate a linked list would the code below be correct for passing that list back to the calling program ?

please note that this is not the program im writeing but just an example of what im doing with the pointers.


Code:
 
typedef tlist { 
                 int option; 
                 int data; 
                 struct tlist *nextrec; 
              }NODE; 
 


void makelist(NODE *); 



int main () 
{ 
      NODE *storeit; 
   
      makelist(&storeit); 
 
return 0; 
} 
 

void makelist(NODE *passdat) 
{ 
     NODE *new = NULL; 


     /* code to make list */ 


    passdat = new; 
}
 
Old 06-14-2007, 07:42 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Your call to makelist() requires a pointer to a NODE, which is what storeit is, so your call only needs to be:

makelist(storeit);
 
Old 06-17-2007, 01:36 PM   #3
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
No, it should be

Code:
*passdat = new;
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;
}
 
Old 06-18-2007, 11:21 AM   #4
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
I totally agree with dawkcid.
The better way would be returning the pointer from the function.
 
Old 06-18-2007, 01:37 PM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I think its just personal taste traditionally return values indicate success and failure in C for this sort of thing.
So that would be something like:
Code:
int main()
{
   NODE *storeit;
   if( ! makelist(&storeit) )
   {
      /*do something */
      return 0;
   }
   ....
}

int makelist(NODE** node)
{
    *node = malloc(sizeof(NODE));

    ... code to make a list ...

   return *node ? 1 : 0;
}
 
Old 06-18-2007, 04:30 PM   #6
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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.
 
Old 06-18-2007, 09:48 PM   #7
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,599
Blog Entries: 4

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
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...):
  1. It returns a result that reflects the 'result' of what it set out to do ... or...
  2. It returns an indication that it (plausibly) "had nothing to do," or...
  3. 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.'"

Selah....
 
Old 06-19-2007, 12:23 AM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I guess that I mistook the situation when I replied. So let me be a little clearer.

If the main() function is going to allocate the memory for the initial node then it only needs to pass the pointer around, hence:
Code:
struct node
{
   int data;
   node* next;
};

void printList (node* list)
{
   while (list)
   {
      printf("%d ", list->data);
      list = list->next;
   }
}

void buildList (node* list)
{
   list->data = 1;
   list->next = (node*)malloc(sizeof (node));
   list->next->data = 2;
   list->next->next = 0;
}

int main(int argc, char *argv[])
{
   node * list;
   list = (node*)malloc(sizeof (node));
   buildList(list);
   printList(list);
}
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:
Code:
struct node
{
   int data;
   node* next;
};

void printList (node* list)
{
   while (list)
   {
      printf("%d ", list->data);
      list = list->next;
   }
}

void buildList (node** pList)
{
   node* list;
   list = (node*)malloc(sizeof (node));
   list->data = 1;
   list->next = (node*)malloc(sizeof (node));
   list->next->data = 2;
   list->next->next = 0;
   *pList = list;
}

int main(int argc, char *argv[])
{
   node * list;
   buildList(&list);
   printList(list);
}
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.
 
Old 06-19-2007, 07:00 PM   #9
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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.
 
Old 06-19-2007, 07:13 PM   #10
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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
 
Old 06-21-2007, 11:13 AM   #11
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
Quote:
Originally Posted by exvor
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).
 
Old 06-21-2007, 03:04 PM   #12
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
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.
 
Old 06-21-2007, 03:36 PM   #13
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
That's not a good idea. It will mean your code can only operate on one list. What if you need multiple lists?
 
Old 06-21-2007, 06:05 PM   #14
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
Not sure.... Every time I try and move it to the pointer and do anything with it I get a compiler error.

I just need to learn more about using pointers and such i think.
 
Old 06-22-2007, 06:06 PM   #15
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
I have resolved all of my issues with this. thanks for all your help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked list manas_sem Programming 3 12-21-2006 01:53 AM
C linked list exvor Programming 4 04-28-2006 05:25 AM
C++ Linked List question lowpro2k3 Programming 3 06-16-2005 10:15 AM
linked list + c dilberim82 Programming 5 05-04-2005 11:48 PM
cirular linked list pantera Programming 8 04-21-2005 06:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration