6

a.H:

-(NSArray *) returnarray:(int) aa
{
  unsigned char arry[1000]={"aa","vv","cc","cc","dd"......};
  NSArray *tmpary=arry;
  return tmpary;
}

a.c:

#include "a.H"
main (){
  // how do I call returnarray function to get that array in main class
}

I need that array in main and I need to retain that array function in separate class.

Can someone please provide a code example to do this?

2
  • 1
    It's clear you didn't try to COMPILE this example. I have cleaned up your code a little. Commented Jan 3, 2012 at 5:41
  • ok let me try to clear you. suppose i have an array like NSArray *tmparry={"aa","vv","cc","cc","dd","de".......}; it contains 1000 values and i wan to create an object of that function class in other callass and want all array values in other class... so please now tell me if you are clear.. Thanks in advance for all responces Commented Jan 3, 2012 at 7:11

3 Answers 3

7

These lines:

unsigned char arry[1000]={"aa", "vv", "cc", "cc", "dd", ...};
NSArray *tmpary=arry;

Should instead be:

unsigned char arry[1000]={"aa", "vv", "cc", "cc", "dd", ...};
NSMutableArray * tmpary = [[NSMutableArray alloc] initWithCapacity: 1000];
for (i = 0; i < 1000; i++)
{
    [tmpary addObject: [NSString stringWithCString: arry[i] encoding:NSASCIIStringEncoding]];
}

This is because a C-style array (that is, int arr[10]; for example) are not the same as actual NSArray objects, which are declared as above.

In fact, one has no idea what an NSArray actually is, other than what the methods available to you are, as defined in the documentation. This is in contrast to the C-style array, which you are guaranteed is just a contiguous chunk of memory just for you, big enough to hold the number of elements you requested.

Sign up to request clarification or add additional context in comments.

3 Comments

Note that stringWithCString: is deprecated. You should use one of the other methods to create an NSString from a C string.
Thanks @Peter. I meant to come back and update that. It's been edited.
shouldn't this be wrong? unsigned char arry[1000]={"aa", "vv", "cc", "cc", "dd", ...}; a String in C would be char* and having an array of strings is char**. The type of arry[] is only arry*.
1

C-style arrays are not NSArray's so your assignment of arry (the definition of which has some typos, at least the unsighned part) is not valid. In addition, you call arry an array of char, but you assign it an array of null-terminated strings.

In general you need to loop and add all the elements of the C-style array to the NSArray.

Comments

0

I'm not sure why you must do it in main. If you want a global you can do it by declaring a global in another file. That said, you CANNOT assign a plain C data array to an objective C NSArray, which is different in nature entirely.

1 Comment

Sure you can 'convert' an plain C data array. You can't coerce it, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.