Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Silly Question about linked lists

Status
Not open for further replies.

marsd

IS-IT--Management
Apr 25, 2001
2,218
US
I have the following code:
#include <stdio.h>
#include <stdlib.h>

struct R1 {
char *name;
int index;
float (*foo)(void);
struct R1 *next;
};

float bogus(void);
void PrintList(struct R1 *ptr);
struct R1 *newlist(int i);

int main(void) {
struct R1 foobar = { &quot;mytest&quot;, 0, bogus, NULL};

foobar.next = newlist(foobar.index);
PrintList(&foobar);
PrintList(foobar.next);
free(foobar.next);

return 0;
}

float bogus(void) {
return rand() % 6;
}

void PrintList(struct R1 *ptr) {
printf(&quot;Name = %s, index = %d, return from float function = %f\n&quot;, ptr->name,
ptr->index, ptr->foo());
}

struct R1 *newlist(int i) {
struct R1 *newfoo;
newfoo = malloc(sizeof(struct R1));

if (newfoo) {
printf(&quot;List name: &quot;);
newfoo->name = malloc(30);
scanf(&quot;%s&quot;, newfoo->name);
newfoo->index = i + 1;
newfoo->foo = bogus;
newfoo->next= NULL;
}
return newfoo;
}

Say I want to keep track of the order in this list using the index member, how would you go about iterating through the index members? This is not urgent or anything, I'm just
curious. Also, is there a memory leak there with not releasing newfoo->name?
 
Never Mind, figured it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top