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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:31:14+00:00 2026-05-13T19:31:14+00:00

I’m trying to get better at using pointers, and not using array notation. So

  • 0

I’m trying to get better at using pointers, and not using array notation. So I have a function to read user input and return a pointer to that array. I can do it like this and it seems to work ok:

float *GetValues(float *p, size_t n) 
{   

    float input;
    int i = 0;

    if ((newPtr = (float *)malloc(n * sizeof(float))) == NULL) {
        cout << "Not enough memory\n";
        exit(EXIT_FAILURE);
    }
    cout << "Enter " << n << " float values separated by whitespace: \n";

    while (scanf("%f", &input) == 1) {
        p[i] = input;
        i++;
        cout << *p;
    }
    return p;
}

But then if I do this:

float *GetValues(float *p, size_t n) 
{   
    float *newPtr;
    float input;

    if ((newPtr = (float *)malloc(n * sizeof(float))) == NULL) {
        cout << "Not enough memory\n";
        exit(EXIT_FAILURE);
    }
    cout << "Enter " << n << " float values separated by whitespace: \n";

    while (scanf("%f", &input) == 1) {
            *newPtr++ = input;
    }
    return newPtr;
}

I get just 0s entered into p. Why is that?

Also, do I have to allocate memory here for an array of size n? I first tried it with the method above using pointers and not allocating memory but just set p = to input and I was getting garbage values. Thanks!

Edited: Sorry, I allocated a new ptr and was returning the wrong one like you said. I was just trying to input the numbers into my pointer and display it back on the screen to myself and wasn’t paying attention to the return type and I was getting an output of 0 when I would cout << *newPtr.

  • 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-13T19:31:14+00:00Added an answer on May 13, 2026 at 7:31 pm

    A few tips:


    The first GetValues allocates newPtr (which is not declared within the function, a global variable?) but then does nothing with it. There are two possible ways that your function could work with regards to memory storage:

    1. The function gets a pointer to valid memory for an array of size large enough. In that case the signature of the function ought to be

      float *GetValues(float *array, size_t arraySize)

      to more clearly state the nature of the arguments. You need not to allocate anything inside the function.

    2. The function should allocate the needed memory itself and let the caller free the memory some time later. In that case you MUST have some kind of hint in the name of the function that it allocates memory. Otherwise you are in for a disaster in terms of maintaining this code because it will be extremely easy to make mistakes in freeing memory. You will not need to pass an array pointer to the function, but if you do it needs to be a double pointer for it to be of any meaning (C and C++ passes arguments by value so it is not possible to change a pointer which is passed as an argument)

      float *GetValuesAndAllocateMemmory(size_t n)

      float *GetValuesAndAllocateMemmory(float **array_pp, size_t n)


    The loop in the second GetValues should be

    float *start_p = p;
    ...
    while (scanf("%f", &input) == 1) {
            *p++ = input;
            cout << *start_p;
    }
    return start_p;
    

    in order to be identical to the first GetValues.


    Here there is upper limit of number of float values is n but your code does not check this

    cout << "Enter " << n << " float values separated by whitespace: \n";
    while (scanf("%f", &input) == 1) {
    

    and will crash if more than n floats are entered. Never trust user input data, even when it comes from yourself. Always verify. Search for the term “input validation” for more information about this.

    cout << "Enter " << n << " float values separated by whitespace: \n";
    entered_values = 0;
    while (entered_values < n && scanf("%f", &input) == 1) {
           entered_values++;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 442k
  • Answers 442k
  • 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 For OSX >= 10.6 use NSWorkSpace: -desktopImageURLForScreen: -setDesktopImageURL:forScreen:options:error: For a… May 15, 2026 at 6:00 pm
  • Editorial Team
    Editorial Team added an answer Well not seeing your HTML, I can't say exactly what… May 15, 2026 at 6:00 pm
  • Editorial Team
    Editorial Team added an answer The navigationController property of UIViewController refers to the nav controller… May 15, 2026 at 6:00 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.