Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-21-2011, 02:14 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 149
crashmonkey is on a distinguished road
Default Creating an array of linked list structs???

This might be too convoluted to even make sense, but I am giving it a try anyway. This seems to make sense in theory but I'm having trouble implementing it.

I want to create an array of structs in which I can make individual linked lists.
I understand how to create and manage a single linked list, but getting a bunch of them inserted into an array is not working. I want each element of the array to be a separate linked list.

According to my reference, I have defined a struct node that is the basic element of the linked list in my .h file.
Then declare the array pointer variable to hold the lists.

Code:
#import <Foundation/Foundation.h>
#import "Operators.h"

// Structure for linked list node in the array 
struct p_node {
    int *p;
    struct p_node *next;
};

@interface MySubClass : NSObject {

struct p_node   *list_array;

}
In the .m I have a method to allocate and initialize the first elements of the array. I need the first elements to all be NULL so that the lists are all empty and can be easily identified as empty. The size of the array is 150 elements.

Code:
    list_array = malloc(sizeof(struct p_node) * 150);
    
    for (int i = 0; i < 150; i++) {
       struct p_node *first = NULL;
        list_array[i] = first;
    }
I am iterating through the block of memory to set each block as a NULL pointer.
On the last line I get a compiler error saying "Assigning to 'struct p_node' from incompatible type 'struct p_node*' ".

I'm confused because I have declared the array as a struct pointer. I have set aside a block of memory to hold 150 structs, and they all have an address.
If I change the last line to
Code:
  list_array[i] = *first;
Then it will compile, but when I run it I get a crash and "BAD ACCESS" error on that line.

I'm unsure how to go about doing this since it involves mixing array and pointer syntax and exchanging a lot of pointer assignments. I understand how it is done with a single list, but not creating an array of lists.
crashmonkey is offline   Reply With Quote
Old 09-08-2011, 04:19 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 149
crashmonkey is on a distinguished road
Default

Figured it out. Thanks.
crashmonkey is offline   Reply With Quote
Old 09-08-2011, 04:23 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2011
Posts: 6
phani is on a distinguished road
Default

Quote:
Originally Posted by crashmonkey View Post
Figured it out. Thanks.
how did u fig it..

can u share wit us..

thanks in adv
phani is offline   Reply With Quote
Old 09-08-2011, 08:36 AM   #4 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 864
nobre84 is on a distinguished road
Default

You must choose between making an array of Pointers to the struct , or an array of structs. The first option will store 150 pointers, that you allocate on the fly when adding each of them. The second option, (which you are doing), would store 150 structs directly on the array. But then you tell it to add a struct pointer in the position that would have to store a structure, hence the error. Either way, you were just nulling them out, no need to declare anything. You could just set the position to NULL, or better yet, use memset(&list_array, 0, sizeof(list_aray))

Post what you really need, and what was your solution to the issue, to check if its really the best option...

Last edited by nobre84; 09-08-2011 at 08:40 AM.
nobre84 is offline   Reply With Quote
Old 09-08-2011, 09:55 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 149
crashmonkey is on a distinguished road
Default

Thanks. I opted for the first example. I created an array of struct pointers.
Declared in the class.h
Code:
struct p_node *list_array[150];
Then as part of the class initialization I assign all the indexed pointer values to NULL.
Code:
    for (int i = 0; i < 150; i++) 
        list_array[i] = NULL;
I need a NULL pointer as a condition for the checks I am running on the lists.
If a list is empty (first struct NULL), then I can pass over the process.

It took me days to figure out the proper application of pointer math, array indexing, and dereferencing to get it all to work, but it works fine now with surprisingly little code. I used the linked list code verbatum straight out of my C textbook to add and remove nodes. Never thought I would use such a convoluted amalgam of pointers, but it seemed like the proper solution for my project.

My new problem now is that I also have to declare a pointer to the list_array.
I need a pointer to the array to reference the array in another class object.
So now I am having difficulty making the proper reference and indexing my pointer as though it were the array itself.

In the original class.h I declare...
Code:
struct p_node *list_array[150];
struct p_node *list_pointer;
Then in the viewController where everything is being allocated and initialized
I assign the list pointer...
Code:
list_pointer = [class2 listArrayPointer];
I have to use a custom getter method to assign the pointer because I can't seem to declare the list_array as a property, since it is an array, or something? Here is the getter method...

Code:
- (struct p_node*)listArrayPointer{
    return list_array[0];
}
I'm not sure I am returning the proper value since I am getting a BAD ACCESS error when I try to index the list pointer in the other class.

Code:
struct p_node *list = list_pointer+index;
                for  (; list->p != NULL; list = list->next)
Again, I am not making the proper use of pointer arithmetic, array indexing, and dereferencing to get this part working. Unfortunately I'm not adequately versed in data structures to know exactly what I am doing. I'm sort of learning on the fly here, and running into roadblocks at every step because it is not logically planned out in advance.
crashmonkey is offline   Reply With Quote
Old 09-09-2011, 05:43 PM   #6 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 149
crashmonkey is on a distinguished road
Default

Ok, I finally solved the second problem.
I have to use a pointer to a pointer for the array pointer.


In the original class.h I declare...
Code:
struct p_node *list_array[150];
struct p_node **list_pointer;
Then in the viewController where everything is being allocated and initialized
I assign the address of the first element of the array to the list pointer...
Code:
list_pointer = [class2 listArrayPointer];
Here is the getter method...
Code:
- (struct p_node**)listArrayPointer{
    return &list_array[0];
}

The key was surrounding the pointer indexing expression with parenthesis...
Code:
struct p_node *list = *(list_pointer+index);
                for  (; list != NULL; list = list->next)
I found the solution, not to mention the concept of the linked list itself, in a highly recommended C textbook: C Programming A Modern Approach 2nd ed.
by K.N. King.
I owe pretty much everything I have learned and applied so far to this book. It explains all the necessary concepts of C thoroughly and clearly. Even though I obviously don't get it right away, I know the answer is in there once I go back and search for it.
crashmonkey is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 396
13 members and 383 guests
7twenty7, chiataytuday, cristofercolmbos, dedeys78, fiftysixty, gmarro, iOS.Lover, jonathandeknudt, raymng, stanny, tymex, UMAD, xerohuang
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:12 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0