title: Taking user input with pointers
body: <p>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:</p>
  
  <pre><code>float *GetValues(float *p, size_t n) 
  {   
  
      float input;
      int i = 0;
  
      if ((newPtr = (float *)malloc(n * sizeof(float))) == NULL) {
          cout &lt;&lt; "Not enough memory\n";
          exit(EXIT_FAILURE);
      }
      cout &lt;&lt; "Enter " &lt;&lt; n &lt;&lt; " float values separated by whitespace: \n";
  
      while (scanf("%f", &amp;input) == 1) {
          p[i] = input;
          i++;
          cout &lt;&lt; *p;
      }
      return p;
  }
  </code></pre>
  
  <p>But then if I do this:</p>
  
  <pre><code>float *GetValues(float *p, size_t n) 
  {   
      float *newPtr;
      float input;
  
      if ((newPtr = (float *)malloc(n * sizeof(float))) == NULL) {
          cout &lt;&lt; "Not enough memory\n";
          exit(EXIT_FAILURE);
      }
      cout &lt;&lt; "Enter " &lt;&lt; n &lt;&lt; " float values separated by whitespace: \n";
  
      while (scanf("%f", &amp;input) == 1) {
              *newPtr++ = input;
      }
      return newPtr;
  }
  </code></pre>
  
  <p>I get just 0s entered into p.  Why is that?</p>
  
  <p>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!</p>
  
  <p>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 &lt;&lt; *newPtr.</p>