C Style Arrays

C Style Arrays

Post by Michael J. Bergi » Sun, 22 Jun 2003 13:29:25



I'm trying to write a method that returns an array.  I've been trying to use
a C style array because that's all the functionality I need but can't seem
to get it to work.  Do I have to use NSArray?

I have this:

- (MyType[])getAllPossibleTypes {
     MyType[5] types = {TYPE1, TYPE2, TYPE3, TYPE4, TYPE5};
     return types;

Quote:}

 
 
 

C Style Arrays

Post by Joe Shimku » Sun, 22 Jun 2003 13:41:26




Quote:> I'm trying to write a method that returns an array.  I've been trying to use
> a C style array because that's all the functionality I need but can't seem
> to get it to work.  Do I have to use NSArray?

> I have this:

> - (MyType[])getAllPossibleTypes {
>      MyType[5] types = {TYPE1, TYPE2, TYPE3, TYPE4, TYPE5};
>      return types;
> }

Well, your 'types' array is going to cease to exist when the function
returns.  You might start with that.

--
PGP Key (DH/DSS): http://www.shimkus.com/public_key.asc
PGP Fingerprint:  89B4 52DA CF10 EE03 02AD  9134 21C6 2A68 CE52 EE1A

 
 
 

C Style Arrays

Post by nospa » Tue, 24 Jun 2003 12:23:35




Quote:> I'm trying to write a method that returns an array.  I've been trying to use
> a C style array because that's all the functionality I need but can't seem
> to get it to work.  Do I have to use NSArray?

> I have this:

> - (MyType[])getAllPossibleTypes {
>    MyType[5] types = {TYPE1, TYPE2, TYPE3, TYPE4, TYPE5};
>    return types;
> }

What on earth is a type of MyType[5]

Maybe you mean MyType  types[5] = {TYPE1, TYPE2, TYPE3, TYPE4, TYPE5};
Arrays in C are pointer-pointer types. Whats the rest of this code look
like?

  Hard to give a good example with out understanding what you intended with
MyTypes[5] and indeed what MyTypes is defined as?

    But in pure C

#include <stdio.h>

int *tst()
{
static  int types[5] = { 1,2,3,4,5 };

        return( types );

Quote:}

main()
{
        int     i;
        int     *a;

        a=tst();
        for (i=0;i<5;i++ ) {
                printf("%d:%d\n", i,a[i]);
        }

Quote:}

 
 
 

C Style Arrays

Post by Michael J. Bergi » Wed, 25 Jun 2003 10:39:14






>> I'm trying to write a method that returns an array.  I've been trying to use
>> a C style array because that's all the functionality I need but can't seem
>> to get it to work.  Do I have to use NSArray?

>> I have this:

>> - (MyType[])getAllPossibleTypes {
>>    MyType[5] types = {TYPE1, TYPE2, TYPE3, TYPE4, TYPE5};
>>    return types;
>> }

> What on earth is a type of MyType[5]

> Maybe you mean MyType  types[5] = {TYPE1, TYPE2, TYPE3, TYPE4, TYPE5};
> Arrays in C are pointer-pointer types. Whats the rest of this code look
> like?

> Hard to give a good example with out understanding what you intended with
> MyTypes[5] and indeed what MyTypes is defined as?

>   But in pure C

> #include <stdio.h>

> int *tst()
> {
> static  int types[5] = { 1,2,3,4,5 };

>       return( types );
> }

> main()
> {
>       int     i;
>       int     *a;

>       a=tst();
>       for (i=0;i<5;i++ ) {
>               printf("%d:%d\n", i,a[i]);
>       }
> }

I'm not real worried about the syntax, I can read that in a book on my own.
I guess to state my question more clearly, are C style arrays normally used
in Objective-C?  If so are there any limitations/bad points to using them?
 
 
 

C Style Arrays

Post by David Ste » Wed, 25 Jun 2003 12:29:24



Quote:

> I'm not real worried about the syntax, I can read that in a book on my own.
> I guess to state my question more clearly, are C style arrays normally used
> in Objective-C?

Yes, there is no difference with C in that respect.

Quote:> If so are there any limitations/bad points to using them?

There are restrictions on return values or arguments of messages.
These are related to how messages are used in Objective-C (they default to
returning objects and to having objects as arguments).

Some restrictions may exist due to the type casting when other types are used
for messages.

Basically, the "bad point" may consist of using too many typed constructs,
if you have a great deal of Objective-C messages that do not simply return
objects or take objects as arguments, then something's wrong.

The concept of the Objective-C messenger is that it tries to exchange as
much data as possible as "objects", not as typed data with a C type.

So as a question of programming style, one could say that if a program or
some library that uses Objective-C messages tends to heavily use C types, it
may be a good idea to revise that API, to revise that interface, and see whether it could not be better designed to use objects.