Array of Structs - How do you move to the next struct in the array?

• 12-06-2005
stevong
Array of Structs - How do you move to the next struct in the array?
Hi,

Let's say I have an array of structs.
How do I move through the array to retrieve the next struct in list?

• 12-06-2005
Salem
Same as any other array - a for loop and an index ought to do it.
• 12-06-2005
stevong
But I can't seems to iterate through it.

What I intend to do is have a function that will loop through the linked list; collect all the structs and store them in an array.

This function will return the array of structs.

Then I can call the function and make use of the array of structs when neccessary.

Code:

```node *ShowAllNodes() {  node *curr=NULL;  node *arr[256];  int i;  curr=FirstNode;  while(curr)  {   arr[i]=curr;   curr=curr->Prev;  }  return *arr; }```
Then assuming I have stored data into the list, i attempted to try and make use of the data.

Code:

```node *results=NULL; .. .. ..         results=ShowAllNodes();           printf("%s\n",results[0]->IC);  /*This 2 lines hits compilation erro*/           printf("%s\n",results[1]->IC);  /**/```

• 12-06-2005
quzah
It's an array, not a linked list. (Or maybe it's both, you just haven't set it up that way perhaps.) Do you know how to walk through a string? Do the same thing, but keep in mind you're using structures instead of characters:
Code:

```for( x = 0; x < size; x++ ) {     array[ x ].something = foo;     bar = array[ x ].somethingelse; }```
Or, you could use a pointer:
Code:

```nodeptr = array; ...do fun stuff... nodeptr++; <-- move to the next array element```

Quzah.
• 12-06-2005
stevong
Thanks Salem and Quzah :)

After some reading up, I came out with the following:

Code:

```node **ShowAllNodes() {  node *curr = NULL;  node **RtnValues=NULL;  int i=0;  curr = LastNode;  while(curr)  {   RtnValues[i]=malloc(sizeof(node)); /*this line will cause it to crash*/   if(RtnValues[i]==NULL) /*this line too! */   {   printf("bah!!"); /*i dont even hav chance to see it print "bah"!"*/   exit(1);   }   RtnValues[i]->id=curr->id; /*this line will cause it to craash too.*/   strcpy(RtnValues[i]->IC,curr->IC);    /*this line will cause it to craash too.*/   curr = curr->Next;   i++;  }  return RtnValues; }```
Code:

```node **results=NULL; .. .. ..         results=ShowAllNodes();       printf("%i\n", results[0]->id);       printf("%s", results[0]->IC);```
But the program will crash whenever it hits any lines with "RtnValues[i]"

What would possible went wrong?
• 12-06-2005
stevong
I came up with something "similiar". And it works fine. Im puzzled.

Code:

```person **testing() {   person **test;   test[0] = malloc(sizeof(person));   test[1] = malloc(sizeof(person));   test[0]->name="ONE";   test[1]->name="TWO";     return test; } int main() {  person **ptr;  ptr=testing();  printf("%s\n",ptr[0]->name);  printf("%s",ptr[1]->name);  getchar();    return 0; }```
• 12-06-2005
quzah
You lucked out. You have no 'test[0]' and 'test[1]', because you didn't allocate any pointers. I don't know why you're using a set of double pointers there. You just need one.
Code:

```struct node *list; struct node *ptr; size_t x; list = malloc( N * sizeof( struct node )  ); /* allocate an 'array' of N nodes */ if( !list ) {     abandonship( ); } for( ptr = list; ptr < list + N; ptr++ ) {     ... do something with this node ... } for( x = 0; x < N; x++ ) {     list[ x ].dosomethingwithme = dosomethinghere( ); } free( list );```
Quzah.
• 12-06-2005
stevong
Quote:

Originally Posted by quzah
You lucked out. You have no 'test[0]' and 'test[1]', because you didn't allocate any pointers. I don't know why you're using a set of double pointers there. You just need one

Quzah.

*blushed*
I often used trial and error to venture into new territories and trying out new stuffs. =p My luck indeed ran out. haha.

I've used pointer to pointer because

**ptr is the same as *ptr[] ?

Pointer to array of strings.. So i thought the concept should be similiar with structs?

I believe not many agree with trial and error. Programming requires full understanding of the things you are playing with. That's the difference between a rookie and seasoned professionals I supposed :D
• 12-06-2005
cbastard
Quote:

I've used pointer to pointer because

**ptr is the same as *ptr[] ?
NO, its not.
• 12-06-2005
Salem
I'm puzzled as to why something called showallnodes should need to call malloc, or why it seems to start at the last node of the list.
• 12-06-2005
stevong
Quote:

Originally Posted by Salem
I'm puzzled as to why something called showallnodes should need to call malloc, or why it seems to start at the last node of the list.

My concepts on memory alloc and pointers are still totally screwed. Im confusing myself at times. (I feel guilty posting all these threads at times)

Ok, here's the oringinal show all nodes, which i feel it's not very useful since it only prints out onto the screen:

Code:

```void ShowAllNodes() {  node *curr = NULL;  curr = LastNode;  while(curr)  {   printf("  %i",curr->id);   printf("  %s",curr->IC);   printf("\n");   curr = curr->Next;  } }```
The above showallnodes do not require malloc is because memory is already allocated in the Add Node functions. It's job is just to output to the screen.

So I thought: Why not write a function which returns all the items and values in the link list and use a variable to keep hold of it; then I would use it whenever I want...and the result is: This thread..

Why I malloc in showallnodes is because, erm, need memory space to keep the information? then this info can be returned to the user.
• 12-06-2005
itsme86
I'm not sure of the point, unless you're just looking for a way to quickly access nodes in your linked list by index. I've used a similar method before by creating an array of pointers to my linked list nodes. That way I could walk through all of the nodes quickly by using the linked list, or I could randomly access a node in the linked list quickly by index instead of having to walk to it through the linked list. In order to maintain integrity I'd set my array elements to NULL for deleted nodes.

Am I totally off on your intentions?
• 12-06-2005
stevong
Quote:

Originally Posted by itsme86
I'm not sure of the point, unless you're just looking for a way to quickly access nodes in your linked list by index. I've used a similar method before by creating an array of pointers to my linked list nodes. That way I could walk through all of the nodes quickly by using the linked list, or I could randomly access a node in the linked list quickly by index instead of having to walk to it through the linked list. In order to maintain integrity I'd set my array elements to NULL for deleted nodes.

Am I totally off on your intentions?

You meant, something similiar to this? My struct has a member int which auto increment by one...

Code:

```node *GetNode(int idx) {  node *curr = NULL;  curr=LastNode;  while(curr)  {   if(idx==curr->id)return curr;   curr=curr->Next;  } }```
RecordCount is a function which loops thru the list to calculate the number of nodes in the list..

Code:

```        rc=RecordCount();         for(c=0;c<rc;c++)         {           singlenode=GetNode(c);                  printf("%s\n",singlenode->IC);         }```