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 09-09-2011, 04:42 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 149
crashmonkey is on a distinguished road
Default creating a pointer to an array of pointers?

I have a class that contains a c array of pointers declared as...
Code:
struct blah  *array[15];
I need to create a pointer in another class that references the array such that i can search the array and change the data its pointers reference from within the other class.
I have tried declaring the pointer as
Code:
struct blah *pointer;
and even as a pointer to a pointer...
Code:
struct blah **pointer;
but I can't seem to access the array data properly. I just get crashes and BAD ACCESS errors.

I am assigning the pointer in a viewController viewDidLoad method.
The viewController contains both classes as ivars.
I'm not sure I am performing the assignment correctly, which may be the problem.
Code:
class2.pointer = [class1 getArray];
The getter method in class1 looks like this..
Code:
- struct blah *getArray{
     return array[0];
}
When I try to access the array from the class2 pointer I am doing this...

Code:
struct blah *new = pointer+index;
         if (new != NULL)
                do something;
This seems like it should be pretty straightforward , but I am not referencing or dereferencing the pointers correctly.
I'm thinking I am trying to get a pointer to point to the beginning address of the array that holds the pointers to other memory. I would then randomly access any of the array pointers using pointer math on the class2 pointer.
I don't know the proper syntax.
crashmonkey is offline   Reply With Quote
Old 09-09-2011, 05:18 PM   #2 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 149
crashmonkey is on a distinguished road
Default

Awesome! I figured it out.

It was just a simple matter of syntax.

Using a pointer to a pointer was correct
Code:
struct blah **pointer;
Then in the getter method I get the address of the first element of the array...
Code:
- (struct blah **)getArray{
     &return array[0];
}
Then when accessing the array with the pointer I needed parenthesis around the pointer expression..
Code:
struct blah *new = *(pointer+index);
         if (new != NULL)
                do something;
That works. A pointer to an array of pointers.
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: 408
10 members and 398 guests
apatsufas, Eclectic, fiftysixty, JackReidy, jeroenkeij, teebee74, tim0504, UMAD, yomo710, yuncarl28
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

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