What is the issue with malloc?
Hi,
I tried writing a code to manipulate text files (code below). I currently have a students.txt file. The goal of this code is to read in student information from the students.txt file and the entries that have students with a passing score should be copied over to a 2nd file, the students_temp.txt file.
I am using microsoft visual c++ 2010 express, and the following issue occurs: the IDE detects an issue with malloc, and when I hover over it with my mouse, the following error message shows up:
Code:
"Error: a value of type "void*" cannot be used to initialize an entity of type "student"
Also the output box shows some other errors (which I can list if necessary)
I tried running exact same code using Code::Blocks and the Code Blocks ide does not have an issue and compiles perfectly fine. Why is that?
I have both IDE's setup to be compiling "C" code and not "C++" and not sure if this matters or not but one other thing I noticed is that both softwares are using different compilers (Code Blocks was set to use GNU gcc and microsoft visual C++ uses the microsoft compiler.)
I believe that malloc is being used in a valid way in my code as I have it written so not sure what is causing the issues :confused:
Just wanted to use malloc to allocate some memory space to accommodate for currently unknown size (number of students that have passing score is unknown at this time and so I wanted to allocate enough space just in case, if that makes any sense)
Thanks!!
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *name;
float score;
}student;
int main()
{
FILE *originalFile;
FILE* newFile;
originalFile = fopen("D:\\Documents\\Visual Studio 2010\\Projects\\C\\Test\\edit_File\\edit_File\\students.txt", "r");
newFile = fopen("D:\\Documents\\Visual Studio 2010\\Projects\\C\\Test\\edit_File\\edit_File\\students_temp.txt", "w");
int studentCount;
fscanf(originalFile, "%i", &studentCount);
student *passingStudents = malloc(sizeof(student) * studentCount);
fclose(originalFile);
fclose(newFile);
printf("Program ended. No error!\n");
return 0;
}