isaric
isaric

Reputation: 305

Why am I getting "undefined reference to" error?

The code below throws an error "undefined reference to 'poredi'".

Everything is defined in this single c file (aside from the c libs in include).

'poredi' is just a function, of which I define a prototype right below the typedef and then I impelement it lower in the file.

Looking at some of the similar questions I can say that it is compiled on Windows 10 using the MinGW C compiler through the CodeBlocks IDE without any additional arguments for compiling.

I am a total C noob so any help is appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    char ime[20];
    char prezime[20];
    char registracija[10];
    int dRod;
    int mRod;
    int gRod;
    char datumIzdavanja[10];
} vozac;

void poredi(vozac *vozaci, int linije);
int nlines(char *datoteka);
void napuni(FILE *ulaz, vozac *vozaci, int lines);

int main(){
    int lines = nlines("Registar.in");
    FILE *ulaz = fopen("Registar.in", "r");
    int i;
    FILE *izlaz = fopen("Najmladji.out", "w");
    vozac *vozaci = calloc(lines,sizeof(vozac));
    napuni(ulaz,vozaci,lines);
    fclose(ulaz);
    poredi(vozaci,lines);
    for (i = 0; i < 5; i++){
        printf("\n%s %s (%d.%d.%d)", vozaci[i].ime, vozaci[i].prezime, vozaci[i].dRod, vozaci[i].mRod, vozaci[i].gRod);
    }
    fclose(izlaz);
    return 0;
}
int nlines(char *datoteka){
    FILE *ulaz = fopen("Registar.in", "r");
    int brojac = 0;
    while (!feof(ulaz)){
        char ch = fgetc(ulaz);
        if (ch == '\n') brojac++;
    }
    fclose(ulaz);
    return brojac;
}
void napuni(FILE *ulaz, vozac *vozaci, int lines){
    int i;
    char *linija = calloc(70, sizeof(char));
    char *token;
    char *token2;
    char *znak;
    char *znak2;
    for (i= 0; i < lines; i++){
        fgets(linija,70,ulaz);
        token = strtok(linija," ");
        strcpy(vozaci[i].prezime, token);
        znak = strchr(vozaci[i].prezime,',');
        *znak = 0;
        token = strtok(NULL," ");
        strcpy(vozaci[i].ime, token);
        znak2 = strchr(vozaci[i].ime, ';');
        *znak2 = 0;
        token = strtok(NULL, " ");
        strcpy(vozaci[i].registracija, token);
        token = strtok(NULL, " ");
        token2 = strtok(token, ".");
        vozaci[i].dRod = atoi(token2);
        token2 = strtok(NULL, ".");
        vozaci[i].mRod = atoi(token2);
        token2 = strtok(NULL, ".");
        vozaci[i].gRod = atoi(token2);
        token = strtok(NULL, " ");
        strcpy(vozaci[i].datumIzdavanja, token);
    }
void poredi(vozac *vozaci, int linije){
    int sortirano = 1;
    vozac buffer;
    while (sortirano){
        sortirano = 0;
        for (i = 0; i < linije; i++){
            if (vozaci[i].gRod > vozaci[i+1].gRod){
                buffer = vozaci[i];
                vozaci[i] = vozaci[i+1];
                vozaci[i + 1] = buffer;
                sortirano = 1;
            }
            else if (vozaci[i].gRod == vozaci[i + 1].gRod){
                if (vozaci[i].mRod > vozaci[i + 1].mRod){
                    buffer = vozaci[i];
                    vozaci[i] = vozaci[i+1];
                    vozaci[i + 1] = buffer;
                    sortirano = 1;
                }
                else if (vozaci[i].mRod == vozaci[i + 1].mRod){
                    if (vozaci[i].dRod > vozaci[i + 1].dRod){
                        buffer = vozaci[i];
                        vozaci[i] = vozaci[i+1];
                        vozaci[i + 1] = buffer;
                        sortirano = 1;
                    }
                }
            }
        }
    }
}
}

Upvotes: 1

Views: 523

Answers (1)

P.P
P.P

Reputation: 121347

You have poredi() defined inside napuni(). Nested functions is not valid in ISO C but gcc allows it as an extension. I doubt you actually intended to use nested functions but misplaced of braces. Basically remove one brace } from the end of your source file add it above the definition of poredi().

A better indentation would have help avoid such surprises.

Upvotes: 4

Related Questions