-1

I'm done writing a code but can't figured out the solution for error. I think it's quite simple fix.

I have 3 files

grades.txt // contains 8 grades
grades.h // structure
grades.c // main

Here are the description of the program is asking

  1. Accepting two arguments, 1 for filename grades.txt, and 1 for user ID (ex.123)
  2. Display the grades and those average, until I have all 8 grades or met the negative grades
  3. Convert user ID into a number uid
  4. Read the structure starting at offset (uid*sizeof(struct GR))

grades.h

#ifndef GRADE
#define GRADE
struct GR {
    float gr[8];
};
#endif

grades.c

#include <stdio.h>
#include "grades.h"
int main(int argc, char *argv[]) {    
    struct GR *grades = melloc(sizeof(struct GR));
    FILE *file = fopen(argv[1], "rb");    

    // convert character uid into integer uid
    char *chUid = argv[2];
    int uid = 0;
    int len = strlen(chUid);
    for (int i = 0; i < len; i++) {
        uid = uid * 10 + (chUid[i] - '0');
    }
    printf("Converted uid: %d \n", uid);

    fread(grades, uid * sizeof(struct GR), 1, file);

    float total = 0, avg = 0;
    int i = 0;
    while (i < 9 && grades->gr[i] > 0) {
        printf("%d\n", grades->gr[i]);
        total += grades->gr[i];
        i++;
    }
    printf("Average: %d \n", total / i);        
    return 0;
}

my error msg are

error LNK2019: unresolved external symbol _melloc referenced in function _main
error LNK1120: 1 unresolved externals

----update------

I added header and changed the typo from melloc and malloc, the error is gone but I'm stuck on fread method.. struct is not having any value

2 Answers2

0

You've got

error LNK2019: unresolved external symbol _melloc referenced in function _main

because the function to allocate memory in C is called malloc and not melloc (melon maybe ;p):

void* malloc (size_t size);

You have to include a header in which malloc is declared:

#include <stdlib.h>
4pie0
  • 29,204
  • 9
  • 82
  • 118
0

Your error tells everything.

error LNK2019: unresolved external symbol _melloc referenced in function _main It is not melloc, it is malloc

Lefsler
  • 1,738
  • 6
  • 26
  • 46