In the following code, I have a function that is called in main by the line
The function is suppose to do, as the title would suggest, load a file of numbers (one number per line, of N lines) into an array. I had it running at one point but got a segfault, and realize I needed to allocate memory for my array. However, as the code below is, I get an error on line 20 that says "error: invalid operands to binary * (have 'int *' and 'unsigned int'). I understand the error, but don't know how to fix it. I think I am not finding Size correctly.
I haven't coded in quite a while, so I would appreciate some help!
Code:
Array = Load_File (Filename, &Size);
I haven't coded in quite a while, so I would appreciate some help!
Code:
#include <stdio.h>
#include <stdlib.h>
#include "sorting.h"
long *Load(char *File, int *Size) {
FILE *fp = fopen(File, "r");
if (fp == NULL) { // check to see if file opens; if NULL, failed
printf("Error opening file. \n");
return 0;
}
fscanf(fp, "%d", Size); // First line of input file is designated as the total number of elements in file; store it into "Size"
long *Array = malloc(Size * sizeof(long)); // Error is here
if (Array == NULL) {
printf("Out of memory!\n");
return 0;
}
while (! feof(fp)) {
fscanf(fp, "%ld", Array); // Store all numbers into Array
}
fclose(fp);
return(Array); // Return array for main.c
}
Comment