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 6815535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:49:09+00:00 2026-05-26T20:49:09+00:00

I have a CGFloat pointer in my header that is used to point at

  • 0

I have a CGFloat pointer in my header that is used to point at a CGFloat array. I declare it like that: CGFloat* pointer;. In the initialization I try to set the pointer using this code:

CGFloat newArray[2] = {
            0.0f, 1.0f,
        };

        pointer = newArray;

This “actually” works. I don’t get any compiler errors and stuff. When i print the second value of the array right after setting the pointer with this code: printf("%f", pointer[1]);
I get the right result (1.000000). However, when I print the second value of the array in the method called next i get 0.000000 which means that the pointer doesn’t point at the array anymore.

I’ve got two questions. How do I fix this problem and why does the pointer work right after setting it but “forgets” its value again?

  • 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-26T20:49:09+00:00Added an answer on May 26, 2026 at 8:49 pm
    CGFloat newArray[2] = {
            0.0f, 1.0f,
        };
    

    Array indices start at 0. pointer[1] prints the second element in the array, not the first. pointer[2] is off the end of the array.

    CGFloat newArray[2] = {0,0};
    newArray[0]; // this is the 1st item
    newArray[1]; // this is the 2nd item
    newArray[2]; // this is nonsense.
    

    Note that if you try to pass newArray somewhere with the intention of using it later, it’ll be badness. newArray is on the stack and will be destroyed when the scope is destroyed.

    If you want a function that returns an array of two floats, you’d do:

     CGFloat *two_of_these() {
         CGFloat *newArray = malloc(2 * sizeof(CGFLoat));
         newArray[0] = 42.0;
         newArray[1] = 59.4;
         return newArray;
     }
    

    Just make sure and call free on that return value later. Note that it is exceedingly rare to allocate something so small. Typically, it’ll be a structure that is returned directly. See CGPoint, CGRect, NSRange, etc….

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

Sidebar

Related Questions

For example, I have properties like this: @property (nonatomic, assign) CGFloat startValue; @property (nonatomic,
Suppose I have a method like this - (UIButton *) createButtonAtX:(CGFloat)vX Y:(CGFloat)vY{ //... bla
I have a CGFloat that contains only integers, in the meaning that it actually
i have this method Code: - (void)addImageSubViewAtX:(CGFloat)x atY:(CGFloat)y { CGRect myImageRect1 = CGRectMake(x, y,
I have a custom UIView subclass that i'm trying to use as a header
i have added a pickerview from interface builder.i am using this method to show
I have this code for a scrollview to showe 3 images: const CGFloat kScrollObjHeight
I have a few functions that I'm using to draw different type of text.
I have a gradient that has rounded corners that I an using for a
I want to have a pointer to an UIImage view so that I don't

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.