title: Following a pointer in C
body: <p>I will show the entire code, but look for the quotes above the pointers as I will be talking about my problem there. I will tell you what I think happened there at the quote comments. The code compiles and runs, I just need help understanding parts of the code.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  int *get_data(int num_grades);
  float calc_average(int num_grades, int grades[]);
  void display_average(float ave);
  
  int main()
  {
          int num_grades = 10;
          const int MAX_GRADE = 100;
  </code></pre>
  
  <blockquote>
    <p>Below this quote box at: <strong>int *result = get_data(num_grades);</strong></p>
    
    <p>I have a pretty simple understanding of pointers at this point, as I've been introduced to them recently, however I can't seem to wrap my head around what happens when you assign a function to a pointer. What I think should happen is that the pointer <strong>int *result</strong> should be pointing at an address. Instead its pointing to a function that is also a pointer i think. So is this a pointer to a pointer at: <strong>int *result = get_data(num_grades);</strong> ?</p>
    
    <p>I will put the rest of my question at the get_data function below.</p>
  </blockquote>
  
  <pre><code>        int *result = get_data(num_grades);
  </code></pre>
  
  <blockquote>
    <p><strong>^ Above ^</strong></p>
  </blockquote>
  
  <pre><code>        if (num_grades ==0)
                  printf("no grades to average\n");
          else
          {
                  float ave = calc_average(num_grades, result);
                  display_average(ave);
          }
  
          free(result);
          return 0;
  }
  
  float calc_average(int num_grades, int grades[])
  {
          float ave;
          int i;
          int sum = 0;
          for (i = 0; i &lt; num_grades; i++)
          {
                  sum+=grades[i];
          }
          ave = (float)sum/num_grades;
          return ave;
  }
  
  void display_average(float ave)
  {
          printf("average: %.2f\n", ave);
  }
  </code></pre>
  
  <blockquote>
    <p>Below this quote box,</p>
    
    <p>I think this is a function pointer that returns a int pointer? </p>
    
    <p>So, inside the body of the function, we create a new pointer, allocate memory for it, assign grades for each 'pointee' i think, and then I'm not sure why a[i] = grade is working, what are these indexes coming from, I don't understand why this is working if there is no array declared. Can someone explain what is happening here? How does this work? I'm really confused here.</p>
  </blockquote>
  
  <pre><code>int *get_data(int num_grades)
  {
  
  int* a;
  a = malloc(num_grades * sizeof(int));
  int i;
  for (i = 0; i &lt; num_grades; i++)
  {
          printf("enter a grade: ");
          int grade;
          scanf("%d", &amp;grade);
          if(grade&lt;=100)
          {
                  a[i] = grade;
          }
          else
          {
                  printf("grade needs to be &gt; 0 and &lt;= 100\n");
                  i--;
          }
  }
  return a;
  </code></pre>
  
  <blockquote>
    <p><strong>^ Above, this whole function ^</strong></p>
  </blockquote>
  
  <pre><code>}
  </code></pre>