0
#include <stdio.h>

void main()
{
    static int array[5] = { 200, 400, 600, 800, 1000 };
    int sum;

    int addnum(int *ptr); // what happened here ?

    sum = addnum(array);
    printf("Sum of all array elements = %5d\n", sum);
}
int addnum(int *ptr)
{
    int index, total = 0;
    for (index = 0; index < 5; index++)
    {
        total += *(ptr + index);
    }
    return(total);
}

int addnum(int *ptr); //Exactly what does this code mean ?

Just the topic or name of the concept would be fine . Thanks a lot in advance .

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Major
  • 53
  • 7
  • 3
    local forward declaration of the `addnum` method. Perfectly legal. – Jean-François Fabre Jan 28 '18 at 15:17
  • It's a [forward declaration](https://en.wikipedia.org/wiki/Forward_declaration) of function `addnum` – orhtej2 Jan 28 '18 at 15:17
  • 1
    It's **a bad practice** of forward declaration. – iBug Jan 28 '18 at 15:18
  • @iBug usually, stack overflow users abhor global variables. I wonder why they do not with global forward declarations.. – machine_1 Jan 28 '18 at 16:47
  • @machine_1 Because functions and variables are linked differently. – iBug Jan 28 '18 at 16:48
  • @iBug I understand, but sometimes, if a function need not be used except inside another one function only, then it is not a bad practice to forward-declare locally. – machine_1 Jan 28 '18 at 16:50
  • @machine_1 Your statement may be true for smaller, single-file programs. In practice it'll sooner or later become an issue as the size of the project grows. – iBug Jan 28 '18 at 16:51

1 Answers1

1

int addnum(int *ptr); // what happened here ?

It is so called forward declaration allowing compiler to know that such function will be defined later.

In C it is possible (although unwise) to implement a pair of mutually recursive functions thus:

int first(int x) {
        if (x == 0)
            return 1;
        else
            return second(x-1); // forward reference to second
}

int second(int x) {
    if (x == 0)
        return 0;
    else
        return first(x-1); // backward reference to first
}
iBug
  • 35,554
  • 7
  • 89
  • 134
sg7
  • 6,108
  • 2
  • 32
  • 40