Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifier not found?

After two years of C#, I tried C and i have some "noob" errors.

I tried to reverse an array with recursion, and i have this error:

error C3861: 'Rekurzija' indentifer not found

this is my code:

#include "stdafx.h"
#include "stdio.h"

int main()
{
    int niz[] = {1,2,3,4,5,6};
    int duzina = sizeof(niz)/sizeof(int);
    printf("%s",niz[Rekurzija(duzina)]);
    getchar();
}

int Rekurzija(int niz)
{
    int i = sizeof(niz)/sizeof(int);
    while(i!=0)
        return Rekurzija(i-1);
}
like image 329
DocNet Avatar asked Oct 29 '12 20:10

DocNet


3 Answers

In C, everything must be declared before being used. So you must add a declaration for Rekurzija before main:

int Rekurzija(int);

This just tells the compiler that when it sees the Rekurzija call later, that is a function call taking an int and returning an int. That is all it needs to handle the call, the definition can be somewhere else, such below main in your case, or even in another file, as is very common (delcaration in a .h file, and definition in a .c file).

like image 196
amaurea Avatar answered Oct 19 '22 19:10

amaurea


In C you should have the function prototype listed before the function that calls it.

So you should add the following after your includes:

int Rekurzija(int niz);

Notice that without having a prototype, things still might work; the compiler will make a guess for the prototype but problems will arise if the guess is different than what your function really is.

like image 5
Fingolfin Avatar answered Oct 19 '22 19:10

Fingolfin


In C, you have to declare entities before you reference them.

You need to add:

int Rekurzija(int niz);

to tell the compiler that Rekurzija exists, and what kind of properties it has (e.g., it is a function with int argument and result) as a "forward" declaration preceding your main function.

like image 1
Ira Baxter Avatar answered Oct 19 '22 17:10

Ira Baxter