Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 662013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:19:53+00:00 2026-05-13T23:19:53+00:00

I was checking out this question which has this code – (NSArray *) percentagesRGBArray:(float[])

  • 0

I was checking out this question which has this code

- (NSArray *) percentagesRGBArray:(float[]) rgbArray
{
    NSNumber *red = [NSNumber numberWithFloat:rgbArray[0] / 255];
    NSNumber *green = [NSNumber numberWithFloat:rgbArray[1] / 255];
    NSNumber *blue = [NSNumber numberWithFloat:rgbArray[2] / 255];
    NSNumber *alpha = [NSNumber numberWithFloat:rgbArray[3]];
    return [NSArray arrayWithObjects:red, green, blue, alpha, nil];
}

and I thought, “that’s terrible, what if you have more than three colors?” I know, you don’t, but what if you did have count-1 colors and an alpha? Let’s say you had [rgbArray count] (does count even work for a real array?) Using only objective-C, what the normal way that you would return an NSArray of n objects?

I just tried to work it out but I still don’t have the chops to do this in objective-C. Here’s my failed attempt:

- (NSArray *) what:(float[]) rgbArray
{
    int len = sizeof(rgbArray)/sizeof(float); // made up syntax
    NSLog(@"length is wrong dummy %d", len);
    NSNumber *retVal[len];
    for (int i=0;i<(len-1);i++) {
        NSNumber *red = [NSNumber numberWithFloat:rgbArray[0] / 255];
        retVal[i] = red;
        [red release];
    }
    retVal[len-1] = [NSNumber numberWithFloat:rgbArray[len-1]];
    return [NSArray arrayWithObjects:retVal count:len];

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:19:53+00:00Added an answer on May 13, 2026 at 11:19 pm

    Well, just as arrayWithObjects:count: has count: part, you can do

    - (NSArray *) what:(float[]) rgbArray count:(int)len
    {
        NSMutableArray*result=[NSMutableArray array];
        for (int i=0;i<len;i++) {
            NSNumber *red = [NSNumber numberWithFloat:rgbArray[0] / 255];
            [result addObject:red];
        }
        return result;
    }
    

    If you want, I can be as close as what you wrote, which would be

    - (NSArray *) what:(float[]) rgbArray count:(int)len
    {
        NSNumber**retVal=malloc(len*sizeof(NSNumber*));
        for (int i=0;i<len;i++) {
            NSNumber *red = [NSNumber numberWithFloat:rgbArray[0] / 255];
            retVal[i]=red;
        }
        NSArray*result=[NSArray arrayWithObjects:retVal count:len];
        free(retVal);
        return result;
    }
    

    At this stage, it’s not really a question of Objective-C, but is a question of just plain C, right? Your questions are

    1. how to dynamically allocate an array in C and
    2. how to get the size of an array from a function in C.

    The answers are

    1. You use malloc.
    2. You can’t do that, so you need to pass that to the function.

    That’s it.
    So, if you have a question about how to deal with C arrays, you should ask C experts… there’s nothing special about Objective-C, except the method declaration syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You might want to look into http://www.sequelpro.com/docs/Source_Code. They seem to… May 14, 2026 at 10:35 pm
  • Editorial Team
    Editorial Team added an answer Basically you only need to worry about synchronisation if data… May 14, 2026 at 10:35 pm
  • Editorial Team
    Editorial Team added an answer A wait() only makes sense when there is also a… May 14, 2026 at 10:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.