Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 12-14-2003, 07:22 PM   #1
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Question Structs and pointers in C

Okay, I've been hammering away at this for more time than I care to admit. What this program is supposed to do is create a linked list of random numbers, print them out, and find the smallest one. I haven't been able to successfully code the function that finds the smallest number. I suspect it is something with the pointers in that function.

If anyone could please tell me where the heck I'm going wrong, I'd greatly appreciate it.
Attached Files
File Type: txt struct prog.txt (2.2 KB, 43 views)
__________________
There are two secrets to staying young, being happy, and achieving success. You have to laugh and find humor every day, and you have to have a dream.

Last edited by Force Flow; 12-14-2003 at 11:50 PM.
Force Flow is offline   Reply With Quote
Old 12-14-2003, 07:34 PM   #2
Member (8 bit)
 
Join Date: Dec 2003
Posts: 130
struct_prog.exe - 1 error(s), 0 warning(s)

EDIT: I think its a bug that wont let the code execute properly...

Last edited by monster; 12-14-2003 at 07:42 PM.
monster is offline   Reply With Quote
Old 12-14-2003, 10:57 PM   #3
Member (7 bit)
 
Join Date: Sep 2003
Posts: 90
normally, i charge for this kind of work, but i'll let you off easy this time.

included is my code. i stress, MY code. i entrust you to learn from it, not to steal it. it bears some semblance to the original, but fixes many of the little things. so, please look over it and ask me questions.

i removed the JUMP typedef. it only confused me. i deal better if i explicitly know something is a pointer.

there were many unused variables. gcc on any level should warn you of these. i assume you are using some form of gcc?

the srandom function sets the seed for the random generator based on the current time, so it should change everytime you run it.

i always insert at front because it is easy and constant time.

you forget in many places to check for a NULL pointer on head. if the list is NULL your code will barf.

the specific problem (or at least one) was the assignment of p to value. p is a pointer, value is an int. gcc warns about this implicit casting also.

also, free the memory!!!! if you have access to purify, then run it. it has saved me more than once.

please respond with questions so i at least know you looked at it.

AS

Attachment removed by moderator by request due to it containing personal information

Last edited by glc; 12-15-2003 at 09:06 PM.
AerynSedai is offline   Reply With Quote
Old 12-14-2003, 11:02 PM   #4
Member (7 bit)
 
Join Date: Sep 2003
Posts: 90
oh, i forgot to mention, the code runs without warnings using gcc with the following tags:

gcc -Wall -O2

-Wall turns on all warnings and is ALWAYS a good idea

-O2 turns on cool optimizations

AS
AerynSedai is offline   Reply With Quote
Old 12-15-2003, 11:38 AM   #5
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Thanks, AerynSedai. That worked out.

btw, the random seed is strand()
Force Flow is offline   Reply With Quote
Old 12-15-2003, 04:42 PM   #6
Member (7 bit)
 
Join Date: Sep 2003
Posts: 90
are you using standard C libraries?
i used rand and srand which are in stdlib.h, but i can't find a man page for "strand"

AS
AerynSedai is offline   Reply With Quote
Old 12-15-2003, 08:55 PM   #7
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
whoops, my mistake. It is srand.
Force Flow is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:43 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2